スキップしてメイン コンテンツに移動

投稿

ラベル(log)が付いた投稿を表示しています

PowerShellでStart-Processで起動したアプリケーションのログをファイルとコンソールの両方に出力

はじめに PowerShell上で Start-Process コマンドを使って起動したアプリケーションは -RedirectStandardOutput や RedirectStandardError オプションを指定することで、ログを ファイルに出力 できます(コンソールには出力できないので注意)。 このログを同時にコンソールにも表示させたかったのですが、思いのほか苦戦したので備忘録として共有します。 できた方法 結局出力されたログファイルを、別起動したプロセスでTailするという方法を採用しました。 下記はsomethig.logに書き込みつつ同時にコンソールにもログの内容を出力するPowerShellの例です。 try { $logJobObj = Start-Job - ScriptBlock { Get-Content - Path ".\something.log" - Wait } $mainProcessObj = Start-Process - PassThru - FilePath process . exe - ArgumentList "-something_argument... -RedirectStandardOutput " . \something . log " RedirectStandardError " . \something . log" / / アプリケーションが終了するまで待つ While ( ! $mainProcessObj . HasExited ) { / / ログをTailしているJobの出力を受け取る Receive-Job $logJobObj / / 適当な秒数待つ Start-Sleep - Seconds 1 } Receive-Job $logJobObj } finally { if ( $logJobObj ) { Receive-Job $logJobObj / / ログをTailしているJobを終了 Stop-Job $logJobObj Re

Linuxコマンドの組み合わせだけで、前日の日付がが入ったファイルのみgzip圧縮する方法

ログファイルなどでは日ごとにファイルを生成し、日付がファイル名に入っている場合があると思います。 前日までの日付のファイルをすべてgzipで圧縮するLinuxコマンドを紹介します。 注: コマンドの組み合わせは、もっと少なくできる方法もあるかもしれませんが、今回紹介したコマンドの組み合わせでも、たたき台にはなるかと思いますので、参考にしてみてください。 # /dir/ディレクトリはいかにある、拡張子がlogのファイルでかたファイル名に含まれる日付がコマンド実行時の前日のものをすべてgzip圧縮するコマンドの組み合わせ。 find /dir/ -type f -name "*.log" | sed 's/.*_\([0-9]*\)\.log/\1/` | awk -v date="$(date +%Y%m%d)" '{if ($0 < date) print $0}' | xargs -I {} find /dir/ -name "*{}.log" | xargs --no-run-if-empty gzip 簡単に解説です。 find /dir/ -type f -name "*.log" :dirディレクトリ配下で拡張子がlogのファイルをすべてリストアップ sed 's/.*_\([0-9]*\)\.log/\1/` :ファイル名から日付部分のみを抜き出し awk -v date="$(date +%Y%m%d)" '{if ($0 < date) print $0}' :コマンド実行時の前日の日付のみ抜き出し。ちなみにawkのvオプションで変数dateを定義しています。 xargs -I {} find /dir/ -name "*{}.log" :前日以前の日付が含まれる拡張子がlogのファイルをすべて取得。ちなみにxargsの -Iオプションの{}の部分で日付部分をfindコマンドに渡しています。 xargs --no-run-if-empty gzip :ファイルがなかったら実行しないというxargsのオプション

Static Access to Symfony2 Logger

If you would like to use logging functionality in Symfony2, you should access logger service via container. For example in controller, you can easily access container and access logger service. $logger = $this->get('logger'); $logger->info('I just got the logger'); $logger->error('An error occurred'); Well, that's it. However, this is big however for me, what should I do when accessing logger not from controller class? Of course you can pass container or logger service itself to the class in service.yml service_use_logger: class: LoggingExample\Service\ServiceUsingLogger arguments: - @logger A bit pain everytime we should passing logger to the service class if you would like to use logger. For Java or .Net, I have never seen general logging functionality is provided from dependency injection container. For Almost all logging library, logger can be accessible as static - for example log4j. I tried to make logging functio

File Clean Up and Archiving Linux Command Tips

If you would like to delete files whose name ends with "batch.log" older than 30 days, Use the following command. find /var/log/ -name "*batch.log" -mtime +30 -type -f -delete If you would like to archive files whose name ends with "batch.log" older than 30 days, Use the following command. The archived file will have timestamp at the end of the filename. find /var/log/ -name "*batch.log" -mtime +30 -type -f -pront0 | tar -czvf /var/log/batch.log.tar.gz.`date +\%Y%m%d%H%M%S` -T - --null --remove-files