log4j does not output in springboot and controls third-party log level

Configuration (log4j.properties)

log4j.rootLogger = debug,stdout,D,E,DB

### Output information to control lift ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
#### change to info in deployed env.###
log4j.appender.stdout.Threshold = DEBUG 
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

### output DEBUG Logs above level to=E://logs/log.log ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File =  E:/testlog/logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = INFO 
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

### output warn Logs above level to=E://logs/warn.log ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =E:/testlog/logs/warn.log 
log4j.appender.E.Append = true
log4j.appender.E.Threshold = WARN 
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

### output ERROR Logs above level to=E://logs/error.log ###
log4j.appender.DB = org.apache.log4j.DailyRollingFileAppender
log4j.appender.DB.File =E:/testlog/logs/error.log 
log4j.appender.DB.Append = true
log4j.appender.DB.Threshold = ERROR 
log4j.appender.DB.layout = org.apache.log4j.PatternLayout
log4j.appender.DB.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

###log output of specified class
#Usage method
##Logger logger = LoggerFactory.getLogger("Single");
log4j.logger.Single= debug,single
log4j.additivity.Single= false
log4j.appender.single=org.apache.log4j.DailyRollingFileAppender
#log4j.appender.Single.MaxFileSize=1MB
log4j.appender.single.File=E:/testlog/logs/single.log 
log4j.appender.single.Append = true
log4j.appender.single.Threshold = INFO
log4j.appender.single.layout=org.apache.log4j.PatternLayout
log4j.appender.single.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n
#Third-party jar package log output level
log4j.logger.org.springframework =ERROR
log4j.logger.org.apache.rocketmq=ERROR
log4j.logger.org.quartz=ERROR
log4j.logger.org.apache.zookeeper=ERROR
log4j.logger.com.alibaba.dubbo=ERROR

Demo


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class KafproducerApplication {
	static Logger logger = LoggerFactory.getLogger(KafproducerApplication.class);
	static Logger log= LoggerFactory.getLogger("Single");
	public static void main(String[] args) {
		ConfigurableApplicationContext run =SpringApplication.run(KafproducerApplication.class, args);
		logger.info("info");
		logger.error("error");
		logger.debug("debug");
		logger.warn("warn");
		log.info("this is single");
		log.error("error");
       
	}
}

Configuration Resolution

Logging priority is divided into OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL or custom levels.Log4j recommends using only four levels, with the highest to lowest priority being ERROR, WARN, INFO, and DEBUG, respectively.

Appender: The destination r for log information output, responsible for controlling the output of log operations. org.apache.log4j.ConsoleAppender (Console),
org.apache.log4j.FileAppender (file),
org.apache.log4j.DailyRollingFileAppender (produces one log file per day),
org.apache.log4j.RollingFileAppender (a new file is generated when the file size reaches the specified size),
org.apache.log4j.WriterAppender (sends log information in streaming format to any specified place)

Layout: Responsible for formatting Appender output. org.apache.log4j.HTMLLayout (in HTML tables),
org.apache.log4j.PatternLayout (flexible layout),
org.apache.log4j.SimpleLayout (level and information string containing log information),
org.apache.log4j.TTCCLayout (contains information on the time, threads, categories, and so on of log generation)

Threshold: Specifies the lowest level of output for log messages

File: Specify the output file path and name

Append=false: The default value is true, which adds the message to the specified file, and false means that the message overwrites the specified file content

MaxFileSize=100KB: The suffix can be KB, MB or GB. When the log file reaches this size, it will scroll automatically

MaxBackupIndex=2: Specifies the maximum number of scrolling files that can be generated

Tags: log4j Apache Zookeeper Dubbo

Posted on Sat, 21 Mar 2020 13:38:05 -0400 by Sillysoft