LoginSignup
1
0

More than 5 years have passed since last update.

[Symfony2 Monolog] Monologをごにょる

Posted at

あらまし

symfony2を使おうと思うんやけど、ログファイルが1つになってるのが気に食わんねんな。
dev.logとかprod.logとか。
なんの機能のログなんかわかったもんちゃうし、ファイルサイズでかなるし。
たとえばbundleごとに動的に出力先ファイルが切り替わるとか、そういうカスタムができる状態にできたらええなぁって。
と、そういうわけでね (´・ω・`)

どういう風に切り替えるねんって話は書いてへんからね。
そこは自分で練ってや、ボウヤちゃうんやし (´・ω・`)

classの配置

あとで出てくるserviceの設定でどうとでもできるので、ご自由にどうぞ。

  • 配置の例
    • XxxBundle/Logger/Formatter/CustomLineFormatter.php
    • XxxBundle/Logger/Handler/CustomStreamHandler.php

config.ymlの設定

/app/Resources/config/config.yml
monolog:
    handlers:
        main:
            type: service
            id: handler.stream.custom
  • ノーマルで使ってもしゃーないので、Monologハンドラをすべて削除する。
  • mainを"type=service"として記述する。
  • mainに対して利用するサービスIDを指定する。

service.ymlの設定

/app/Resources/config/service.yml
service:
    # ログフォーマッタ"line"
    formatter.line.custom:
        class: 'namespace\of\CustomLineFormatter'
        arguments: [ null, null, true, true ]
    # ログハンドラ"stream"
    handler.stream.custom:
        class: 'namespace\of\CustomStreamHandler'
        arguments: [ '%kernel.logs_dir%/#log_file#.log', 100, true, null ]
        calls:
            - method: setFormatter
              arguments: [ @=container.get('formatter.line.custom') ]
  • ストリームハンドラのサービスを登録する。
  • ストリームハンドラからログフォーマッタを利用するよう設定する。
  • ログフォーマッタの第3第4引数をtrueに固定しているのは、"[]"が邪魔なのと、見難いので改行して欲しいから。

ストリームハンドラ

XxxBundle/Logger/Handler/CustomStreamHandler.php
class CustomStreamHandler extends StreamHandler {
    CONST DEFAULT_LOG_FILE_NAME = 'default';

    public function __construct($stream, ) {
        parent::__construct($stream, );
        $this->replaceLogFileName();
    }

    private function replaceLogFileName() {
        $log_file_name = $this->generateLogFileName();
        // 出力対象ファイルを書き換える。
        $this->url = str_replace('#log_file#', $log_file_name, $this->url);
    }

    private function generateLogFileName() {
        // ごにょごにょしてファイル名を決定する。
        return $lig_file_name;
    }

    // ...
}

親クラスのコンストラクタの実装内容はこちら(↓)

StreamHandler.php
public function __construct($stream, ) {
    parent::__construct($level, $bubble);
    if (is_resource($stream)) {
        $this->stream = $stream;
    } elseif (is_string($stream)) {
        $this->url = $stream;
    } else {
        throw new \InvalidArgumentException('A stream must either be a resource or a string.');
    }

    $this->filePermission = $filePermission;
    $this->useLocking = $useLocking;
}
  • \$streamがリソースではない場合には、\$this->streamには反映せず、$this->urlに反映される。
    • ymlからpathが拾われた場合、そのルート("is_string()"側)に入る。
    • テキスト指定なので、ファイルパス書き換え対象は\$this->urlとなる。
  • まあ、本番環境やったらStreamHandlerじゃなくて、CrossedFingerHandler?の方がええやろかね。

ほんでもって

なんかよくわからんけど、対象ファイルの差し替えとしては一応機能するようになったものの、想定した以外のファイルが転がってることが結構あって、謎。
hoge.css.logとか。
(処理の詳細は伏せるが)何をしとるんだ (´・ω・`)
タイムスタンプを見る限りでは、何かしらのエラーがあったときとかっぽいけど。

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