The examples in this article describe the usage of Laravel logs. Share it with everyone for your reference, the details are as follows:
The Laravel version used here is still 5.2
The log is very important. Debug mode can be turned on for local development, but viewing logs for online projects is a very simple and effective way to debug. Laravel integrates the Monolog logging library to provide a variety of powerful log processors.
Laravel supports logging methods single, daily, syslog and errorlog. For example, if you want log files to be generated on a daily basis instead of generating a single file, you should set the log value in the configuration file config/app.php as follows:
'log' => 'daily'
The system default configuration is single
#config/app.php:111 'log' => env('APP_LOG', 'single'),
Let’s take a look at how Laravel configures logs.
#vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:36 protected $bootstrappers = [ 'Illuminate\Foundation\Bootstrap\DetectEnvironment', 'Illuminate\Foundation\Bootstrap\LoadConfiguration', 'Illuminate\Foundation\Bootstrap\ConfigureLogging', 'Illuminate\Foundation\Bootstrap\HandleExceptions', 'Illuminate\Foundation\Bootstrap\RegisterFacades', 'Illuminate\Foundation\Bootstrap\RegisterProviders', 'Illuminate\Foundation\Bootstrap\BootProviders', ]; <?php namespace Illuminate\Foundation\Bootstrap; use Illuminate\Log\Writer; use Monolog\Logger as Monolog; use Illuminate\Contracts\Foundation\Application; class ConfigureLogging { /** * Bootstrap the given application. * * @param \Illuminate\Contracts\Foundation\Application $app * @return void */ public function bootstrap(Application $app) { $log = $this->registerLogger($app); // If a custom Monolog configurator has been registered for the application // we will call that, passing Monolog along. Otherwise, we will grab the // the configurations for the log system and use it for configuration. if ($app->hasMonologConfigurator()) { call_user_func( $app->getMonologConfigurator(), $log->getMonolog() ); } else { $this->configureHandlers($app, $log); } }
If you customize the Monolog configuration, use the if condition, and the default is else
protected function configureHandlers(Application $app, Writer $log) { $method = 'configure'.ucfirst($app['config']['app.log']).'Handler'; $this->{$method}($app, $log); } /** * Configure the Monolog handlers for the application. * * @param \Illuminate\Contracts\Foundation\Application $app * @param \Illuminate\Log\Writer $log * @return void */ protected function configureSingleHandler(Application $app, Writer $log) { $log->useFiles( $app->storagePath().'/logs/laravel.log', #存储文件 $app->make('config')->get('app.log_level', 'debug') #存储级别 ); }
The useFiles method here is to register the signle file log handler and set the storage file and storage level.
The following are four log processing registration methods when initializing logs.
public function useFiles($path, $level = 'debug') #单一文件 public function useDailyFiles($path, $days = 0, $level = 'debug') #每日生成 public function useSyslog($name = 'laravel', $level = 'debug') #系统日志的方式 public function useErrorLog($level = 'debug', $messageType = ErrorLogHandler::OPERATING_SYSTEM) #等同于php的error_log方式
The log initialization information is basically the above.
You can use the Log facade to write log information into the log:
Eight log levels: emergency, alert, critical, error, warning, notice, info and debug.
Log::emergency($error); Log::alert($error); Log::critical($error); Log::error($error); Log::warning($error); Log::notice($error); Log::info($error); Log::debug($error);
I hope this article will be helpful to everyone’s PHP program design based on the Laravel framework.
For more detailed explanations of Laravel log usage and related articles, please pay attention to the PHP Chinese website!