- Package com.v512.log4j;
- Import Org.apache.log4j.Logger;
- Public class Hellolog4j {
- //Construct a logger, which is the class in which the logger is located, indicating that you want to log in the class
- private static Logger Logger = Logger.getlogger (hellolog4j. Class);
- /**
- * @param args
- */
- public static void Main (string[] args) {
- GetMessage ();
- }
- private static void GetMessage () {
- //record various levels of information, where it is stored, and in which way it is configured in the Log4j.properties file.
- 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 PlainCopy
- 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 PlainCopy
- Message.log.2012-07-28
- Message.log.2012-07-27
- Message.log (today)
- 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.
Transfer from http://blog.csdn.net/seeds_home/article/details/7800908
About Log4j.properties Example: Dailyrollingfileappender