简体   繁体   English

SLF4j未写入日志文件

[英]SLF4j not writing to a log file

In my web project I am trying to use SLF4j with logback , I have pasted the SLF4j xml settings and also the section of POM ,when I start the application on Tomacat in local machine. 在我的Web项目中,尝试在本地计算机上的Tomacat上启动应用程序时,我尝试将SLF4j与logback一起使用,粘贴了SLF4j xml设置以及POM部分。 I do not see any errors nor the logs are generated. 我没有看到任何错误,也没有生成日志。

logback.xml logback.xml

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

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <layout class="ch.qos.logback.classic.PatternLayout">
      <Pattern>%d{yyyyddMM:HHmmss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
    </layout>
  </appender>
  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>./myapp.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <!-- daily rollover -->
      <fileNamePattern>./myapp.%d{yyyy-MM-dd}.log</fileNamePattern>
      <!-- keep 30 days' worth of history -->
      <maxHistory>14</maxHistory>
    </rollingPolicy>
    <encoder>
      <pattern>%d{yyyyddMM:HHmmss.SSS} [%thread] %-5level %logger{35} - %msg%n</pattern>
    </encoder>
  </appender>
  <appender name="DEBUG" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>./myapp_debug.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <!-- daily rollover -->
      <fileNamePattern>./myapp_debug.%d{yyyy-MM-dd}.log</fileNamePattern>
      <!-- keep 30 days' worth of history -->
      <maxHistory>1</maxHistory>
    </rollingPolicy>
    <encoder>
      <pattern>%d{yyyyddMM:HHmmss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>
  <!-- logger name="com.base22" level="TRACE"/ -->
  <logger name="com.sample.app" level="TRACE" />
  <logger name="org.apache" level="ERROR" />
  <logger name="org.springframework" level="ERROR" />
  <logger name="org.hibernate" level="ERROR" />
  <logger name="java.sql" level="ERROR" />
  <logger name="jdbc.audit" level="ERROR" />
  <logger name="jdbc.connection" level="ERROR" />
  <logger name="jdbc.sqltiming" level="ERROR" />
  <logger name="jdbc.resultset" level="ERROR" />
  <logger name="jdbc.resultsettable" level="ERROR" />

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

Section of Maven Pom for Logging jars Maven Pom的伐木罐部分

<!-- Logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>1.7.12</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>log4j-over-slf4j</artifactId>
            <version>1.7.12</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.12</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.3</version>
        </dependency>

There is an interesting way to deal with this. 有一种有趣的方法可以解决这个问题。

First, verify that you have no conflicting bindings (ie log4j getting pulled in). 首先,请验证您没有冲突的绑定(即,将log4j插入)。 Since you are using maven, run the following command: 由于您正在使用maven,请运行以下命令:

mvn dependency:tree -Dverbose -Dincludes=:log4j,:commons-logging

If you see any of those dependencies included transitively, add exclusions to the dependencies that are bringing those in. 如果看到这些依赖项中有任何传递性包含,请将排除项添加到引入这些依赖项中。

Secondly, add jul-to-slf4j to your dependency list (corresponding to the version you are using). 其次,将jul-to-slf4j添加到您的依赖项列表中(对应于您使用的版本)。 You should put any and all bindings outside of slf4j-api as runtime scope. 您应该将所有绑定都放在slf4j-api之外,作为runtime范围。

Thirdly, add the file src/main/resources/logging.properties with the contents 第三,添加文件src / main / resources / logging.properties及其内容

handlers = org.slf4j.bridge.SLF4JBridgeHandler

If you are launching tomcat through the standard startup or catalina scripts it will automatically configure a "log manager" that will allow you to override a context specific java.util.logging (jul) configuration. 如果通过标准启动脚本或Catalina脚本启动tomcat,它将自动配置“日志管理器”,该日志管理器将允许您覆盖特定于上下文的java.util.logging(jul)配置。

See here for more information. 有关更多信息,请参见此处

Hi I fixed the problem made couple of changes , 嗨,我解决了几个问题,

POM.XML POM文件

<dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>1.7.12</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.3</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.1.3</version>
        </dependency>

In logback.xml 在logback.xml中

changed ./myapp.log to ${catalina.home}/logs/myapp.log 将./myapp.log更改为$ {catalina.home} /logs/myapp.log

and I can see the logs generated now in Tomcat/Logs directory 我可以在Tomcat / Logs目录中看到现在生成的日志

可能是您搜索的日志文件位置可能不正确,请使用名称搜索日志文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM