FreeBSD 11.1 RELEASE - apache24 - postgreSQL - ログの設定

クラウディア 
1. 概要
2. /var/db/postgres/data96/postgresql.conf の設定
3. フォルダとファイルの作成
4. 出力の確認

1. 概要

 「Let's Postgres ログ関連の設定」に有用なことが書いてありますので、参考にちょっとやってみます。  バージョン 8.4 くらいからだったでしょうか。  PortgreSQL の起動直後に

LOG:  ending log output to stderr
HINT:  Future log output will go to log destination "syslog".
 というメッセージが出力されるようになりまして。ERROR や WARNING ではないのですがちょっと気になっていました。  英語文盲なのもので「よくわからんなぁ」と思っていましたが、まさにログに関するメッセージだったのですね。

2. /var/db/postgres/data96/postgresql.conf の設定

 PortgreSQL のログは、だいぶ使いやすくなっているようで、

/var/db/postgres/data96/postgresql.conf
 で出力先や動作を指定することができます。  コンフィグレーションファイルの編集の際は気を付けて、所有者が postgres:postgres であることを確認します。  root ユーザで編集して所有者が root:wheel になったりすると PortgreSQL 起動時に読み込めなくて、えらいことになります。  ログの指定方法は、バージョンによって違いがあるようなので、詳細は「Let's Postgres ログ関連の設定」を参考にしてください。  わたしは 9.6 で確認しています。

/var/log/postgresql/postgresql.log
 というファイルに出力させ、1日ずつローテーションさせることとしますと

log_destination = 'syslog'
#log_destination = 'stderr'              # Valid values are combinations of
                                        # stderr, csvlog, syslog, and eventlog,
                                        # depending on platform.  csvlog
                                        # requires logging_collector to be on.

# This is used when logging to stderr:
#logging_collector = off                # Enable capturing of stderr and csvlog
                                        # into log files. Required to be on for
                                        # csvlogs.
                                        # (change requires restart)

# These are only used if logging_collector is on:
#log_directory = 'pg_log'               # directory where log files are written,
                                        # can be absolute or relative to PGDATA
#log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'        # log file name pattern,
                                        # can include strftime() escapes
#log_file_mode = 0600                   # creation mode for log files,
                                        # begin with 0 to use octal notation
#log_truncate_on_rotation = off         # If on, an existing log file with the
                                        # same name as the new log file will be
                                        # truncated rather than appended to.
                                        # But such truncation only occurs on
                                        # time-driven rotation, not on restarts
                                        # or size-driven rotation.  Default is
                                        # off, meaning append to existing files
                                        # in all cases.
#log_rotation_age = 1d                  # Automatic rotation of logfiles will
                                        # happen after that time.  0 disables.
#log_rotation_size = 10MB               # Automatic rotation of logfiles will
                                        # happen after that much log output.
 を

#log_destination = 'syslog'															←	コメント化
log_destination = 'stderr'              # Valid values are combinations of			←	コメント解除
                                        # stderr, csvlog, syslog, and eventlog,
                                        # depending on platform.  csvlog
                                        # requires logging_collector to be on.

# This is used when logging to stderr:
logging_collector = on                  # Enable capturing of stderr and csvlog		←	コメント解除して on
                                        # into log files. Required to be on for
                                        # csvlogs.
                                        # (change requires restart)

# These are only used if logging_collector is on:
log_directory = '/var/log/postgresql'   # directory where log files are written,	←	コメント解除して変更
                                        # can be absolute or relative to PGDATA
log_filename = 'postgresql.log'			# log file name pattern,					←	コメント解除して変更
                                        # can include strftime() escapes
#log_file_mode = 0600                   # creation mode for log files,
                                        # begin with 0 to use octal notation
#log_truncate_on_rotation = off         # If on, an existing log file with the
                                        # same name as the new log file will be
                                        # truncated rather than appended to.
                                        # But such truncation only occurs on
                                        # time-driven rotation, not on restarts
                                        # or size-driven rotation.  Default is
                                        # off, meaning append to existing files
                                        # in all cases.
log_rotation_age = 1d                  	# Automatic rotation of logfiles will		←	コメント解除
                                        # happen after that time.  0 disables.
#log_rotation_size = 10MB               # Automatic rotation of logfiles will
                                        # happen after that much log output.
 1日ごとローテーションは、具合をみて書き換えるかも・・・。  ローテーションは newsyslog.conf に書いてそちらで制御させようかとも思ったのですが、ファイルの所有権の問題がややこしそうなので、当面こちらで・・・。  頭に出力時刻だけつけたいので

#log_error_verbosity = default          # terse, default, or verbose messages
#log_hostname = off
#log_line_prefix = ''                   # special values:
                                        #   %a = application name
 を下記のように変更します。

#log_error_verbosity = default          # terse, default, or verbose messages
#log_hostname = off
log_line_prefix = '%t '                 # special values:
                                        #   %a = application name
 後ろにセパレータの空白か何かをいれないと後続のメッセージとくっついて出力されちゃいます。

3. フォルダとファイルの作成

 初回出力用にフォルダとファイルを作成します。

mkdir /var/log/postgresql
touch /var/log/postgresql/postgresql.log
chown -R postgres:postgres /var/log/postgresql

4. 出力の確認

 PortgreSQL を再起動して、ログを確認します。

> service postgresql restart
2016-11-12 15:22:48 JST LOG:  redirecting log output to logging collector process
2016-11-12 15:22:48 JST HINT:  Future log output will appear in directory "/var/log/postgresql".
 をを、メッセージも変わったじゃないの。  ログの中身を確認してみますと

2016-11-12 15:22:48 JST LOG:  MultiXact member wraparound protections are now enabled
2016-11-12 15:22:48 JST LOG:  database system is ready to accept connections
2016-11-12 15:22:48 JST LOG:  autovacuum launcher started
 となっています。
earthcar(アースカー)