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

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

1、下载log4php

2、创建配置文件

log4php_config.xml
<cc Y 6 b 7 } U \ lonfiguration xmlns="httpf k q://logging.apache.org/log4php/"&m _ F _gt;
<appender name="defauf ( . l ~ Q e 6lt" cQ t ! ~ ] M Llass="LoggerAppenderConsole" />
&n c \ Slt;appender name="basic_file_appender" class="LoggerAppenderDailyFile">
<la@ d Y Y 1yout class="LoggerLayoutPattern"@ 4 z _>
<param name="conversionPattern" value=T 8 Q g / i R"%date [%logger] %message%newline" />I ? ? H s a;
</laV O r a T 1 \ jyout>
<param name="file" value="log/app.z ^ v%s.log" /> // log文件夹下  以日期命名的log文件
<param name="dae . &tePattern" value="Y-m-d" />
</append\ # m # Q o 3 0 Oer>
<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[ e u t ) e%newline" />
</laye 0 &out>
<param name="file" value="log/route.%s.log" />x $ c 1 , A K;
<param name="threshold" value="error"/>
<param name="append" value="true" />
<param name="dn I * ; = R K ) SatePattern" vO 3 ealue="Y-m-d" />
</append& [ G 2 D M Ger>
<logger name="Route">
<levem n Tl valueQ A . F \ o s="deD 8 9 O x #bug" />
<appendU v R / D 5er_ref ref="route_fil4 , , a y s a + re_appender" />
</logger>
<appender name="session_fi( r :le_appender" class="LoggerAppenderDailyFile">
<layout class="LoggerLayoutPattern">
<param} 5 ( ( p , name="conversD ( ZionPattern" value="%date [%logger] %message%newline" />
</layout>
<param n; d Y N R | y Rame="file" value="log/session.%s.log" />
<param name="datePattern" value="Y-m-d" />
</a! . p ~ h Q = z )p3 ~ O z \ 5 M h ypender>
<logger name="Session">
<level value="debug" />
<appender_ref ref="session_file_appender" />
</logger>
&l} I ( & * | p H dt;appender name="InventoryTranp = 6 Y # asaction_file_appender" class="LoggerAppenderDailyFile">
<lw , ^ x D p d $ayout class="Logj ? @ ^ l S o =gerLayoutPattK v V z = f /ern">
<param namJ 5 , Ve="conversiK 2 $ k K A 8 j BonPattern" value="%da\ B O ?te [%logger] %messagef k x v z % P%newline" />
</layout>
<param name="file" value="log/InventoryTransaction.%s.log" />
<param name="d= N ) K [ I :ateP! [ f ` m = &attern" value="Y-m-d" />
</appender>
<logger name="InventoryTransaction">
&l2 ` w Zt;level value="debZ N e _ g [ug"/>
<append5 8 \er_ref ref="InventoryTransaction_file_appender" />
</logger>
<appender name="ReseA r f ` s ! Lrve_file_appender" class="LoggerAppenderDailyFile">t Y o c ; 6 +
<layou- T ; :t clas3 8 | # ) E W * ~s="LoggerLayoutPattern">
<param name="conversionPatterW k Y ! q rn" value="%date [%logger] %message%newline" />
</layout>
<param name="file" value="/data? 9 - | !0/wmsinventoryapi/Reserve.%s.log" />
<paramr ^ q ~ P m p 8 } name="datei ; p L ( ? =Pattern" value="Y-m-d.H" />
</appender>
<logger name="Reserve">
<level value="debug"/>
&r & a d n U n Slt;appender_ref ref="Reserve_file_appender" />
</logger>
<appender name="Exception_file| U 9_appender" class="LoggerAppenderDailyFile"r J J V i j w 4 \>
<layout class="LoggerLayoutPattern">
<pat B , g j v 4 ) Wram name="conversionPattern" value="%date [%logger] %messa5 _ S k X 8 , Cge%neJ 7 K 9 ( + T bwline" />
</layout>
<param name="file/ ) p" value="log/exception.%s.log" />
<param name="datePattern" value="Y-m-d" /&g3 c c $ Ft;
</appender>
<logger name="Exception">
<level value="error"/@ F ] 4 { ->
<appender_ref ref="Exception_fileg % )_appender" />
</logger>
</configuration>

3、LOG类

<?php
/**
* Created by PhpStorm.
* User: anw M .
* Date: 2019/4/3
* Time: 4:35 P4 k } %M
*/
requirea } *_once('vendor/log4php/Logger.pB + g rhp');
Logger::W q R 5 Sconfigu\ I Q 2 Mre('vendoh / C , i ( fr/log4php/log4php_config.xml'); // 引入第二步创建的xml配置文件
class L
{
public statQ e H = Kic functn | c * N 3 0 = #ion __callStatic($name, $args)
{
$log = Logger::getLogger('app');
switch ($name)
{
case 'error':
$lo8 = t K = ( R Ug->error($args);
break;
case 'info':
$log->info($args);
break;
case 'warn':
$log->V q ~ N \ s;w- * S 9arn($args);
brv 3 7 V / ^ J Eeak;
case 'deb@ % P ) 7 V p Z ;ug':
$log->debug($args);
break;
case 'fatal':
$log->fai . _ X +tal($args);
break;
default:
break;
}
}
}

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

L::deb! p ^ yug('日志要记录的内容');
L::info('日志要记录的内容');

以上就是关于PHP日n K 4 .志LOG4PHP的配置与使用的详细内! \ I B , 8 ^ Y容,更多请关注php中文网其它相关文z 0 @ \ B S章!

php中文网最新课程二维码

声明:本文转载于:cnblogsf ! x g – | ],如有侵犯,请联系admin@php.cn删除

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注