1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

PowerShellでPSFrameworkのLoggingを利用してログ出力してみる

Posted at

PSFrameworkPowerShell 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さん

各種サイト

今回利用する環境

  • Windows 21H2
  • PSFramework 1.6.214

インストール

# ユーザスコープにインストールする場合
Install-Module PSFramework -Scope CurrentUser

# インストールチェック
Get-Module PSFramework -ListAvailable

ログを書き込んでみる

PSFrameworkではWrite-PSFMessageコマンドレットを利用してログを書込みます。

helloworld.ps1
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

image.png

書き込まれたログについて

Getting Started with Logging
Logging to: Debuglog

PSFrameworkではWrite-PSFMessageでログを書き込む事がわかりましたが。
ここでは書き込まれたログがどう扱われているか確認します。

PSFrameworkではログをメモリと`ファイルシステム(AppDataフォルダ配下のファイル)`に保持します。

メモリ上のログを確認する Get-PSFMessage

PSFrameworkではGet-PSFMessageコマンドを利用して、Write-PSFMessageコマンドレってで書込みしたメモリ上のログを取得します。

image.png

APPDATA配下のログファイルを確認する(Logging Provider:FileSystem)

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

image.png

image.png

image.png

上記のようにファイルに実行日時とメッセージが保存されています。

Logging Provider(Logfile)

Writing Logging Providers

Logging to: A 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という形式でファイルが保存されます。

image.png

ログローテーションについて

Log Rotation

先程、c:\Logsにログファイルを出力しましたが。
出力されたログをログローテーションしたい場合はLogging ProviderLogRotatePathを指定します。

$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 to: A Logfile

Logging ProviderFilePathで`&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以外の出力形式で出力できたり。

LogfileLogging Provider以外にもWindows EventlogだったりSplunkなどなどのLogging Providerが用意されており、様々な出力に対応していたり。

奥深いログ機能が提供されています。

モジュール作成者の公演動画があるので、こちらを見るのがわかり易い?

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?