欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

apche的log4j.properties和log4j2.xml的配置和使用

程序员文章站 2022-05-24 15:42:35
...

日志是应用软件中不可缺少的部分,Apache的开源项目log4j是一个功能强大的日志组件,提供方便的日志记录。

apache下载log4j:https://logging.apache.org/log4j/2.x/download.html

一、导入log4j的包:

1)下载jar包放到lib中。

apche的log4j.properties和log4j2.xml的配置和使用

 

apche的log4j.properties和log4j2.xml的配置和使用

2)maven下载依赖:

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.11.0</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.11.0</version>
</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.12</version>
		</dependency>

 

二、编写配置文件:

 

1)配置log4j.properties

### 设置###
log4j.rootLogger = debug,stdout,D,E

### 输出信息到控制抬 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n

### 输出DEBUG 级别以上的日志到=G://logs/error.log ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = G://logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG 
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %l] - [ %p ]  %m%n

### 输出ERROR 级别以上的日志到=G://logs/error.log ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =G://logs/error.log 
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR 
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [%l] - [ %p ]  %m%n

1)配置log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN" monitorInterval="30">
	<!--定义所有的appender -->
	<appenders>
		<!--这个输出控制台的配置 -->
		<console name="Console" target="SYSTEM_OUT">
			<!--输出日志的格式 -->
			<PatternLayout
				pattern="[%d{HH:mm:ss:SSS}] [%p] - %l -%n%m%n" />
		</console>
		<!-- 这个会打印出所有的info及以下级别的信息,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档 -->
		<RollingFile name="RollingFileInfo"
			fileName="G://log2/info.log"
			filePattern="G://log2/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log">
			<!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch) -->
			<ThresholdFilter level="info" onMatch="ACCEPT"
				onMismatch="DENY" />
			<PatternLayout
				pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n" />
			<Policies>
				<TimeBasedTriggeringPolicy />
				<SizeBasedTriggeringPolicy size="10MB" />
			</Policies>
		</RollingFile>
		<RollingFile name="RollingFileWarn"
			fileName="G://log2/warn.log"
			filePattern="G://log2/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log">
			<ThresholdFilter level="warn" onMatch="ACCEPT"
				onMismatch="DENY" />
			<PatternLayout
				pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n" />
			<Policies>
				<TimeBasedTriggeringPolicy />
				<SizeBasedTriggeringPolicy size="10MB" />
			</Policies>
			<!-- DefaultRolloverStrategy属性如不设置,则默认为最多同一文件夹下7个文件,这里设置了10 -->
			<DefaultRolloverStrategy max="10" />
		</RollingFile>
		<RollingFile name="RollingFileError"
			fileName="G://log2/error.log"
			filePattern="G://log2/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log">
			<ThresholdFilter level="error" onMatch="ACCEPT"
				onMismatch="DENY" />
			<PatternLayout
				pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n" />
			<Policies>
				<TimeBasedTriggeringPolicy />
				<SizeBasedTriggeringPolicy size="10MB" />
			</Policies>
		</RollingFile>
	</appenders>
	<!--然后定义logger,只有定义了logger并引入的appender,appender才会生效 -->
	<loggers>
		<!--过滤掉spring和mybatis的一些无用的DEBUG信息 -->
         <logger name="org.springframework" level="INFO"></logger>
         <logger name="org.mybatis" level="INFO"></logger>
		<root level="all">
			<appender-ref ref="Console" />
			<appender-ref ref="RollingFileInfo" />
			<appender-ref ref="RollingFileWarn" />
			<appender-ref ref="RollingFileError" />
		</root>
	</loggers>
</configuration>
	<!--定义所有的appender -->
	<appenders>
		<!--这个输出控制台的配置 -->
		<console name="Console" target="SYSTEM_OUT">
			<!--输出日志的格式 -->
			<PatternLayout
				pattern="[%d{HH:mm:ss:SSS}] [%p] - %l -%n%m%n" />
		</console>
		<!-- 这个会打印出所有的info及以下级别的信息,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档 -->
		<RollingFile name="RollingFileInfo"
			fileName="G://log2/info.log"
			filePattern="G://log2/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log">
			<!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch) -->
			<ThresholdFilter level="info" onMatch="ACCEPT"
				onMismatch="DENY" />
			<PatternLayout
				pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n" />
			<Policies>
				<TimeBasedTriggeringPolicy />
				<SizeBasedTriggeringPolicy size="10MB" />
			</Policies>
		</RollingFile>
		<RollingFile name="RollingFileWarn"
			fileName="G://log2/warn.log"
			filePattern="G://log2/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log">
			<ThresholdFilter level="warn" onMatch="ACCEPT"
				onMismatch="DENY" />
			<PatternLayout
				pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n" />
			<Policies>
				<TimeBasedTriggeringPolicy />
				<SizeBasedTriggeringPolicy size="10MB" />
			</Policies>
			<!-- DefaultRolloverStrategy属性如不设置,则默认为最多同一文件夹下7个文件,这里设置了10 -->
			<DefaultRolloverStrategy max="10" />
		</RollingFile>
		<RollingFile name="RollingFileError"
			fileName="G://log2/error.log"
			filePattern="G://log2/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log">
			<ThresholdFilter level="error" onMatch="ACCEPT"
				onMismatch="DENY" />
			<PatternLayout
				pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n" />
			<Policies>
				<TimeBasedTriggeringPolicy />
				<SizeBasedTriggeringPolicy size="10MB" />
			</Policies>
		</RollingFile>
	</appenders>
	<!--然后定义logger,只有定义了logger并引入的appender,appender才会生效 -->
	<loggers>
		<!--过滤掉spring和mybatis的一些无用的DEBUG信息 -->
         <logger name="org.springframework" level="INFO"></logger>
         <logger name="org.mybatis" level="INFO"></logger>
		<root level="all">
			<appender-ref ref="Console" />
			<appender-ref ref="RollingFileInfo" />
			<appender-ref ref="RollingFileWarn" />
			<appender-ref ref="RollingFileError" />
		</root>
	</loggers>
</configuration>

三、编写代码:

1)Log4j.java  (注意:导入Logger的jar包和Logger的获取方式)

package com.lris.test;

import org.apache.log4j.Logger;
public class Log4j {

	private static Logger logger = Logger.getLogger(Test.class);  

    public static void main(String[] args) {  
    		
        // 记录debug级别的信息  
        logger.debug("This is debug message!");  
        // 记录info级别的信息  
        logger.info("This is info message!");  
        // 记录error级别的信息  
        logger.error("This is error message!");
        // 记录warn级别的信息
     	logger.warn("This is warn message!"); 
    }  
    

}

2)Log4j2.java  (注意:导入Logger的jar包和Logger的获取方式)

package com.lris.test;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


public class Log4j2 {

    private static Logger logger2 = LogManager.getLogger(Test.class);

    public static void main(String[] args) {  
    	
		// 记录debug级别的信息
		logger2.debug("This is debug message!");
		// 记录info级别的信息
		logger2.info("This is info message!");
		// 记录error级别的信息
		logger2.error("This is error message!"); 
		// 记录warn级别的信息
		logger2.warn("This is warn message!"); 
    }  
    

}
相关标签: log4j log4j2