Log Factory (STDOUT_LOGGING)

Use: used to view our program abnormalities, convenient troubleshooting

Implementation: Configure the Settings file in mybatis-config. XML

<settings>                                              
        <setting name="logImpl" value="STDOUT_LOGGING"/>    
</settings>
Copy the code

Log4j

Log4j is an open source project of Apache. By using Log4j, you can control the destination of log messages to the console, files, GUI components, even the socket server, NT event logger, UNIX Syslog daemon, etc. We can also control the output format of each log; By defining the level of each log message, we can more carefully control the log generation process. The most interesting thing is that these can be configured flexibly through a configuration file without the need to modify the application code.

Implementation steps:

  1. Import the log4j package first
<dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
</dependency>
Copy the code
  1. Write the log4j properties
Output DEBUG logs to the console and file destinations. Console and file are defined in the code below
log4j.rootLogger=DEBUG,console,file
Settings for console output
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.Threshold=DEBUG
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%c]-%m%n
# File output Settings
log4j.appender.file = org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./log/Log4j.log
log4j.appender.file.MaxFileSize=10mb
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd}][%c]%m%n
# Log output level
log4j.logger.org.mybatis=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sq1.PreparedStatement=DEBUG

Copy the code
  1. Configure the Settings file in the mybatis-config. XML file
    <settings>
        <setting name="logImpl" value="Log4j"/>
    </settings>
Copy the code
  1. test

    // The arguments in the getLogger method fill in the current class object
    Logger log = Logger.getLogger(UserDaoTest.class);
    @Test
    public void Log4jTest(a){
        
        // Three levels of info debug error
        log.info("info");
        log.debug("debug");
        log.error("error");
    }
Copy the code