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?

rsyslogのtemplateの情報が少なかったので

Last updated at Posted at 2025-05-15

rsyslog の template(テンプレート)とは?

rsyslog のテンプレートとは、ログメッセージの出力形式出力先パスをカスタマイズするための定義です。

テンプレートを使うことで、以下のような柔軟なログ設計が可能になります。

  • ホスト名やファシリティ別など、動的なパス構成
  • 特定プロパティのログを分離出力
  • JSON形式など独自のログ書式出力

一例:

以下のテンプレートを使用すると、

  • /var/log/[ホスト名]/ ディレクトリに
  • [ファシリティ].log というファイル名で
  • 各ファシリティのログがそれぞれ記録されます。
/etc/rsyslog.conf
$template MyOutputFile,"/var/log/%hostname%/%syslogfacility-text%.log"

*.* ?MyOutputFile
作成されるログ
[root@dns /]# tail -n2 /var/log/`hostname -s`/*
==> /var/log/dns/authpriv.log <==
May 15 22:26:25 dns usermod[1840]: add 'postfix' to group 'mail'
May 15 22:26:25 dns usermod[1840]: add 'postfix' to shadow group 'mail'

==> /var/log/dns/cron.log <==
May 15 22:21:44 dns crond[1681]: (CRON) INFO (running with inotify support)
May 15 22:21:44 dns crond[1681]: (CRON) INFO (@reboot jobs will be run at computer's startup.)

==> /var/log/dns/daemon.log <==
May 15 22:27:23 dns systemd[1]: man-db-cache-update.service: Consumed 36.476s CPU time.
May 15 22:27:23 dns systemd[1]: run-r059dd2ae6e984c0c9b3d7bb07c51e2d8.service: Deactivated successfully.

==> /var/log/dns/mail.log <==
May 15 22:26:43 dns postfix/postfix-script[13548]: stopping the Postfix mail system
May 15 22:26:43 dns postfix/master[10900]: terminating on signal 15

==> /var/log/dns/syslog.log <==
May 15 21:44:54 dns rsyslogd[1627]: [origin software="rsyslogd" swVersion="8.2310.0-4.el9" x-pid="1627" x-info="https://www.rsyslog.com"] start
May 15 21:44:54 dns rsyslogd[1627]: imjournal: journal files changed, reloading...  [v8.2310.0-4.el9 try https://www.rsyslog.com/e/0 ]

templateのルール

書式
$template テンプレート名,"出力先パスまたはメッセージ書式"
  • テンプレート内の変数は % で囲む(例:%hostname%)。
  • 出力定義でテンプレートを使うときは ?テンプレート名 と書く。

templateで"-"使用の注意

rsyslogにおける "-"(ハイフン)の意味

rsyslog の出力定義で使う -非同期書き込みを意味します。

  • ログを一時的にバッファに溜め、まとめてディスクに書き込むことで性能向上が期待できます。

templateで使用上の注意:

  • - はテンプレートの定義行には書けません。
  • - はテンプレート名の前、?テンプレート名? の前に記述します。
/etc/rsyslog.conf
$template MyOutputFile,"/var/log/%hostname%/%syslogfacility-text%.log"

*.* -?MyOutputFile

※最も伝えたかったのは、この -?テンプレート名 の構文です。

情報があまり出回っておらず、苦労しました。


補足

  • %syslogfacility-text% などの変数は、rsyslogの公式ドキュメントに一覧があります。
  • JSON形式などの複雑な出力も $template を使えば可能です。
  • /var/log 以下を用途に応じて細かく整理したい場合、テンプレートを使いこなすと非常に便利です。

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?