Logback.xml configuration
Asked Answered
D

6

6

I am trying to configure the stout to save into a file. However, it is not saved to a file - do you have an idea why?. also - I want the log file name would be configurable inside the logback.xml something like {LOG_FILE_NAME} which will come from the cmd - is it possible?

This is my logback.xml:

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

<!-- For assistance related to logback-translator or configuration  -->
<!-- files in general, please contact the logback user mailing list -->
<!-- at http://www.qos.ch/mailman/listinfo/logback-user             -->
<!--                                                                -->
<!-- For professional support please see                            -->
<!--    http://www.qos.ch/shop/products/professionalSupport         -->
<!--                                                                -->
<configuration>
  <appender name="defaultLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <!--See also http://logback.qos.ch/manual/appenders.html#RollingFileAppender-->
    <File>sarit_test.log</File>
    <encoder>
      <pattern>%d{dd MMM yyyy HH:mm:ss.SSS} [%t] %-5p %x %F:%L - %m</pattern>
    </encoder>
    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"/>
    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
      <MaxFileSize>50000KB</MaxFileSize>
    </triggeringPolicy>
  </appender>
  <root level="INFO">
    <appender-ref ref="defaultLog"/>
  </root>
</configuration>
Dioptase answered 13/8, 2013 at 9:9 Comment(0)
P
2

For the first answer, Check here : https://github.com/abdulwaheed18/Slf4jTutorial

Second Answer : You have to use SIFT appender to take system parameters for file.

 <appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender">
    <!-- in the absence of the class attribute, it is assumed that the desired 
        discriminator type is ch.qos.logback.classic.sift.MDCBasedDiscriminator -->
    <discriminator>
        <key>FILE_NAME</key>
        <defaultValue>DEFAULT_FILE_NAME</defaultValue>
    </discriminator>
    <sift>
        <appender name="FILE-${FILE_NAME}"
            class="ch.qos.logback.core.rolling.RollingFileAppender">
            <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
                <evaluator> <!-- defaults to type ch.qos.logback.classic.boolex.JaninoEventEvaluator -->
                    <expression>return message.contains("Broken pipe");</expression>
                </evaluator>
                <OnMismatch>NEUTRAL</OnMismatch>
                <OnMatch>DENY</OnMatch>
            </filter>
            <File>${LOGDIR}/${FILE_NAME}.log</File>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <FileNamePattern>${LOGDIR}/${FILE_NAME}.%d{yyyy-MM-dd}.%i.log.gz
                </FileNamePattern>  <!-- keep 30 days' worth of history -->
                <MaxHistory>30</MaxHistory>
                <!-- Limit all logs size to 300MB -->
                <timeBasedFileNamingAndTriggeringPolicy
                    class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                    <!-- or whenever the file size reaches 10MB -->
                    <maxFileSize>10MB</maxFileSize>
                </timeBasedFileNamingAndTriggeringPolicy>
            </rollingPolicy>
            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                <Pattern>%date [%thread] %-5level %logger{36} - %msg%n</Pattern>
            </encoder>
        </appender>
    </sift>
</appender>
Prone answered 23/8, 2013 at 10:12 Comment(0)
P
2

The <File> node should be all in lower-case letters. So, instead of

 <File>sarit_test.log</File>

it should be

<file>sarit_test.log</file>

This was one of the mistakes that you have made, try to fix it (maybe it solves the issue) and next time, please append the error message to your question.

Pivot answered 14/5, 2014 at 11:28 Comment(0)
G
1

Properties can be set at command line like:

java -DUSER_HOME="/home/sebastien" MyApp2

You can also set these properties at system level. LogBack will first look at configuration-properties, then at java system propertes, then at system properties.

Use following configuration to write STDOUT to the console and a File:

<configuration>

    <!-- LOG_FILE_NAME: Java system properties set on the command line  -->
    <!-- LOG_HOME: Set at this line below -->
    <property name="LOG_HOME" value="/home/sebastien" />

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%date %-5level [%thread] - [%logger]- %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <file>${LOG_HOME}/${LOG_FILE_NAME}</file>
        <encoder>
            <pattern>%date %-5level [%thread] - [%logger] - %msg%n</pattern>
        </encoder>
    </appender>


    <root level="WARN">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </root>
</configuration>
Gilda answered 10/10, 2013 at 8:18 Comment(0)
P
1

One thing I see straight away is that you've only opened the <rollingPolicy> but the policy itself is empty. I bet that creates some problems.

For the second part of your question, yes, it is possible and the simplest way is probably to define a "constant" who's value will be set by a class in your application.

I've reworked your logback.xml to incorporate both suggestions above. I realize this is a year old now, but it could still be useful for other people looking for similar problems.

<configuration>
  <define name="logPath" class="path.to.your.Class.with.public.method.getLogPath">
    <key>getLogPath</key>
  </define>

  <appender name="defaultLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <!--See also http://logback.qos.ch/manual/appenders.html#RollingFileAppender-->
    <File>${logPath}${file.separator}sarit_test.log</File>
    <encoder>
      <pattern>%d{dd MMM yyyy HH:mm:ss.SSS} [%t] %-5p %x %F:%L - %m</pattern>
    </encoder>
    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"/>
      <fileNamePattern>${logPath}${file.separator}sarit_test.log.%i.zip</fileNamePattern>
      <minIndex>1</minIndex>
      <maxIndex>5</maxIndex>
    </rollingPolicy>
    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
      <MaxFileSize>50000KB</MaxFileSize>
    </triggeringPolicy>
  </appender>
  <root level="INFO">
    <appender-ref ref="defaultLog"/>
  </root>
</configuration>    
Pubilis answered 13/7, 2015 at 23:13 Comment(0)
E
0
<configuration>

    <!-- LOG_FILE_NAME: Java system properties set on the command line  -->
    <!-- LOG_HOME: Set at this line below -->
    <property name="LOG_HOME" value="/home/sebastien" />

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%date %-5level [%thread] - [%logger]- %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="FILE" class="ch.qos.logback.core.RollingFileAppender">
        <file>${LOG_HOME}/${LOG_FILE_NAME}</file>
        <encoder>
            <pattern>%date %-5level [%thread] - [%logger] - %msg%n</pattern>
        </encoder>
    </appender>


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

This will work fine to write in a file and console

Ekaterinodar answered 22/1, 2020 at 18:10 Comment(0)
M
-1

Certainly, here's a Logback XML configuration that fulfills your requirements for logging to both the console and a file, with daily and size-based rollovers, a maximum of 30 archived files retained, and logs kept for up to 180 days:

<configuration>

    <!-- Console Appender -->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg%n</pattern>
        </encoder>
    </appender>

    <!-- File Appender -->
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>/path/to/your/logfile.log</file> <!-- Replace with your desired file path -->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg%n</pattern>
        </encoder>

        <!-- Rolling Policy for Daily Rollover -->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>/path/to/your/logfile-%d{yyyy-MM-dd}.log</fileNamePattern> <!-- Daily rollover pattern -->
        </rollingPolicy>

        <!-- Trigger rollover when file size reaches 50 KB within the same day -->
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>50KB</maxFileSize>
        </triggeringPolicy>

        <!-- Set the maximum number of archived log files to keep -->
        <maxHistory>30</maxHistory>

        <!-- Delete log files older than 180 days -->
        <cleanHistoryOnStart>true</cleanHistoryOnStart>
        <maxDays>180</maxDays>
    </appender>

    <!-- Root Logger -->
    <root level="INFO">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="FILE" />
    </root>

</configuration>

In this configuration:

  • Log messages at INFO level and above will be logged to both the console and the specified log file.
  • The log file will roll over daily, with each day's log stored in a separate file.
  • If the log file reaches 50 KB within the same day, it will trigger an additional rollover for that day.
  • A maximum of 30 archived log files will be retained.
  • Log files older than 180 days will be deleted automatically when the application starts (<cleanHistoryOnStart>true</cleanHistoryOnStart>).

Make sure to replace /path/to/your/logfile.log with the actual path and filename where you want to save the log file.

Maurizio answered 5/9, 2023 at 10:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.