Laravel 7 - 如何禁用日志记录?
Laravel 7 - How to disable logging?
我正在使用 Laravel 7.x 并且我想编写自己的日志。我已经配置 config/logging.php 并实现了写入我的日志条目,但是 Laravel 也在我的日志文件中写入了自己的日志条目。
如何禁用自动 Laravel 日志条目并保留我自己的日志记录?
提前致谢。
您可以尝试针对您的问题给出的解决方案。
请在您的控制器或 class 文件中包含以下命名空间。
use Carbon\Carbon;
use Illuminate\Support\Facades\File;
现在您可以创建日志目录和文件来编写您的日志。
$logPath = storage_path() . '/custom_logs/';
$log = $logPath . 'custom-file-log-'.Carbon::today()->format('d-m-Y') . '.log';
if (!File::exists($logPath)) {
File::makeDirectory($logPath, $mode = 0777, true, true);
}
现在将错误或客户信息写入日志文件。
您可以在日志文件中记录短信
$message = "It is first custom log";
File::append($log, Carbon::now()->format('Y-m-d H:i:s')." -- ".$message . PHP_EOL);
您可以在日志文件中记录数组。
$message = ['A'=>'B', 'C'=>'D'];
File::append($log, Carbon::now()->format('Y-m-d H:i:s')." -- ".print_r($message,true). PHP_EOL);
谢谢!!
您可以创建自己的日志频道。编辑您的 config/logging.php
:
....
'channels' => [
....
'your-channel-name' => [
'driver' => 'daily',
'path' => storage_path('logs/your-channel-log-file-name.log'),
'level' => 'debug'
],
],
....
之后你可以这样记录到 your-channel-log-file-name.log
文件:
\Log::channel('your-channel-name')->debug('This is my debug message');
这就像使用 logger()
函数。
我正在使用 Laravel 7.x 并且我想编写自己的日志。我已经配置 config/logging.php 并实现了写入我的日志条目,但是 Laravel 也在我的日志文件中写入了自己的日志条目。
如何禁用自动 Laravel 日志条目并保留我自己的日志记录?
提前致谢。
您可以尝试针对您的问题给出的解决方案。
请在您的控制器或 class 文件中包含以下命名空间。
use Carbon\Carbon;
use Illuminate\Support\Facades\File;
现在您可以创建日志目录和文件来编写您的日志。
$logPath = storage_path() . '/custom_logs/';
$log = $logPath . 'custom-file-log-'.Carbon::today()->format('d-m-Y') . '.log';
if (!File::exists($logPath)) {
File::makeDirectory($logPath, $mode = 0777, true, true);
}
现在将错误或客户信息写入日志文件。
您可以在日志文件中记录短信
$message = "It is first custom log";
File::append($log, Carbon::now()->format('Y-m-d H:i:s')." -- ".$message . PHP_EOL);
您可以在日志文件中记录数组。
$message = ['A'=>'B', 'C'=>'D'];
File::append($log, Carbon::now()->format('Y-m-d H:i:s')." -- ".print_r($message,true). PHP_EOL);
谢谢!!
您可以创建自己的日志频道。编辑您的 config/logging.php
:
....
'channels' => [
....
'your-channel-name' => [
'driver' => 'daily',
'path' => storage_path('logs/your-channel-log-file-name.log'),
'level' => 'debug'
],
],
....
之后你可以这样记录到 your-channel-log-file-name.log
文件:
\Log::channel('your-channel-name')->debug('This is my debug message');
这就像使用 logger()
函数。