- log4j.xml
除了使用log4j.properties,也可以使用xml格式进行配置。
在src目录下装备log4j.xml文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN" "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d %-5p %c.%M:%L - %m%n"/> </layout> </appender> <!-- specify the logging level for loggers from other libraries --> <logger name="com.opensymphony"> <level value="ERROR" /> </logger> <logger name="org.apache"> <level value="ERROR" /> </logger> <logger name="org.hibernate"> <level value="ERROR" /> </logger> <!-- for all other loggers log only debug and above log messages --> <root> <priority value="ERROR"/> <appender-ref ref="STDOUT" /> </root> </log4j:configuration>
- 修改TestLog4j
使用log4j.xml作为配置文件,并运行
package log4j; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; public class TestLog4j { static Logger logger = Logger.getLogger(TestLog4j.class); public static void main(String[] args) throws InterruptedException { PropertyConfigurator.configure("e:\\project\\log4j\\src\\log4j.xml"); for (int i = 0; i < 5000; i++) { logger.trace("跟踪信息"); logger.debug("调试信息"); logger.info("输出信息"); logger.warn("警告信息"); logger.error("错误信息"); logger.fatal("致命信息"); } } }
LOG4J.XML
最新推荐文章于 2023-09-24 11:19:53 发布