LoginSignup
6
2

More than 3 years have passed since last update.

【ログ出力設定】rsyslogとloggerコマンド。

Last updated at Posted at 2020-06-26

実施環境:RHEL7.3

ログ出力は重要です。RHEL7におけるrsyslogとloggerコマンドによるログ出力の一例を記載します。
サーバー名がclash royalなのは私の好きなゲームです。気にしないでください。(笑)

まずは、理解のため、ユーザーアプリケーション等のためのログ出力を行います。
出力したいログファイル名:/root/test/test_tool.log

①ログファイルの準備

[root@clash_royale ~]# mkdir -p /root/test
[root@clash_royale ~]# cd /root/test
[root@clash_royale test]# touch test_tool.log
[root@clash_royale test]# ls -l /root/test/*
-rw-r--r-- 1 root root 0 6月 26 21:23 /root/test/test_tool.log

空のログファイルができました。

②rsyslog設定

【まずは起動確認】
[root@clash_royale ~]# systemctl status rsyslog
● rsyslog.service - System Logging Service
Loaded: loaded (/usr/lib/systemd/system/rsyslog.service; enabled; vendor preset: enabled)
Active: active (running) since 金 2020-06-26 18:28:01 JST; 2h 44min ago
Main PID: 2661 (rsyslogd)
CGroup: /system.slice/rsyslog.service
mq2661 /usr/sbin/rsyslogd -n

6月 26 18:28:01 clash_royale systemd[1]: Starting System Logging Service...
6月 26 18:28:01 clash_royale systemd[1]: Started System Logging Service.

【設定ファイル修整!/etc/rsyslog.conf】
[root@clash_royale ~]# ls -l /etc/rsyslog.conf
-rw-r--r-- 1 root root 3429 6月 26 18:27 /etc/rsyslog.conf

気にすべきはこの辺でス。↓↓↓
別のサーバー(管理サーバーなど)へログ出力を飛ばすことなどができますが、今回は記載しません。
必要ないところは省略してます。

[root@clash_royale ~]# cat /etc/rsyslog.conf
#### RULES ####

# Log all kernel messages to the console.
# Logging much else clutters up the screen.
#kern.*                                                 /dev/console

# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
#mail.none;authpriv.none;cron.none;                /var/log/messages

# The authpriv file has restricted access.
authpriv.*                                              /var/log/secure

# Log all the mail messages in one place.
mail.*                                                  -/var/log/maillog


# Log cron stuff
cron.*                                                  /var/log/cron

# Everybody gets emergency messages
*.emerg                                                 :omusrmsg:*

# Save news errors of level crit and higher in a special file.
uucp,news.crit                                          /var/log/spooler

# Save boot messages also to boot.log
local7.*                                                /var/log/boot.log

1行の見方としては
ファシリティ.プライオリティ アクション
となります。

ファシリティとは出力対象ログ、プライオリティとはサービスのメッセージ種別、アクションとは出力先ログファイルとなります。

ファシリティ一覧↓↓↓

facility 対象のログ
authpriv 認証サービス
cron cronのメッセージ
kern カーネルのメッセージ
lpr プリンタサービスのメッセージ
mail メールサービスのメッセージ
user ユーザープロセスのメッセージ
uucp uucp転送を行うプログラムのメッセージ
local0~7 独自に利用できるファシリティ

プライオリティ一覧↓↓↓

priority 内容
emerg システムが停止するようなエラーを示すメッセージ
alert 緊急に対処すべきエラーを示すメッセージ
crit ハードウェアの傷害など致命的なエラーを示すメッセージ
err 一般的なエラーメッセージ
warning 警告メッセージ
notice 通知メッセージ
info 一般的な情報メッセージ
debug デバッグメッセージ
none ログメッセージ出力しない設定
* すべてのプライオリティを示す

プライオリティには=も使えます。.だけだと例えばinfo「以上」の情報を出力となるのですが、.=infoだとinfo情報「のみ」となります。

今回やりたいことはユーザーアプリケーションなのでlocal0にtest_tool.logへの出力設定しましょう。
(なぜかlocal7はデフォルトで使用されていることに注意してください。
# Save boot messages also to boot.logの箇所ですね。)

では、/etc/rsyslog.confに以下を追記します。

\#tool.log
local0.info                                             /root/test/test_tool.log

追記したらrsyslogを再起動します。

[root@clash_royale test]# systemctl restart rsyslog
[root@clash_royale test]# systemctl status rsyslog
● rsyslog.service - System Logging Service
   Loaded: loaded (/usr/lib/systemd/system/rsyslog.service; enabled; vendor preset: enabled)
   Active: active (running) since 金 2020-06-26 21:49:38 JST; 2min 40s ago
 Main PID: 2787 (rsyslogd)
   CGroup: /system.slice/rsyslog.service
           mq2787 /usr/sbin/rsyslogd -n

 6月 26 21:49:38 clash_royale systemd[1]: Starting System Logging Service...
 6月 26 21:49:38 clash_royale systemd[1]: Started System Logging Service.

これでログ出力設定は完了になります。実際にログが出力されることを確認しましょう!

出力テスト

コマンド実行コンソールと出力確認コンソールを立ち上げ、監視します。
【出力確認コンソール】

[root@clash_royale ~]# tail -f /root/test/test_tool.log



では実際に出力させます。ここでは、info情報として、info_test_dazeと出力させることにします。
【コマンド実行コンソール】

[root@clash_royale test]# logger -p local0.info "info_test_daze"

【すると出力確認コンソールに。。。】

[root@clash_royale ~]# tail -f /root/test/test_tool.log
Jun 26 21:54:11 clash_royale root: info_test_daze

無事出力できましたー。

エラーログも実施してみる。
【コマンド実行コンソール】

[root@clash_royale test]# logger -p local0.err "err_test_daze"

【出力確認コンソール】

[root@clash_royale ~]# tail -f /root/test/test_tool.log
Jun 26 21:54:11 clash_royale root: info_test_daze
Jun 26 22:00:06 clash_royale root: err_test_daze

これでログ出力の感覚がつかめたかと思います。

では簡単な(雑な)shellを作ってみましょう。

#!/bin/sh

a=1; b=3;

if [ -e kekka.txt ]; then
  echo $((a+b)) >> kekka.txt;
  logger -p local0.info "kekka.txtに実行結果を出力したよ"
else
  logger -p local0.err "kekka.txtがないよ"
  exit 10
fi

exit 0

kekka.txtファイルがある場合、4と追記し、なかった場合異常終了し、/root/test/test_tool.logに
kekka.txtがないよとログ出力するだけのshellです。こんな雑なshellで説明してすいません。

小話

とあるサーバーでrsyslog.confの、SYSLOG(/var/log/messages)への設定を行に*.infoが先頭に入っていました。
これにより、local0を作成すると、自動でSYSLOGへinfo情報以上すべて記録されてしまいます。

しかし、要件ではlocal0のERROR以上をSYSLOG出力へ出力したいんだよなー。って時がありました。
さあどうする。素直にlocal0.errをSYSLOGの行に追記してもinfo情報は記録されます。
(*.infoによりlocal0.infoも記録されているのです。)

以下で解決しました。

*.info;mail.none;authpriv.none;cron.none;local0.none;local0.err                /var/log/messages

そうです。一度noneで打ち消し、errを上書き!(笑)
個人的にはイケてない気がするので、*.infoとかっていう書き方好きじゃないなー。と思った次第です。

6
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
6
2