简体   繁体   中英

Redirect logging from Tomcat to my file (logback)

I use Tomcat 8 and logback on Windows. I configured my logback.xml this way:

<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>logging.log</file>
    <param name="Append" value="false" />
    <encoder>
        <pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
    </encoder>
</appender>

<root level="info">
    <appender-ref ref="FILE" />
</root>

My pom.xml if neccessary:

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-core</artifactId>
    <version>1.2.11</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.2.11</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.36</version>
    <scope>compile</scope>
</dependency>

When I started app with public static void main , everything was logging to my logging.log file in the project properly. However when I started booting my app with Apache Tomcat 8 , is has embedded logging (example is on the picture):

Tomcat embedded logging

So my logging.log file keeps empty. How to redirect all the logs back to it?

Tomcat uses different logging mechanism - JULI . What you want to do, is to add jul-to-slf4j bridge. See https://www.slf4j.org/legacy.html#jul-to-slf4j .

I just found out how silly my question was.

Answer is that Tomcat just changes project root location for logging to &CATALINA_HOME&\bin (in my case C:\apache-tomcat-8.5.81\bin). The file logging.log was created in that directory and has been correctly logging all this time. Meanwhile I was into a stupor because of empty logging.log in my project directory.

Solution is just to describe the absolute path for the logging file in logback.xml:

<file>C:\Users\cognosce\Desktop\myproject\logging.log</file>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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