org.slf4j.Logger

1.导入maven依赖


        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.0version>
        dependency>

        
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-apiartifactId>
            <version>1.7.25version>
        dependency>

        
        <dependency>
            <groupId>ch.qos.logbackgroupId>
            <artifactId>logback-classicartifactId>
            <version>1.2.3version>
        dependency>

        
        <dependency>
            <groupId>org.junit.jupitergroupId>
            <artifactId>junit-jupiter-apiartifactId>
            <version>5.2.0version>
            <scope>testscope>
        dependency>

2.生成日志配置文件logback.xml


<configuration>

    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%npattern>
        encoder>
    appender>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%npattern>
            <charset>utf-8charset>
        encoder>
        <file>log/output.logfile>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>log/output.log.%ifileNamePattern>
        rollingPolicy>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>1MBMaxFileSize>
        triggeringPolicy>
    appender>

    <root level="DEBUG">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="FILE" />
    root>
configuration>

使用

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Log4jTest {
    final Logger logger = LoggerFactory.getLogger(getClass());

    @Test
    public void log4jTest() {
        String str = "锄禾日当午,\n汗滴禾下土。\n谁知盘中餐吗,\n粒粒皆辛苦。";
        System.out.println("--------------------debug------------------------");
        logger.debug(str);
        System.out.println("--------------------info------------------------");
        logger.info(str);
        System.out.println("--------------------warn------------------------");
        logger.warn(str);
        System.out.println("--------------------error------------------------");
        logger.error(str);
    }
}

你可能感兴趣的:(Java,java,maven,intellij-idea)