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>
Sarit
  • 101
  • 1
  • 3
  • 6

5 Answers5

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>
Michael M.
  • 10,486
  • 9
  • 18
  • 34
Waheed
  • 1,835
  • 15
  • 21
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.

Marko Zajc
  • 307
  • 3
  • 14
Mr.Q
  • 4,316
  • 3
  • 43
  • 40
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>
Dimitri Dewaele
  • 10,311
  • 21
  • 80
  • 127
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>    
Nadar
  • 152
  • 1
  • 1
  • 11
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