log4j/Log4j2 配置

Event LevelLoggerConfig Level
 --TRACEDEBUGINFOWARNERRORFATALOFF
ALLYESYESYESYESYESYESNO
TRACEYESNONONONONONO
DEBUGYESYESNONONONONO
INFOYESYESYESNONONONO
WARNYESYESYESYESNONONO
ERRORYESYESYESYESYESNONO
FATALYESYESYESYESYESYESNO
OFFNONONONONONONO
package main;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
public class Main  {
    public static void main(String args[]){
        Logger Log = LogManager.getLogger(Main.class.getName());
        Log.info("this is the info");
        Log.warn("this is the warn info");
        Log.error("this is the error info");
        Log.fatal("this is the fatal info");
        Log.trace("enter Main.test()");
        //new Main().test();
        Log.trace("exit Main.test()");
    }
}
<?xml version="1.0" encoding="UTF-8"?>
 
<configuration debug="off" status="INFO"> <!-- 这个status是控制系统信息的输出级别 -->
	<Properties>
		<Property name="path">D:/logs</Property>
	</Properties>
 
	<Appenders>
		<Console name="Console" target="SYSTEM_OUT">	<!-- 将日志信息从控制台输出 -->
			<ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY" />
			<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5level %class{36}.%M()/%L - %msg%xEx%n" />
		</Console>
 
		<File name="debug" fileName="${path}/debug.log" append="true">	<!-- 将日志信息写入日志文件 -->
			<Filters>
				<!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch) -->
				<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY" />
				<ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY" />
			</Filters>
			<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5level %class{36}.%M()/%L - %msg%xEx%n" />
		</File>
 
		<RollingFile name="RollingFile" fileName="${path}/log.log" filePattern="${path}/log-%d{yyyy-MM-dd}_%i.log">
			<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5level %class{36}.%M()/%L - %msg%xEx%n" />
			<SizeBasedTriggeringPolicy size="50MB" />
		</RollingFile>
	</Appenders>
 
	<Loggers>
		<Root level="trace">
			<AppenderRef ref="Console" />   <!-- 仅有上述的Appenders配置还不够,这里还不能少,少了就不会在控制台输出 -->
			<AppenderRef ref="RollingFile" />   
			<AppenderRef ref="debug" />  <!-- 仅有上述的Appenders配置还不够,这里还不能少,少了就不会写入文件,但会创建文件 -->
		</Root>
	</Loggers>
 
</configuration>
log4j.rootLogger=debug,stdout,error

# set the output lever
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.Encoding=UTF-8
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout.ConversionPattern=[%p] - [%d{yyyy-MM-dd HH:mm:ss}] %F %m%n

# %-d{yyyy-MM-dd HH\:mm\:ss} [%p]-[%c] %m%n

# set the Spring lever
log4j.category.org.springframework.beans.factory = DEBUG

### put the log to the file
log4j.appender.debug=org.apache.log4j.RollingFileAppender
log4j.appender.debug.File=../logs/debug.log
log4j.appender.debug.MaxFileSize = 10MB
log4j.appender.debug.Threshold = DEBUG
log4j.appender.debug.Encoding=UTF-8
log4j.appender.debug.layout=org.apache.log4j.PatternLayout
log4j.appender.debug.layout.ConversionPattern=[%p] - [%d{yyyy-MM-dd HH:mm:ss}] %F %m%n

### put the log to the file
log4j.appender.error=org.apache.log4j.RollingFileAppender
log4j.appender.error.File=../logs/error.log
log4j.appender.error.MaxFileSize = 10MB
log4j.appender.error.Threshold = ERROR
log4j.appender.error.Encoding=UTF-8
log4j.appender.error.layout=org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPattern=[%p] - [%d{yyyy-MM-dd HH:mm:ss}] %F %m%n

### output the sql
log4j.logger.com.pay.dao=DEBUG

### druid sql ###
log4j.logger.druid.sql=warn,stdout
log4j.logger.druid.sql.DataSource=warn,stdout
log4j.logger.druid.sql.Connection=warn,stdout
log4j.logger.druid.sql.Statement=warn,stdout
log4j.logger.druid.sql.ResultSet=warn,stdout

There are no comment yet.

HTML tag cannot be used in this comment.