方法一:
use Monolog\Logger;
(new Logger('local'))
->pushHandler(new RotatingFileHandler(storage_path('logs/api.log')))
->info("test", ['data' => "zxc", 'error' => 'error']);
方法二:
Log::useFiles(storage_path('logs/api.log'), 'debug');
Log::debug("test --------------------------------");
方法三:
<?php
namespace App\Library;
use Illuminate\Log\Writer;
use Monolog\Logger;
/**
* 自定義日誌類實現
* Class BLogger
* @package App\Library
*/
class BLogger
{
// 所有的LOG都要求在這裡註冊
const LOG_ERROR = 'error';
const LOG_DEBUG = 'debug';
const LOG_INFO = 'info';
private static $loggers = [];
// 獲取一個例項
public static function getLogger($type = self::LOG_ERROR, $day = 30)
{
if (empty(self::$loggers[$type])) {
self::$loggers[$type] = new Writer(new Logger($type));
self::$loggers[$type]->useDailyFiles(storage_path() . '/logs/' . $type . '.log', $day);
}
$log = self::$loggers[$type];
return $log;
}
}
具體使用:
BLogger::getLogger(BLogger::LOG_ERROR)->error('test', ['test' => 'zxc']);
在 storage/logs/error_xxxx-xx-xx.log 檢視檔案內容
總結:
其實方法二和三基本相同。方法三做了個分類.
本作品採用《CC 協議》,轉載必須註明作者和本文連結