背景与用途

  • log4j的配置文件是用来设置纪录器的级别、存放位置和布局的,可以通过Java属性文件(key=value)格式设置或XML格式设置。

  • SpringBoot中内置了log4j。

【20180928】SpringBoot   Log4j日志记录 - 图1

调试Sample代码

  1. public static void main(String [] args) {
  2. //BasicConfigurator.configure();
  3. Logger logger = LoggerFactory.getLogger(Log4jT.class);
  4. logger.debug( " this is {} debug log", Log4jT.class.getName() );
  5. logger.error( " this is {} error log", Log4jT.class.getName());
  6. }
  7. 结果:
  8. log4j:WARN No appenders could be found for logger (com.xiaofan.test.Test).
  9. log4j:WARN Please initialize the log4j system properly.
  10. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
  1. import org.apache.log4j.Logger;
  2. public class Test {
  3. private static Logger logger= Logger.getLogger(Test.class);
  4. public static void main(String[] args){
  5. logger.debug("This is a debug message.");
  6. logger.info("This is a info message.");
  7. logger.error("This is a error message.");
  8. }
  9. }

参考资料