PSFramework
はPowerShell Gallery
に公開されているPowerShellモジュールとなっており、Framework
と名付けられているだけあって、各種便利機能が提供されています。
Prominent features:
Advanced message & logging functionality
Manage scripts & modules using DSC
Simple Tab Completion
Runspace-safe development
PowerShell Scheduled tasks
今回はこのフレームワークに含まれているloggingの機能利用してみます。
PowerShellでスクリプトを作成する際に、ログ出力をするためのその場限りのlogger
を作成したりしますが。
こういうフレームワークを使うのも、一つの方法かと思います。
PSFramework
のメンテナーはFriedrich Weinmannさん。
各種サイト
- PSFramework
- PSFramework Documentation
- PowerShell Framework
- Logging in a DevOps world by Friedrich Weinmann
- FriedrichWeinmann/P2019-PSSummit-Logging-in-a-DevOps-World
- PSConfAsia 2018: Logging in a DevOps world
今回利用する環境
- Windows 21H2
- PSFramework 1.6.214
インストール
# ユーザスコープにインストールする場合
Install-Module PSFramework -Scope CurrentUser
# インストールチェック
Get-Module PSFramework -ListAvailable
ログを書き込んでみる
PSFramework
ではWrite-PSFMessage
コマンドレットを利用してログを書込みます。
Write-PSFMessage -Message "HelloWorld"
Write-PSFMessage -Message "HelloWorld" -Level Verbose
Write-PSFMessage -Message "HelloWorld" -Level Host
Write-PSFMessage -Message "HelloWorld" -Level Warning
Write-PSFMessage -Message "HelloWorld" -Level Error
Write-PSFMessage -Message "HelloWorld" -Level Debug
書き込まれたログについて
Getting Started with Logging
Logging to: Debuglog
PSFramework
ではWrite-PSFMessage
でログを書き込む事がわかりましたが。
ここでは書き込まれたログがどう扱われているか確認します。
PSFramework
ではログをメモリ
と`ファイルシステム(AppDataフォルダ配下のファイル)`に保持します。
メモリ上のログを確認する Get-PSFMessage
PSFramework
ではGet-PSFMessage
コマンドを利用して、Write-PSFMessage
コマンドレってで書込みしたメモリ上のログを取得します。
APPDATA配下のログファイルを確認する(Logging Provider:FileSystem)
ログファイルはPSFramework
のConfig psframework.logging.filesystem.logpath
で設定された場所に出力されており。
Get-PSFConfigValue
コマンドレットで設定値を確認できます。
# ログファイルの保存先確認
Get-PSFConfigValue -FullName psframework.logging.filesystem.logpath
# ログファイルの保存先を開く
Get-PSFConfigValue -FullName psframework.logging.filesystem.logpath | ii
上記のようにファイルに実行日時とメッセージが保存されています。
Logging Provider(Logfile)
PSFramework
では書き込まれたログはデフォルトで設定されたフォルダにファイルとして保持されます。
特定の処理のログを設定したい場合は、Logging Provider
を定義して利用します。
ここではLogfile
として出力するLogginf Provider
を定義します。
$paramSetPSFLoggingProvider = @{
Name = 'logfile'
InstanceName = '<taskname>'
FilePath = 'C:\Logs\TaskName-%Date%.csv'
Enabled = $true
}
Set-PSFLoggingProvider @paramSetPSFLoggingProvider
上記の例では、$paramSetPSFLoggingProviderというhashテーブルをSet-PSFLoggingProvider
にスプラフティングで渡して、Logging Provider(Logfile)
を定義している。
Logging Provider(Logfile)
を定義した後に、Write-PSFMessage
でログに書込みを行うと、Logging Provider(Logfile)
で定義されたFilePath
であるC:\Logs
ディレクトリにTaskName-%Date%.csv
という形式でファイルが保存されます。
ログローテーションについて
先程、c:\Logs
にログファイルを出力しましたが。
出力されたログをログローテーションしたい場合はLogging Provider
にLogRotatePath
を指定します。
$paramSetPSFLoggingProvider = @{
Name = 'logfile'
InstanceName = '<taskname>'
FilePath = 'C:\Logs\TaskName-%Date%.csv'
Enabled = $true
LogRotatePath = 'C:\Logs\TaskName-*.csv'
}
Set-PSFLoggingProvider @paramSetPSFLoggingProvider
このように指定する事で、Logging Provider
が実行されるたびにLogRetentionTime(既定値30日)
でログローテーションされるようです。
注意したいのは、ログローテーションが実行されるのは、定義したLogging Provider
が実行されたタイミングとなるため。
LogRetentionTime
を7日と設定しても、Logging Provider
が30日に一度しか動かない場合は、ログローテーションが実行されるのは30日に一度となります。
FilePathで利用できるプレースホルダについて
Logging Provider
のFilePath
で`&Date'というプレースホルダを利用していますが、利用できるプレースホルダについては上記ドキュメントに記載があります。
Placeholder | Value |
---|---|
%date% | The current date in a yyyy-MM-dd format |
%dayofweek% | The current day of the week (Monday, Tuesday, …) |
%day% | The current day of the month |
%hour% | The current hour of the day |
%minute% | The current minute of the hour |
%username% | The name of the current user, picked from environment variables. |
%userdomain% | The domain of the user, picked from environment variables. |
%computername% | The name of the current computer, picked from environment variables. |
%processid% | ID of the current process |
%logname% | The value of the setting/parameter.logname |
総評
本記事ではPSFramework
でとあえすログファイルとして出力する所まで説明してみました。
Logfile
についてもcsv以外の出力形式で出力できたり。
Logfile
なLogging Provider
以外にもWindows Eventlog
だったりSplunk
などなどのLogging Provider
が用意されており、様々な出力に対応していたり。
奥深いログ機能が提供されています。
モジュール作成者の公演動画があるので、こちらを見るのがわかり易い?