Home > Backend Development > PHP Tutorial > About the configuration and use of PHP log LOG4PHP

About the configuration and use of PHP log LOG4PHP

藏色散人
Release: 2023-04-10 06:12:01
forward
4823 people have browsed it

This article introduces you to the configuration and use of PHP log LOG4PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

I maintain an old project without a logging function, so I added log4php. The main reason is that it is easy to integrate and it is sufficient to use.

1. Download log4php

2. Create configuration file

log4php_config.xml
Copy after login
<configuration xmlns="http://logging.apache.org/log4php/">
    <appender name="default" class="LoggerAppenderConsole" />

    <appender name="basic_file_appender" class="LoggerAppenderDailyFile">
        <layout class="LoggerLayoutPattern">
            <param name="conversionPattern" value="%date [%logger] %message%newline" />
        </layout>
        <param name="file" value="log/app.%s.log" /> // log文件夹下  以日期命名的log文件
        <param name="datePattern" value="Y-m-d" />
    </appender>
    <root>
        <level value="debug" />
        <appender_ref ref="basic_file_appender"/>
    </root>

    <appender name="route_file_appender" class="LoggerAppenderDailyFile">
        <layout class="LoggerLayoutPattern">
            <param name="conversionPattern" value="%date %p [%logger] %message%newline" />
        </layout>
        <param name="file" value="log/route.%s.log" />
        <param name="threshold" value="error"/>
        <param name="append" value="true" />
        <param name="datePattern" value="Y-m-d" />
    </appender>
    <logger name="Route">
        <level value="debug" />
        <appender_ref ref="route_file_appender" />
    </logger>

    <appender name="session_file_appender" class="LoggerAppenderDailyFile">
        <layout class="LoggerLayoutPattern">
            <param name="conversionPattern" value="%date [%logger] %message%newline" />
        </layout>
        <param name="file" value="log/session.%s.log" />
        <param name="datePattern" value="Y-m-d" />
    </appender>
    <logger name="Session">
        <level value="debug" />
        <appender_ref ref="session_file_appender" />
    </logger>

    <appender name="InventoryTransaction_file_appender" class="LoggerAppenderDailyFile">
        <layout class="LoggerLayoutPattern">
            <param name="conversionPattern" value="%date [%logger] %message%newline" />
        </layout>
        <param name="file" value="log/InventoryTransaction.%s.log" />
        <param name="datePattern" value="Y-m-d" />
    </appender>
    <logger name="InventoryTransaction">
        <level value="debug"/>
        <appender_ref ref="InventoryTransaction_file_appender" />
    </logger>

    <appender name="Reserve_file_appender" class="LoggerAppenderDailyFile">
        <layout class="LoggerLayoutPattern">
            <param name="conversionPattern" value="%date [%logger] %message%newline" />
        </layout>
        <param name="file" value="/data0/wmsinventoryapi/Reserve.%s.log" />
        <param name="datePattern" value="Y-m-d.H" />
    </appender>
    <logger name="Reserve">
        <level value="debug"/>
        <appender_ref ref="Reserve_file_appender" />
    </logger>

    <appender name="Exception_file_appender" class="LoggerAppenderDailyFile">
        <layout class="LoggerLayoutPattern">
            <param name="conversionPattern" value="%date [%logger] %message%newline" />
        </layout>
        <param name="file" value="log/exception.%s.log" />
        <param name="datePattern" value="Y-m-d" />
    </appender>
    <logger name="Exception">
        <level value="error"/>
        <appender_ref ref="Exception_file_appender" />
    </logger>

</configuration>
Copy after login

3. LOG class

<?php
/**
 * Created by PhpStorm.
 * User: an
 * Date: 2019/4/3
 * Time: 4:35 PM
 */
require_once(&#39;vendor/log4php/Logger.php&#39;);
Logger::configure(&#39;vendor/log4php/log4php_config.xml&#39;); // 引入第二步创建的xml配置文件

class L
{
    public static function __callStatic($name, $args)
    {
        $log = Logger::getLogger(&#39;app&#39;);

        switch ($name)
        {
            case &#39;error&#39;:
                $log->error($args);
                break;
            case &#39;info&#39;:
                $log->info($args);
                break;
            case &#39;warn&#39;:
                $log->warn($args);
                break;
            case &#39;debug&#39;:
                $log->debug($args);
                break;
            case &#39;fatal&#39;:
                $log->fatal($args);
                break;
            default:
                break;
        }
    }
}
Copy after login

4. Use [Recommended learning:PHP video tutorial

L::debug(&#39;日志要记录的内容&#39;);
L::info(&#39;日志要记录的内容&#39;);
Copy after login

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

Related labels:
php
source:cnblogs.com
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