Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logback.xml configuration

Tags:

logback

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>
like image 891
Sarit Avatar asked Aug 13 '13 09:08

Sarit


People also ask

How do I setup a Logback XML file?

In a Logback. xml file, all the configuration options are enclosed within the <configuration> root element. In the root element, you can set the debug=true attribute to inspect Logback's internal status. You can also configure auto scanning of the configuration file by setting the scan=true attribute.

Where can I find Logback xml?

xml or logback. xml can be located directly under any folder declared in the class path.

How do you specify Logback xml in application properties?

xml , you can use <springProperty> to access properties from Spring's environment including those configured in application. properties . This is described in the documentation: The tag allows you to surface properties from the Spring Environment for use within Logback.


3 Answers

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>
like image 134
Waheed Avatar answered Nov 24 '22 00:11

Waheed


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.

like image 20
Mr.Q Avatar answered Nov 23 '22 23:11

Mr.Q


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>
like image 22
Dimitri Dewaele Avatar answered Nov 24 '22 01:11

Dimitri Dewaele