Home  >  Article  >  Backend Development  >  About Laravel log usage

About Laravel log usage

不言
不言Original
2018-06-13 15:26:152318browse

This article mainly introduces the usage of Laravel logs, and analyzes the functions, definitions, usage methods and related precautions of Laravel logs in detail in the form of examples. Friends in need can refer to the following

Examples of this article Laravel log usage. 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 log 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 = &#39;configure&#39;.ucfirst($app[&#39;config&#39;][&#39;app.log&#39;]).&#39;Handler&#39;;
    $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().&#39;/logs/laravel.log&#39;, #存储文件
       $app->make(&#39;config&#39;)->get(&#39;app.log_level&#39;, &#39;debug&#39;) #存储级别
     );
}

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 = &#39;debug&#39;) #单一文件
public function useDailyFiles($path, $days = 0, $level = &#39;debug&#39;) #每日生成
public function useSyslog($name = &#39;laravel&#39;, $level = &#39;debug&#39;) #系统日志的方式
public function useErrorLog($level = &#39;debug&#39;, $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);

The above is the entire content of this article. I hope it will be helpful to everyone’s study. Please pay attention to more related content. PHP Chinese website!

Related recommendations:

About the use of cookies in Laravel5

Learn several usages of laravel’s model events

The above is the detailed content of About Laravel log usage. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn