自带日志

  1. public class Hello {
  2. public static void main(String[] args) {
  3. Logger logger = Logger.getGlobal();
  4. logger.info("start process...");
  5. logger.warning("memory is running out...");
  6. logger.fine("ignored.");
  7. logger.severe("process will be terminated...");
  8. }
  9. }

Log4j

resources目录下创建log4j.properties文件

  1. ### 设置###
  2. log4j.rootLogger = debug,stdout,D,E
  3. ### 输出信息到控制抬 ###
  4. log4j.appender.stdout = org.apache.log4j.ConsoleAppender
  5. log4j.appender.stdout.Target = System.out
  6. log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
  7. log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
  8. ### 输出DEBUG 级别以上的日志到=/home/duqi/logs/debug.log ###
  9. log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
  10. log4j.appender.D.File = C:debug.log
  11. log4j.appender.D.Append = true
  12. log4j.appender.D.Threshold = DEBUG
  13. log4j.appender.D.layout = org.apache.log4j.PatternLayout
  14. log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
  15. ### 输出ERROR 级别以上的日志到=/home/admin/logs/error.log ###
  16. log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
  17. log4j.appender.E.File =C:error.log
  18. log4j.appender.E.Append = true
  19. log4j.appender.E.Threshold = ERROR
  20. log4j.appender.E.layout = org.apache.log4j.PatternLayout
  21. log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n

输出日志的例子

  1. package com.javadu.log;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. public class Log4JTest {
  5. private static final Logger logger = LoggerFactory.getLogger(Log4JTest.class);
  6. public static void main(String[] args) {
  7. // 记录debug级别的信息
  8. logger.debug("This is debug message.");
  9. // 记录info级别的信息
  10. logger.info("This is info message.");
  11. // 记录error级别的信息
  12. logger.error("This is error message.");
  13. }
  14. }