Home >PHP Framework >ThinkPHP >thinkphp logging configuration tutorial

thinkphp logging configuration tutorial

L
Lforward
2020-06-01 17:15:277553browse

thinkphp logging configuration tutorial

thinkphp logging

The processing of logs is automatically performed by the system. When logging is turned on, Record all log information for the allowed log levels.

For performance reasons, the SQL log level must be valid when debugging mode is turned on, otherwise it will not be recorded. System logging is completed by the core Think\Log class and its driver, which provides multiple ways to record different levels of log information.

By default, logging is only recorded in debug mode. To enable logging in deployment mode, the LOG_RECORD parameter must be enabled in the configuration, and the logs that need to be recorded can be configured in the application configuration file. Level, for example:

  1. <span class="str">'LOG_RECORD'<span class="pln"> <span class="pun">=><span class="pln"> <span class="kwd">true<span class="pun">,<span class="pln"> <span class="com">// Turn on logging</span></span></span></span></span></span></span></span>
  2. #'LOG_LEVEL'<span class="str"> <span class="pln">=><span class="pun">'EMERG,ALERT,CRIT,ERR'<span class="str">,<span class="pun"> <span class="pln">//Only record EMERG ALERT CRIT ERR errors <span class="com"></span></span></span></span> </span></span></span>
Log level

ThinkPHP classifies system logs according to levels, including:

  • EMERG Serious error, causing system crash and unavailability
  • ALERT Alert error, error that must be corrected immediately
  • CRIT Critical value Error, error exceeding the critical value
  • ERR General error
  • WARN Warning error, error requiring warning
  • NOTICE Notification, the program can run but it is not perfect error
  • INFO Information, the program output information
  • DEBUG Debugging, used for debugging information
  • SQL SQL statement, this level is only valid when debugging mode is turned on
Recording method

Log The default recording mode is file mode, which can be expanded to support more recording modes through the driver.

The recording method is configured by the LOG_TYPE parameter, for example:

  1. 'LOG_TYPE'<span class="str"> <span class="pln">=><span class="pun"> <span class="pln">'File '<span class="str">,<span class="pun"> <span class="pln">//The default logging type is file mode<span class="com"></span></span></span></span></span></span></span></span>
##File mode record, the corresponding driver file is located in the system's
Library/Think/Log/Driver/File.class.php

.

Manual recording

Generally, the system's logging is automatic and manual recording is not required. However, sometimes it is also necessary to manually record log information. The Log class provides 3 Method for logging.

Method##Log::record()Record log information to memoryLog::save()Write the log information saved in memory (using the specified recording method)Log::write()Write a log message in real time

Since the system will automatically call the Log::save method after the request is completed, usually, you only need to call Log::record to record the log information.

The usage of record method is as follows:

  1. ##\Think\Log<span class="pln">::<span class="pun">record<span class="pln">(<span class="pun">'Test log information '<span class="str">);<span class="pun"></span></span></span></span></span></span>
The default log level is ERR, you can also specify the log level :

  1. \Think\Log<span class="pln">::<span class="pun">record<span class="pln">(<span class="pun">'Test log information, this is the warning level'<span class="str">, <span class="pun">'WARN'<span class="str">);<span class="pun"></span></span></span></span></span></span></span></span>
  2. ##record method only Information about the log level allowed by the current configuration will be recorded. If the application configuration is:

  1. 'LOG_LEVEL' <span class="str">=><span class="pln">'EMERG, ALERT,CRIT,ERR'<span class="pun">,<span class="str"> <span class="pun">//Only record EMERG ALERT CRIT ERR errors<span class="pln"><span class="com"></span></span></span></span></span></span> </span>## Then the log information recorded by the above record method will be filtered directly, or you can force recording:

    \Think\Log
  1. ::record<span class="pln">(<span class="pun">'Test log information, this is the warning level'<span class="pln">,<span class="pun">'WARN'<span class="str">,<span class="pun">true<span class="str">);<span class="pun"><span class="kwd"><span class="pun"></span></span></span></span></span></span></span></span></span></span>##The log information recorded using the record method is not saved in real time. If you need to record in real time, you can use the write method, for example:

##\Think\Log

::
    write
  1. ('Test Log information, this is the warning level, and is written in real time '<span class="pln">,<span class="pun">'WARN'<span class="pln">);<span class="pun"><span class="str"><span class="pun"><span class="str"><span class="pun"></span></span> </span></span></span></span></span>The write method is not affected by the configured allowed log level when writing logs, and can write any level of log information in real time. </span>Recommended tutorial: "
  2. TP5
"

Description

The above is the detailed content of thinkphp logging configuration tutorial. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete