情シス - 雑多メモ

基本は情シス、たまに開発を担当

Codeigniter3 ログ出力設定

Codeigniter3系のログ出力設定の備忘です。

設定ファイル

application/config/config.php
204行目付近(初期値)

$config['log_threshold'] = 0;

$config['log_path'] = '';

$config['log_file_extension'] = '';

$config['log_file_permissions'] = 0644;

$config['log_date_format'] = 'Y-m-d H:i:s';
$config['log_threshold']

ログレベルを指定。初期値は 0 ロギングしない。

0 = Disables logging, Error logging TURNED OFF
1 = Error Messages (including PHP errors)
2 = Debug Messages
3 = Informational Messages
4 = All Messages
$config['log_path']

ログ出力PATHを指定。(例)application/log
出力先ディレクトリはApacheが write できる権限が必要。

$config['log_file_extension']

ログファイル名の拡張子(.log)指定。
指定しない場合はデフォルトの「.php」となる。違和感がある場合は「.log」に変更すればよい。

$config['log_file_permissions']

ログファイルのパーミッション値を指定。セキュリティ要件に合わせて適時変更。

$config['log_date_format']

ログ記録時の日時フォーマット
初期値の'Y-m-d H:i:s'を指定した場合、「2017-07-14 16:29:55」


ログの出力方法

PHPコード内で log_message($level, $message); を呼び出す。
levelは以下のいづれかを指定

error :エラーレベル
debug :デバッグレベル
info  :Infoレベル

デバッグ等でログ出力させたい場合の例

log_message('info', 'hogehogehoge_001');

ログ出力例
INFO - 2017-07-14 16:29:55 --> hogehogehoge_001


余談

php.iniでDefault_Timezoneが指定されていないとdate(date_default_timezone_set)呼び出しに失敗するので忘れずに。

Severity: Warning --> date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone.