LoginSignup
1
1

More than 5 years have passed since last update.

Lumen でログファイルをレベルと日付ごとに分けて出力する

Last updated at Posted at 2018-04-07

Lumen でログファイルをレベル毎、日付毎にわけることができないかといろいろと調べました。

結果、以下の対応が簡単でお勧めです。
 環境:Lumen (5.6.3)

bootstrap\app.php
use Monolog\Logger;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Formatter\LineFormatter;
    :
  途中省略
    :
$handlers = [];
$handlers[] = (new RotatingFileHandler(storage_path("logs/critical.log"), 30, Logger::CRITICAL, false))->setFormatter(new LineFormatter(null, null, true, true));
$handlers[] = (new RotatingFileHandler(storage_path("logs/error.log"), 30, Logger::ERROR, false))->setFormatter(new LineFormatter(null, null, true, true));
$handlers[] = (new RotatingFileHandler(storage_path("logs/warning.log"), 30, Logger::WARNING, false))->setFormatter(new LineFormatter(null, null, true, true));
$handlers[] = (new RotatingFileHandler(storage_path("logs/info.log"), 30, Logger::INFO, false))->setFormatter(new LineFormatter(null, null, true, true));

app('Psr\Log\LoggerInterface')->setHandlers($handlers);

return $app;

setFormatter() で日付が入るように指定しています。
RotatingFileHandler() を使い、Logger::XXXX のレベルのログに対して、ログファイルのパスと最大ファイル数を指定しています。
RotatingFileHandler で最大ファイル数を指定することで、古いログは削除されるため、ログが残りすぎる心配がありません。
便利ですね。
 
 
ぐぐると他にもいろいろと方法は出てきます。
多いのが $app->configureMonologUsing() を使ってログ設定を定義するというものでしたが、どうもこの関数はLumenの最新版では削除されているようで、「未定義の関数を呼んでるぞ!」と怒られますので気を付けましょう。


最近、サーバーサイドを始めました。
REST API を作るのに便利なフレームワークはないか探したところ、Lumen が軽量でよいと聞き、勉強しているところです。

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