찾다

 >  Q&A  >  본문

java - slf4j+logback无法输出日志?

pom.xml中已经引入依赖

      <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.21</version>
        </dependency>
  
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-nop</artifactId>
            <version>1.7.12</version>
        </dependency>

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.1.7</version>
        </dependency>

在resources目录下已经创建了logback.xml

<?xml version="1.0" encoding="UTF-8"?>

<configuration debug="true">

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
</appender>

<root level="debug">
    <appender-ref ref="STDOUT"/>
</root>
</configuration>

测试类中已经引入了logger

  private  Logger logger = LoggerFactory.getLogger(this.getClass());

 @Test
    public void getById() throws Exception {
        Seckill seckill = seckillService.getById(1000L);
        logger.error("seckill={}",seckill);
    }

测试通过但是控制台不打印日志

"C:\Program Files\Java\jdk1.8.0_77\bin\java" -ea -Didea.launcher.port=7541 "-Didea.launcher.bin.path=E:\IntelliJ IDEA 2016.2\bin" -Didea.junit.sm_runner -Dfile.encoding=UTF-8 -classpath "E:\IntelliJ IDEA 2016.2\lib\idea_rt.jar;E:\IntelliJ IDEA 2016.2\plugins\junit\lib\junit-rt.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\rt.jar;F:\chromedownloads\demo\fuckoff\target\test-classes;F:\chromedownloads\demo\fuckoff\target\classes;E:\mavenRepository\junit\junit\4.12\junit-4.12.jar;E:\mavenRepository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;E:\mavenRepository\org\slf4j\slf4j-api\1.7.21\slf4j-api-1.7.21.jar;E:\mavenRepository\org\slf4j\slf4j-nop\1.7.12\slf4j-nop-1.7.12.jar;E:\mavenRepository\ch\qos\logback\logback-core\1.1.7\logback-core-1.1.7.jar;E:\mavenRepository\mysql\mysql-connector-java\5.1.38\mysql-connector-java-5.1.38.jar;E:\mavenRepository\c3p0\c3p0\0.9.1.2\c3p0-0.9.1.2.jar;E:\mavenRepository\org\mybatis\mybatis\3.4.0\mybatis-3.4.0.jar;E:\mavenRepository\org\mybatis\mybatis-spring\1.3.0\mybatis-spring-1.3.0.jar;E:\mavenRepository\taglibs\standard\1.1.2\standard-1.1.2.jar;E:\mavenRepository\jstl\jstl\1.2\jstl-1.2.jar;E:\mavenRepository\com\fasterxml\jackson\core\jackson-databind\2.6.6\jackson-databind-2.6.6.jar;E:\mavenRepository\com\fasterxml\jackson\core\jackson-annotations\2.6.0\jackson-annotations-2.6.0.jar;E:\mavenRepository\com\fasterxml\jackson\core\jackson-core\2.6.6\jackson-core-2.6.6.jar;E:\mavenRepository\javax\servlet\javax.servlet-api\3.1.0\javax.servlet-api-3.1.0.jar;E:\mavenRepository\org\springframework\spring-core\4.2.8.RELEASE\spring-core-4.2.8.RELEASE.jar;E:\mavenRepository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;E:\mavenRepository\org\springframework\spring-beans\4.2.8.RELEASE\spring-beans-4.2.8.RELEASE.jar;E:\mavenRepository\org\springframework\spring-context\4.2.8.RELEASE\spring-context-4.2.8.RELEASE.jar;E:\mavenRepository\org\springframework\spring-aop\4.2.8.RELEASE\spring-aop-4.2.8.RELEASE.jar;E:\mavenRepository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;E:\mavenRepository\org\springframework\spring-expression\4.2.8.RELEASE\spring-expression-4.2.8.RELEASE.jar;E:\mavenRepository\org\springframework\spring-jdbc\4.2.8.RELEASE\spring-jdbc-4.2.8.RELEASE.jar;E:\mavenRepository\org\springframework\spring-tx\4.2.8.RELEASE\spring-tx-4.2.8.RELEASE.jar;E:\mavenRepository\org\springframework\spring-web\4.2.8.RELEASE\spring-web-4.2.8.RELEASE.jar;E:\mavenRepository\org\springframework\spring-webmvc\4.2.8.RELEASE\spring-webmvc-4.2.8.RELEASE.jar;E:\mavenRepository\org\springframework\spring-test\4.2.8.RELEASE\spring-test-4.2.8.RELEASE.jar" com.intellij.rt.execution.application.AppMain com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 org.service.impl.SeckillServiceImplTest,getById
十一月 12, 2016 4:19:56 下午 org.springframework.test.context.support.DefaultTestContextBootstrapper getDefaultTestExecutionListenerClassNames
信息: Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
十一月 12, 2016 4:19:56 下午 org.springframework.test.context.support.DefaultTestContextBootstrapper getTestExecutionListeners
信息: Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@6be46e8f, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@3567135c, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@327471b5, org.springframework.test.context.support.DirtiesContextTestExecutionListener@4157f54e, org.springframework.test.context.transaction.TransactionalTestExecutionListener@90f6bfd, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@47f6473]
十一月 12, 2016 4:19:56 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring/spring-dao.xml]
十一月 12, 2016 4:19:56 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring/spring-service.xml]
十一月 12, 2016 4:19:57 下午 org.springframework.context.support.GenericApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.GenericApplicationContext@28d25987: startup date [Sat Nov 12 16:19:57 CST 2016]; root of context hierarchy
十一月 12, 2016 4:19:57 下午 org.springframework.beans.factory.config.PropertyPlaceholderConfigurer loadProperties
信息: Loading properties file from class path resource [jdbc.properties]
十一月 12, 2016 4:19:57 下午 com.mchange.v2.log.MLog <clinit>
信息: MLog clients using java 1.4+ standard logging.
十一月 12, 2016 4:19:57 下午 com.mchange.v2.c3p0.C3P0Registry banner
信息: Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
十一月 12, 2016 4:19:58 下午 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
信息: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 2, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 1000, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1hge1hk9kob88691uo4ps8|34e9fd99, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hge1hk9kob88691uo4ps8|34e9fd99, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql://localhost:3306/seckill?useUnicode=true&characterEncoding=UTF-8, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 30, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 10, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]

Process finished with exit code 0


16:19    Tests Passed: 1 passed

请问如何解决?

PHP中文网PHP中文网2813일 전923

모든 응답(1)나는 대답할 것이다

  • 天蓬老师

    天蓬老师2017-04-18 10:21:02

    logback.xml은 클래스 디렉터리의 루트 경로에 위치합니다. Maven에 있다면 testresources에 넣으세요

    회신하다
    0
  • 취소회신하다