Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way in logback.xml to specify file log destination through classpath:, without absolute path?

I've in my logback.xml configuration file this appender:

<appender name="FILE"
            class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>classpath:addressbookLog.log</file>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
      <Pattern>%d{dd MMM yyyy;HH:mm:ss} %-5level %logger{36} - %msg%n
      </Pattern>
    </encoder>
    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
      <FileNamePattern>classpath:addressbookLog.%i.log.zip</FileNamePattern>
      <MinIndex>1</MinIndex>
      <MaxIndex>10</MaxIndex>
    </rollingPolicy>

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

so that I specify path to file in which to print logs in a relative way through classpath, but it doesn't work, no file addressbookLog.log is created and written. It only works with absolute paths like /home/andrea/.../resources/addressbookLog.log Have you any ideas on how to make it work with classpath?

like image 691
andPat Avatar asked May 10 '13 10:05

andPat


People also ask

How do I specify the path for Logback xml?

You may specify the location of the default configuration file with a system property named "logback. configurationFile". The value of this property can be a URL, a resource on the class path or a path to a file external to the application.

How do I use application properties in Logback xml?

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.

Which of the following file is used by Logback logging system?

properties file will be defined as properties in the logback. xml file.


1 Answers

The Chapter 3: Logback configuration: Variable substitution told us the various ways to refer to the variable defined outside, e.g. system properties and classpath.

The significant configuration is creating a separate file that will contain all the variables. We can make a reference to a resource on the class path instead of a file as well. e.g.

The logback.xml

<configuration>

  <property resource="resource1.properties" />

  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
     <!-- Here we can refer to the variable 
      defined at the resource1.properties -->
     <file>${USER_HOME}/myApp.log</file>
     <encoder>
       <pattern>%msg%n</pattern>
     </encoder>
   </appender>

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

The external properties file (resource1.properties)

USER_HOME=/path/to/somewhere

Please note that the resource1.properties is a resource which available at the classpath.

You may refer to the full version at Chapter 3: Logback configuration: Variable substitution. I hope this may help.

like image 54
Charlee Chitsuk Avatar answered Sep 28 '22 08:09

Charlee Chitsuk