- package com.v512.log4j;
- import org.apache.log4j.Logger;
- public class HelloLog4J {
- // 构造记录器,形参是记录器所在的类,表示要在该类做日志
- private static Logger logger = Logger.getLogger(HelloLog4J.class);
- /**
- * @param args
- */
- public static void main(String[] args) {
- getMessage();
- }
- private static void getMessage() {
- // 记录下各种级别的信息,这些信息放在哪儿,以哪种方式存放,在log4j.properties文件中配置.
- logger.debug("This is debug message.");
- logger.info("This is a info message.");
- logger.warn("This is a warn message.");
- logger.error("This is a error message.");
- }
- }
[plain] view plain copy
- log4j.rootLogger=info,file
- log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
- log4j.appender.file.File=Message.log
- log4j.appender.file.layout=org.apache.log4j.PatternLayout
- log4j.appender.file.layout.ConversionPattern=%d{yyyyMMdd HH:mm:ss} %p %l [%t] %m%n
[plain] view plain copy
- Message.log.2012-07-28
- Message.log.2012-07-27
- Message.log(今天)
- 20120729 11:26:58 INFO com.v512.log4j.HelloLog4J.getMessage(HelloLog4J.java:18) [main] This is a info message.
- 20120729 11:26:59 WARN com.v512.log4j.HelloLog4J.getMessage(HelloLog4J.java:19) [main] This is a warn message.
- 20120729 11:26:59 ERROR com.v512.log4j.HelloLog4J.getMessage(HelloLog4J.java:20) [main] This is a error message.
- 转自http://blog.csdn.net/seeds_home/article/details/7800908
时间: 2024-11-01 11:17:38