Home > Backend Development > PHP Tutorial > About Laravel log usage

About Laravel log usage

不言
Release: 2023-04-01 06:06:01
Original
2397 people have browsed it

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'
Copy after login

The system default configuration is single

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

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);
  }
}
Copy after login

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;) #存储级别
     );
}
Copy after login

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方式
Copy after login

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);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template