首页 技术 正文
技术 2022年11月18日
0 收藏 765 点赞 2,786 浏览 3191 个字
首先pom文件如下
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.javacodegeeks.snippets.enterprise</groupId>
<artifactId>log4jexample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.0.2</version>
</dependency>
</dependencies>
</project>

然后是log4j2.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="OFF">
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss} [%t] %-5level %logger{36} - %msg%n" />
</Console><Properties>
<Property name="log-path">D:/软件/workspaceForWeb/Test</Property>
</Properties> <RollingFile name="WroxFileAppender" fileName="${log-path}/myexample.log" filePattern="${log-path}/myexample-%d{yyyy-MM-dd}-%i.log" >
<PatternLayout>
<pattern>%d{dd/MMM/yyyy HH:mm:ss,SSS}- %c{1}: %m%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="1 KB" />
</Policies>
<DefaultRolloverStrategy max="4"/>
</RollingFile> </appenders> <Loggers>
<Root level="trace">
<AppenderRef ref="Console" />
</Root>
<logger name="com.wrox" levels="info" additivity="false">
<appender-ref ref="RollingFile"/>
<appender-ref ref="Console">
<!-- 下变是个filter,说明了 可以logger to console appender,but only for event containing a Marker named EROX_CONSOLE-->
<MarkerFilter marker="WORK_CONSOLE" onMathc="NEUTRAL" onMIsmath="DENY"/>
</appender-ref>
</logger>
<Logger name="yourLogger" level="debug" additivity="false">
<AppenderRef ref="Console" />
</Logger>
</Loggers></configuration>

需要说明几点:

0、首先注意 configuration  的status–logging system本身的message,

记录日志系统本身的log,叫做StatusLogger,目的是log events occurring within eh logging system itself,比如说你建立了一个socket Appender然后目的网路不可到达这个日志就记录再次,利用StatusLogger,默认设置是OFF,也就是suppresses all the logging system message。

1、首先在appender中 配置roll the log—RollingFile 

roll log发生的时机是

the log reaching a certain size; 比如这里10m

the data changing;

the supplication starting;

3者中的任意组合

2、logger中的markerfiler:

下变是个filter,说明了 可以logger to console appender,but only for event containing a Marker named EROX_CONSOLE

3、注意日志格式:PatternLayout

%l prints the class,method,file and line number where the logging message occurred

4、关于logger继承关系

4.1、默认子logger是继承父亲的

if the root logger is assigned to  a console Appender and

com.wrox is assigned to file Appender,

log messages written to com.wrox,com.wrox.chat and others go to both the console and the file,

whereas log messages written to root or com.example.test go only to the console.

4.2、可以关掉这个继承利用additivity,表示该节点以及子节点不会在继承该节点的的父亲节点了

<logger name="com.wrox" levels="info" additivity="false">
<appender-ref ref="WroxFileAppender"/>
<appender-ref ref="Console">
</appender-ref>
</logger>
注意属性 additivity,表示是否继承父亲属性com.wrox的属性additivity=false,那么com.wrox,com.wrox.chat,and others go only to the file,表示父继承关闭了4.3、如果

root logger console appender &&

(com.wrox 的additivity=false &&分配给to file Appender) &&

( com.wrox.shop的属性 additivity=true && has a syslog appender),

那么消息 written to com.wrox.shop 会去syslog and the file,不会到console

5、关于java中使用

private static final Logger log=LogManager.getLogger();

log.error(“”);

 

    					
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,497
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,910
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,744
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,498
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,136
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,300