zoukankan      html  css  js  c++  java
  • Mybatis中日志工厂LOG4J的使用步骤

    1.导入jar包

    <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
    </dependency>

    2.新建一个配置文件log4j.properties


    #将等级为DEBUG的日志信息输出到console和file这两个目的地,console和file的定义在下面的代码
    log4j.rootLogger=DEBUG,console,file

    #控制台输出的相关设置
    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

    #文件输出的相关设置
    log4j.appender.file = org.apache.log4j.RollingFileAppender
    log4j.appender.file.File=./log/joe.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

    #日志输出级别
    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.sql.PreparedStatement=DEBUG

    3.在主配置文件中,通过settings标签设置日志文件类型

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

    4.在使用log4j的类中新建一个静态对象,

    4.1:注意要导对包org.apche.log4j.Logger;

    4.2:日志对象,参数为当前类的class    (UserMapperTest.class)

    package com.joe.dao;

    import com.joe.pojo.User;
    import com.joe.utils.MyBatisUtil;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.log4j.Logger;
    import org.junit.Test;


    public class UserMapperTest {
    static Logger logger = Logger.getLogger(UserMapperTest.class);
    @Test
    public void getUserByIDTest(){
    SqlSession sqlSession = MyBatisUtil.getSqlSession();
    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    User userByID = mapper.getUserByID(1);
    System.out.println(userByID);
    sqlSession.close();
    }
    @Test
    public void testLog4j(){
    logger.info("info:进入了testlog4j");
    logger.debug("debug:进入了testLog4j");
    logger.error("error:进入了testLog4j");
    }
    }

    5.在代码中加一句日志信息,可以选择info类型,也可以选择debug或error类型{logger.info(),logger.debug(),logger.error()}

    package com.joe.dao;

    import com.joe.pojo.User;
    import com.joe.utils.MyBatisUtil;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.log4j.Logger;
    import org.junit.Test;


    public class UserMapperTest {
    static Logger logger = Logger.getLogger(UserMapperTest.class);
    @Test
    public void getUserByIDTest(){
    SqlSession sqlSession = MyBatisUtil.getSqlSession();

    logger.info("测试,进入getUserByIDtest方法成功!");

    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    User userByID = mapper.getUserByID(1);
    System.out.println(userByID);
    sqlSession.close();
    }
    @Test
    public void testLog4j(){
    logger.info("info:进入了testlog4j");
    logger.debug("debug:进入了testLog4j");
    logger.error("error:进入了testLog4j");
    }
    }

    6.执行成功后,控制台会打印log4j日志信息,并且之前我们在配置文件中设置了让打印的log日志保存到项目的当前路径下

    可以看到,我这里多了一个lt.log文件,里面清晰的写出了每一句日志的级别,刚才我们自定义的info日志也在其中~~~

    [DEBUG][20-05-04][org.apache.ibatis.logging.LogFactory]Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
    [DEBUG][20-05-04][org.apache.ibatis.logging.LogFactory]Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
    [DEBUG][20-05-04][org.apache.ibatis.datasource.pooled.PooledDataSource]PooledDataSource forcefully closed/removed all connections.
    [DEBUG][20-05-04][org.apache.ibatis.datasource.pooled.PooledDataSource]PooledDataSource forcefully closed/removed all connections.
    [DEBUG][20-05-04][org.apache.ibatis.datasource.pooled.PooledDataSource]PooledDataSource forcefully closed/removed all connections.
    [DEBUG][20-05-04][org.apache.ibatis.datasource.pooled.PooledDataSource]PooledDataSource forcefully closed/removed all connections.
    [INFO][20-05-04][com.joe.dao.UserMapperTest]测试,进入getUserByIDtest方法成功!
    [DEBUG][20-05-04][org.apache.ibatis.transaction.jdbc.JdbcTransaction]Opening JDBC Connection
    [DEBUG][20-05-04][org.apache.ibatis.datasource.pooled.PooledDataSource]Created connection 660879561.
    [DEBUG][20-05-04][org.apache.ibatis.transaction.jdbc.JdbcTransaction]Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@276438c9]
    [DEBUG][20-05-04][com.joe.dao.UserMapper.getUserByID]==> Preparing: select * from user where id = ?
    [DEBUG][20-05-04][com.joe.dao.UserMapper.getUserByID]==> Parameters: 1(Integer)
    [DEBUG][20-05-04][com.joe.dao.UserMapper.getUserByID]<== Total: 1
    [DEBUG][20-05-04][org.apache.ibatis.transaction.jdbc.JdbcTransaction]Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@276438c9]
    [DEBUG][20-05-04][org.apache.ibatis.transaction.jdbc.JdbcTransaction]Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@276438c9]
    [DEBUG][20-05-04][org.apache.ibatis.datasource.pooled.PooledDataSource]Returned connection 660879561 to pool.

    ————————————————
    版权声明:本文为CSDN博主「刘桐ssss」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/delicious_life/article/details/105660519

  • 相关阅读:
    固定sql语句传参批量查询数据库脚本
    超多行数据纵向编辑
    takes 3 positional arguments but 4 were given错误
    使用PMD进行代码审查(转)
    WebADI应用到Office 2016 64-bit
    SVN 提交代码时强制加入注释内容
    DOCKER初体验
    "make_path" is not exported by the File::Path modul
    perl 调用shell脚本
    scp 上传文件到多个服务器节点
  • 原文地址:https://www.cnblogs.com/luckyhong334/p/12826853.html
Copyright © 2011-2022 走看看