An Entry Example of Log4j
The log4j can be configured both programmatically and externally using special configuration files. External configuration is most preferred, because to take effect it doesn’t require change in application code, recompilation, or redeployment. Configuration files can be XML files or Java property files that can be created and edited using any text editor or XML editor, respectively.
1. Programmatically configure log4j
This example is from this site, the code is messy, so why not make it clean.
|
Here is the snapshot of output:
2. Configure Log4j externally
Create a file named “log4j.properties” in the “src” directory of your project.
# Define the root logger with appender file log = /home/ryan/Desktop/log4j log4j.rootLogger = DEBUG, FILE # Define the file appender log4j.appender.FILE=org.apache.log4j.FileAppender log4j.appender.FILE.File=${log}/log.out # Define the layout for file appender log4j.appender.FILE.layout=org.apache.log4j.PatternLayout log4j.appender.FILE.layout.conversionPattern=%m%n
Here is the java code.
|
The log will be printed on the log.out file in /home/ryan/Desktop/log4j directory.
如果你喜欢本文, 请长按二维码,关注公众号 分布式编程.
作者:分布式编程
出处:https://zthinker.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。