0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

特定日付のログ内容を抽出するコマンド

Posted at

概要

サーバに出力されているログの内容を特定日付のみ確認したい場合のメモです。

事前準備

確認したいログファイルを用意します。

確認コマンド

以下のコマンドを入力します。

sudo grep error.log -e '検索文字列'
※正規表現も指定可能

コマンド実行結果

ログファイルに出力されている内容です。

[linux@localhost php-fpm]$ sudo cat error.log
[sudo] password for linux:
[06-Aug-2024 03:41:16] NOTICE: fpm is running, pid 3555
[06-Aug-2024 03:41:16] NOTICE: ready to handle connections
[06-Aug-2024 03:41:16] NOTICE: systemd monitor interval set to 10000ms
[06-Aug-2024 03:41:43] NOTICE: Terminating ...
[06-Aug-2024 03:41:43] NOTICE: exiting, bye-bye!
[21-Aug-2024 00:27:38] NOTICE: fpm is running, pid 4143
[21-Aug-2024 00:27:38] NOTICE: ready to handle connections
[21-Aug-2024 00:27:38] NOTICE: systemd monitor interval set to 10000ms

①特定日付のログのみ確認します

[linux@localhost php-fpm]$ sudo grep error.log -e '06-Aug-2024'
[06-Aug-2024 03:41:16] NOTICE: fpm is running, pid 3555
[06-Aug-2024 03:41:16] NOTICE: ready to handle connections
[06-Aug-2024 03:41:16] NOTICE: systemd monitor interval set to 10000ms
[06-Aug-2024 03:41:43] NOTICE: Terminating ...
[06-Aug-2024 03:41:43] NOTICE: exiting, bye-bye!
[linux@localhost php-fpm]$

②特定日付と特定時間まで指定してログを確認します
時間帯03時からのログのみ出力されます。

[linux@localhost php-fpm]$ sudo grep error.log -e '06-Aug-2024 03'
[06-Aug-2024 03:41:16] NOTICE: fpm is running, pid 3555
[06-Aug-2024 03:41:16] NOTICE: ready to handle connections
[06-Aug-2024 03:41:16] NOTICE: systemd monitor interval set to 10000ms
[06-Aug-2024 03:41:43] NOTICE: Terminating ...
[06-Aug-2024 03:41:43] NOTICE: exiting, bye-bye!
[linux@localhost php-fpm]$

③特定日付と特定時間以後のログを確認します
03時以後のログを出力されます。
※test1-test5は確認のため、ログファイルに追加した内容です。

[linux@localhost php-fpm]$ sudo grep error.log -e '06-Aug-2024 0*'
[06-Aug-2024 03:41:16] NOTICE: fpm is running, pid 3555
[06-Aug-2024 03:41:16] NOTICE: ready to handle connections
[06-Aug-2024 03:41:16] NOTICE: systemd monitor interval set to 10000ms
[06-Aug-2024 03:41:43] NOTICE: Terminating ...
[06-Aug-2024 03:41:43] NOTICE: exiting, bye-bye!
[06-Aug-2024 05:41:43] NOTICE: test1 bye-bye!
[06-Aug-2024 08:41:43] NOTICE: test2 bye-bye!
[06-Aug-2024 11:41:43] NOTICE: test3 bye-bye!
[06-Aug-2024 20:41:43] NOTICE: test4 bye-bye!
[06-Aug-2024 23:41:43] NOTICE: test5 bye-bye!
[linux@localhost php-fpm]$

④特定日付を複数指定も可能です。

[linux@localhost php-fpm]$ sudo grep error.log -e '06-Aug-2024' -e '21-Aug-2024'
[06-Aug-2024 03:41:16] NOTICE: fpm is running, pid 3555
[06-Aug-2024 03:41:16] NOTICE: ready to handle connections
[06-Aug-2024 03:41:16] NOTICE: systemd monitor interval set to 10000ms
[06-Aug-2024 03:41:43] NOTICE: Terminating ...
[06-Aug-2024 03:41:43] NOTICE: exiting, bye-bye!
[06-Aug-2024 05:41:43] NOTICE: test1 bye-bye!
[06-Aug-2024 08:41:43] NOTICE: test2 bye-bye!
[06-Aug-2024 11:41:43] NOTICE: test3 bye-bye!
[06-Aug-2024 20:41:43] NOTICE: test4 bye-bye!
[06-Aug-2024 23:41:43] NOTICE: test5 bye-bye!
[21-Aug-2024 00:27:38] NOTICE: fpm is running, pid 4143
[21-Aug-2024 00:27:38] NOTICE: ready to handle connections
[21-Aug-2024 00:27:38] NOTICE: systemd monitor interval set to 10000ms
[linux@localhost php-fpm]$

終わりに

今までcatコマンドとgrepコマンドの組み合わせで使っていましたが、
grepコマンドだけでもできますね。
これからはこのコマンドを使うようにします。
今日はここまでです!!!

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?