基于Lumen-重写日志方法

Lumen 中默认的文件名是被硬编码在 Application 类中了的,要实现这样的效果,必须重写 Laravel\Lumen\Application@getMonologHandler() 方法。

  • 源文件 /bootstrap/app.php
1
2
3
$app = new Laravel\Lumen\Application(
realpath(__DIR__.'/../')
);
  • 修改后 /bootstrap/app.php
1
2
3
$app = new App\Application(
realpath(__DIR__.'/../')
);
  • 新增文件: app/Application.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php

// Custom default or hard-coded behavior of lumen
// @caoxl

namespace App;

use Laravel\Lumen\Application as LumenBase;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;

class Application extends LumenBase
{
// Rewrite log handler
protected function getMonologHandler()
{
return (
new StreamHandler(storage_path(env(
'APP_LOG_PATH',
'logs/'.date('Y-m-d').'.log'
)),
Logger::DEBUG
))
->setFormatter(new LineFormatter(null, null, true, true));
}
}

/storage/logs/即可看到以类似 2018-08-07.log命名的日志文件

Powered by Hexo and Hexo-theme-hiker

Copyright © 2017 - 2023 Keep It Simple And Stupid All Rights Reserved.

访客数 : | 访问量 :