Log4j
跳到导航
跳到搜索
Apache Log4j is a versatile, industrial-grade Java logging framework composed of an API, its implementation, and components to assist the deployment for various use cases.
https://logging.apache.org/log4j/2.x/
- 控制日志信息输送的目的地是控制台、文件、GUI 组件,甚至是套接口服务器、NT 的事件记录器、UNIX Syslog 守护进程等
- 控制每一条日志的输出格式
- 通过定义每一条日志信息的级别,更加细致地控制日志的生成过程
ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF
pom.xml
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
log4j.properties
# log4j.rootLogger=CONSOLE,stdout,logfile log4j.rootLogger=INFO,stdout # log4j.rootLogger=Error,stdout # Console log4j.appender.console = org.apache.log4j.ConsoleAppender log4j.appender.console.Target = System.out log4j.appender.console.Threshold=DEBUG log4j.appender.console.layout = org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=[%c]-%m%n # File log4j.appender.file = org.apache.log4j.RollingFileAppender log4j.appender.file.File=./log/system.log log4j.appender.file.MaxFileSize=10mb log4j.appender.file.Threshold=Error log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd}][%c]%m%n # Stdout log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] %m%n # Level log4j.logger.org.mybatis=DEBUG log4j.logger.java.sql=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.ResultSet=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG
如 kafka 中会大量输入 debug 信息,可以指定输出级别或关闭(=OFF): log4j.logger.org.apache.kafka.clients=Error log4j.logger.org.apache.kafka.common=Error -.OR.- log4j.logger.org.apache.kafka=Error
输出日志类
private static final Logger logger = Logger.getLogger(CLASSNAME.class) ... logger.info("information...")