LoginSignup
1
0

More than 5 years have passed since last update.

CakePHPでログの出力形式をカスタマイズしたい

Last updated at Posted at 2016-09-28

※「≈≈≈」は省略を表す

バージョン

$ cat lib/Cake/VERSION.txt
≈≈≈
2.4.5

CustomFileLog.phpの作成

$ cp -p lib/Cake/Log/Engine/FileLog.php app/Lib/Log/Engine/CustomFileLog.php
$ vi app/Lib/Log/Engine/CustomFileLog.php
  1. 既存のApp::usesを削除して、以下を追加

    App::uses('FileLog', 'Log/Engine');
    
  2. class FileLog extends BaseLog {」を削除して、以下を追加

    class CustomFileLog extends FileLog {
    
  3. public function write($type, $message)」以外の関数は全て削除

  4. public function write($type, $message)」内の「$output = date('Y-m-d H:i:s') . ' ' . ucfirst($type) . ': ' . $message . "\n";」を削除して、好きなフォーマットに変更
    例) シンプルにメッセージだけを出力したい場合

    $output = $message . "\n";
    
  5. bootstrap.phpを編集

    $ cat app/Config/bootstrap.php
    ≈≈≈
    /**
    * Configures default file logging options
    */
    App::uses('CakeLog', 'Log');
    App::uses('CustomFileLog', 'Log/Engine'); // 追加
    CakeLog::config('debug', array(
    'engine' => 'File',
    'types' => array('notice', 'info', 'debug'),
    'file' => 'debug',
    ));
    CakeLog::config('error', array(
    'engine' => 'File',
    'types' => array('warning', 'error', 'critical', 'alert', 'emergency'),
    'file' => 'error',
    ));
    // 今回は、custom.logだけ、出力形式がカスタマイズしたいので、以下を追加
    CakeLog::config('custom', array(
    'engine' => 'CustomFile', // ここでCustomFileLogクラスを見るように指定している
    'types' => array('custom'),
    'file' => 'custom'
    ));
    

config('const');
```

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