Log
Webman uses monolog/monolog to handle logging.
Usage
<?php
namespace app\controller;
use support\Request;
use support\Log;
class FooController
{
public function index(Request $request)
{
Log::info('log test');
return response('hello index');
}
}
Available Methods
Log::log($level, $message, array $context = [])
Log::debug($message, array $context = [])
Log::info($message, array $context = [])
Log::notice($message, array $context = [])
Log::warning($message, array $context = [])
Log::error($message, array $context = [])
Log::critical($message, array $context = [])
Log::alert($message, array $context = [])
Log::emergency($message, array $context = [])
This is equivalent to
$log = Log::channel('default');
$log->log($level, $message, array $context = [])
$log->debug($message, array $context = [])
$log->info($message, array $context = [])
$log->notice($message, array $context = [])
$log->warning($message, array $context = [])
$log->error($message, array $context = [])
$log->critical($message, array $context = [])
$log->alert($message, array $context = [])
$log->emergency($message, array $context = [])
Configuration
return [
// Default log channel
'default' => [
// Handlers for the default channel, multiple can be set
'handlers' => [
[
// Name of the handler class
'class' => Monolog\Handler\RotatingFileHandler::class,
// Constructor parameters for the handler class
'constructor' => [
runtime_path() . '/logs/webman.log',
Monolog\Logger::DEBUG,
],
// Formatting related
'formatter' => [
// Name of the formatting class
'class' => Monolog\Formatter\LineFormatter::class,
// Constructor parameters for the formatting class
'constructor' => [ null, 'Y-m-d H:i:s', true],
],
]
],
],
];
Multiple Channels
Monolog supports multiple channels, with default
being used by default. If you want to add a log2
channel, the configuration is similar to the following:
return [
// Default log channel
'default' => [
// Handlers for the default channel, multiple can be set
'handlers' => [
[
// Name of the handler class
'class' => Monolog\Handler\RotatingFileHandler::class,
// Constructor parameters for the handler class
'constructor' => [
runtime_path() . '/logs/webman.log',
Monolog\Logger::DEBUG,
],
// Formatting related
'formatter' => [
// Name of the formatting class
'class' => Monolog\Formatter\LineFormatter::class,
// Constructor parameters for the formatting class
'constructor' => [ null, 'Y-m-d H:i:s', true],
],
]
],
],
// log2 channel
'log2' => [
// Handlers for the log2 channel, multiple can be set
'handlers' => [
[
// Name of the handler class
'class' => Monolog\Handler\RotatingFileHandler::class,
// Constructor parameters for the handler class
'constructor' => [
runtime_path() . '/logs/log2.log',
Monolog\Logger::DEBUG,
],
// Formatting related
'formatter' => [
// Name of the formatting class
'class' => Monolog\Formatter\LineFormatter::class,
// Constructor parameters for the formatting class
'constructor' => [ null, 'Y-m-d H:i:s', true],
],
]
],
],
];
When using the log2
channel, the code is as follows:
<?php
namespace app\controller;
use support\Request;
use support\Log;
class FooController
{
public function index(Request $request)
{
$log = Log::channel('log2');
$log->info('log2 test');
return response('hello index');
}
}