Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

logback not generating file

I am new to logback. I am trying to log in a file. My logback.xml is:

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

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%d{yyyy-MM-dd_HH:mm:ss.SSS} %-5level %logger{36} - %msg%n
            </Pattern>
        </encoder>
    </appender>

    <appender name="FILE"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>d:/walknshine.log</File>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%d{yyyy-MM-dd_HH:mm:ss.SSS} [%thread] %-5level %logger{36} -
                %msg%n
            </Pattern>
        </encoder>

        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <FileNamePattern>d:/walknshine.%i.log.zip
            </FileNamePattern>
            <MinIndex>1</MinIndex>
            <MaxIndex>10</MaxIndex>
        </rollingPolicy>

        <triggeringPolicy
                class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>1MB</MaxFileSize>
        </triggeringPolicy>
    </appender>

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

Logging is happening in console but no file is being generated at the given path. My Maven dependencies are

  <!-- LOGGING DEPENDENCIES - LOG4J -->
    <dependency>
        <groupId>logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.0.6</version>
    </dependency>
    <dependency>
        <groupId>logback</groupId>
        <artifactId>logback-core</artifactId>
        <version>1.0.6</version>
    </dependency>
    <dependency>
        <groupId>logback</groupId>
        <artifactId>sl4j</artifactId>
        <version>1.6.0</version>
    </dependency>

I am using the logger as:

private static final Logger logger = LoggerFactory
        .getLogger(Main.class);

logger.info("Logged Message");

Please help me out here... what I am missing?

like image 789
user746458 Avatar asked Jul 16 '12 18:07

user746458


1 Answers

Are you sure your logback.xml file is in the correct location and is being detected? Try intentionally putting in errors in the config file, and seeing if you get messages in the console (Logback will do this automatically). Additionally, you can ask Logback to print out its internal status information.

public static void main(String[] args) {
    // assume SLF4J is bound to logback in the current environment
    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    // print logback's internal status
    StatusPrinter.print(lc);
    ...
}
like image 87
wolfcastle Avatar answered Oct 22 '22 09:10

wolfcastle