import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class HelloLog4j { private static Logger logger = LogManager.getLogger("HelloLog4j"); public static void main(String[] args) { MyApplication myApplication = new MyApplication(); logger.entry(); logger.info("Hello, World!"); myApplication.doIt(); logger.error("Hello, World!"); logger.exit(); } }
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class MyApplication { static Logger logger = LogManager.getLogger(MyApplication.class.getName()); public boolean doIt() { logger.entry(); //Log entry to a method logger.error("Did it again!"); //Log a message object with the ERROR level logger.exit(); //Log exit from a method return false; } }
运行程序,输出结果为:
16:10:28.672 [main] ERROR MyApplication - Did it again! 16:10:28.672 [main] ERROR HelloLog4j - Hello, World!
3. 配置文件
log4j是apache的一个开源项目,在写这篇博客的时候已经发布了2.0的beta版本,首先需要注意的是,log4j 2.0与以往的1.x有一个明显的不同,其配置文件只能采用.xml, .json或者 .jsn。在默认情况下,系统选择configuration文件的优先级如下:(classpath为scr文件夹)
classpath下名为 log4j-test.json 或者log4j-test.jsn文件
classpath下名为 log4j2-test.xml
classpath下名为 log4j.json 或者log4j.jsn文件
classpath下名为 log4j2.xml
必须注意.xml 文件的文件名为log4j2,这里让我纠结了一个下午,系统一直找不到配置文件,最后发现是文件名里面少了一个2。下面以log4j2.xml为例来介绍log4j的配置。
<?xml version="1.0" encoding="UTF-8"?> <configuration status="OFF"> <appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> </Console> </appenders> <loggers> <root level="trace"> <appender-ref ref="Console"/> </root> </loggers> </configuration>
<?xml version="1.0" encoding="UTF-8"?> <configuration status="OFF"> <appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> </Console> </appenders> <loggers> <logger name="com.relin.HelloLog4j" level="error" additivity="false"> <appender-ref ref="Console"/> </logger> <root level="trace"> <appender-ref ref="Console"/> </root> </loggers> </configuration>此时,HelloLog4j则会在error级别上输出log,而其他类则会在trace级别上输出log。需要注意的是 additivity选项,如果设置为true(默认值)则HelloLog4j的log会被打印两次,第二次打印是由于HelloLog4j同时也满足root里面定义的trace
4. 其他特征
<?xml version="1.0" encoding="UTF-8"?> <configuration status="error"> <appenders> <Console name="Console" target="SYSTEM_OUT"> <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/> </Console> <File name="log" fileName="target/test.log" append="false"> <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/> </File> <RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz"> <PatternLayout pattern="%d{yyyy.MM.dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/> <SizeBasedTriggeringPolicy size="500 MB" /> </RollingFile> </appenders> <loggers> <root level="trace"> <appender-ref ref="RollingFile"/> <appender-ref ref="Console"/> </root> </loggers> </configuration>
相关:http://logging.apache.org/log4j/2.x/manual/configuration.html