slf4j结合log4j使用

怎样在maven项目中引入slf4j

  1. 在pom文件中引入需要的jar包
 <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-apiartifactId>
            <version>1.7.25version>
        dependency>
        
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>jcl-over-slf4jartifactId>
            <version>1.7.25version>
            <scope>runtimescope>
        dependency>
         
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>jul-to-slf4jartifactId>
            <version>1.7.21version>
        dependency>
        
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-log4j12artifactId>
            <version>1.7.21version>
        dependency>
         
  		<dependency>
            <groupId>log4jgroupId>
            <artifactId>log4jartifactId>
            <version>1.2.17version>
        dependency>

  1. 在类路径下建立log4j.xml,如下给出一个示例


<log4j:configuration>
    
    
    <appender name="ConsoleOutput" class="org.apache.log4j.ConsoleAppender">
        
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-d{yyyy-MM-dd HH:mm:ss,SSS} [%c]-[%p] %m%n"/>
        layout>
    appender>
    
    <appender name="myFile" class="org.apache.log4j.DailyRollingFileAppender">
        <param name="File" value="E:\\IdeaProjects\\complat\\target\\complat\\output.log"/>
        <param name="Append" value="true"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern"
                   value="%-d{yyyy-MM-dd HH:mm:ss,SSS} [%c]-[%p] %m%n"/>
        layout>
    appender>


    
    

    
    
    <logger name="jdbc.sqlonly" additivity="true">
        <level value="INFO"/>
    logger>
    <logger name="jdbc.sqltiming" additivity="true">
        <level value="ERROR"/>
    logger>

    <logger name="jdbc.resultsettable" additivity="true">
        <level value="ERROR"/>
    logger>

    <logger name="jdbc.resultset" additivity="true">
        <level value="ERROR"/>
    logger>
    <logger name="jdbc.connection" additivity="true">
        <level value="ERROR"/>
    logger>
    <logger name="jdbc.audit" additivity="true">
        <level value="WARN"/>
    logger>


    
    <root>
        
        <priority value="error"/>
        <appender-ref ref="ConsoleOutput"/>
        <appender-ref ref="myFile"/>
    root>
log4j:configuration>



  1. 代码中的使用
//应用日志类
protected final Logger log = LoggerFactory.getLogger(this.getClass());
//占位符输出debug级别的信息
log.debug("username is:{} and password is:{}",user.getUserName(),user.getPassword());   
小常识:

tdiwef:日志级别
log.trace(“Trace Message!”);
log.debug(“Debug Message!”);
log.info(“Info Message!”);
log.warn(“Warn Message!”);
log.error(“Error Message!”);
log.fatal(“Fatal Message!”);
若是warn ,则输出 wef级别的数据;
若是info,则不能输出debug级别和trace的数据

参考:spring-mybatis项目中打印完整sql不带占位符

你可能感兴趣的:(slf4j结合log4j)