Detailed description of Laravel log usage and laravel log usage

Source: Internet
Author: User

Detailed description of Laravel log usage and laravel log usage

This example describes how to use Laravel logs. We will share this with you for your reference. The details are as follows:

The Laravel version is still 5.2.

Logs are very important. Debugging mode can be enabled for local development, but viewing logs for online projects is a simple and effective debugging method. Laravel integrates the Monolog logstore to provide a variety of powerful log processors.

Laravel supports the single, daily, syslog, and errorlog log methods. For example, if you want to generate a log file by day instead of generating a single file, set the log value in config/app. php as follows:

'log' => 'daily'

The default value is single.

#config/app.php:111'log' => env('APP_LOG', 'single'),

Next, let's take a look at how Laravel configures logs.

#vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:36protected $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',];<?phpnamespace 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 the Monolog configuration is customized, The if condition is used. else is used by default.

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', # store the file $ app-> make ('config')-> get ('app. log_level ', 'debug') # Storage Level );}

Here, useFiles registers the signle file log handler and sets the storage file and storage level.

The following are four log processing registration methods for log initialization.

Public function useFiles ($ path, $ level = 'debug') # single file public function useDailyFiles ($ path, $ days = 0, $ level = 'debug ') # generate public function useSyslog ($ name = 'laravel ', $ level = 'debug') on a daily basis # public function useErrorLog ($ level = 'debug ', $ messageType = ErrorLogHandler: OPERATING_SYSTEM) # equivalent to the error_log method of php

The log initialization information is basically the above.

You can use the Log facade to write Log information to 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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.