Question

trying to get logback to log to a .log file, but it's only outputting to the console... any ideas?

heres the logback-test.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="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>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
        </encoder>
    </appender>

    <appender 
        name="FILE" 
        class="ch.qos.logback.core.FileAppender">
        <file>C:/Users/aahmad/Desktop/logbackTestFiel.log</file>
        <append>false</append>
        <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
        <encoder>
            <pattern>%-4r %-5level %logger{35}: %msg%n</pattern>
        </encoder>
    </appender>

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

</configuration>

and heres the code

package log.back.test;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.core.util.StatusPrinter;

public class HelloWorld2 {

  public static void main(String[] args) {
    Logger logger = LoggerFactory.getLogger("chapters.introduction.HelloWorld2");
    logger.debug("Hello world.");
    logger.info("Hello file");
    logger.error("Hello 2 world.");
    // print internal state
//    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
//    StatusPrinter.print(lc);
  }
}

thank you for the help... also, anyone know what the logback equivalent of org.apache.log4j.helpers.OnlyOnceErrorHandler? I'm actually doing a migration and log4j declares this

<errorHandler class="org.apache.log4j.helpers.OnlyOnceErrorHandler" />

but idk what the logback version of this is

Was it helpful?

Solution

ok figured it out, an additional tag needed to be added to the appender

<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>logFile.%d{yyyy-MM-dd}.log</FileNamePattern>
        </rollingPolicy>

this fixed my problem, still not sure about the errorHandler though

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top