Log4j understanding

Brief introduction

Log for Java, the Java language specifically for logging tool

  • Debug Log
  • Run Log
  • Exception log

logback

Log Level

  • fatal
  • error
  • warn
  • info
  • debug
  • trace

Log file output control

Configuration

log4j.properties

  • Output Location
  • Output Format
  • Output Level

Log adjunct

output position log4j.appender.appenderName =

hello-Log4j

slf4j

Simple Loging Facade For Java, the facade pattern, just an interface / rule, did not materialize, slf4j alone can not work alone, to be with the other specific log implementation

POM

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.25</version>
</dependency>

Profiles

log4j.properties

log4j.rootLogger=INFO, console, file

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=logs/log.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.A3.MaxFileSize=1024KB
log4j.appender.A3.MaxBackupIndex=10
log4j.appender.file.layout.ConversionPattern=%d %p [%c] - %m%n

Test in class

public class MyTest {
    
    private static final Logger logger = LoggerFactory.getLogger(MyTest.class);
    
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-context.xml");
        UserService userService = (UserService) applicationContext.getBean("userService");
        userService.sayHi();
        
        logger.info("info 级别日志");
        logger.warn("warn 级别日志");
        logger.error("error 级别日志");
        
        String message1 = "测试1";
        String message2 = "测试2";
        
        logger.info("message is:{}  {}", message1, message2);

//        System.out.println(String.format("message is : %s %s", message1, message2));
    }
}

Guess you like

Origin www.cnblogs.com/hhhqqq/p/12582866.html