Use Mybatis in two kinds of logs

If the database operation, there has been an exception, we need debugging, do not know what went wrong, the log is the best assistant!

method one

STDOUT_LOGGING standard log output
in mybatis core configuration files, configuring our log!

<settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>

Method Two

Laog4j

  • Log4j is Apache is an open source project, through the use of Log4j, we can control the destination log information is conveyed console , a file, the GUI components
  • We can also control the output format of each log
  • Each level is defined by a log of information, we can more carefully control the build process logs.
  • Through a configuration file to configure flexibly, without the need to modify the application code.

1. The first import package log4j

    <dependencies>
        <!-- https://mvnrepository.com/artifact/log4j/log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
    </dependencies>

2.log4j.properties
this according to their need to use the Internet to find a random search

log4j.rootLogger=debug, stdout, R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log
log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=5
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

3. Configure log4j to log realization

    <settings>
        <setting name="logImpl" value="LOG4J"/>
    </settings>

4.log4j use!

  1. Java statement to directly test run

  2. Simple to use

     在要使用Log4j 的类中,导入org.apache.log4j.Logger;
    
     日志对象,加载参数为当前类的class
    
     static Logger logger = Logger.getLogger(UserDaoTest.class);
    
       设置日志级别
    
    logger.info("info:进入了testLog4j方法");
    logger.debug("debug:进入了testLog4j");
    logger.error("error:进入了testLog4j");
    
Published 30 original articles · won praise 51 · views 1973

Guess you like

Origin blog.csdn.net/qq_41256881/article/details/105367535