2021-05-18

关于PHP日志LOG4PHP的配置与使用

本篇文章给大家介绍PHP日志LOG4PHP的配置与使用 。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

维护了 一个老项目, 没有日志功能, 就给加了这个log4php, 主要是集成进去很简单,使用起来也够用了。

1、下载log4php

2、创建配置文件

log4php_config.xml
<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>

3、LOG类

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

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

        switch ($name)
        {
            case 'error':
                $log->error($args);
                break;
            case 'info':
                $log->info($args);
                break;
            case 'warn':
                $log->warn($args);
                break;
            case 'debug':
                $log->debug($args);
                break;
            case 'fatal':
                $log->fatal($args);
                break;
            default:
                break;
        }
    }
}

4、使用【推荐学习:PHP视频教程

L::debug('日志要记录的内容');
L::info('日志要记录的内容');

以上就是关于PHP日志LOG4PHP的配置与使用的详细内容,更多请关注php中文网其它相关文章!

php中文网最新课程二维码

声明:本文转载于:cnblogs,如有侵犯,请联系admin@php.cn删除

  • 相关标签:PHP
  • https://www.php.cn/php-weizijiaocheng-476339.html

    发表回复

    Your email address will not be published. Required fields are marked *