logback how to set destination folder for log files

做~自己de王妃 提交于 2019-12-07 01:31:01

问题


Is there a way to set a single destination folder, such that I can specify where all log files should be created rather than having to set it on an appender by appender basis?


回答1:


You can define a property in the logback configuration file an use it as below

<configuration>

  <property name="USER_HOME" value="/home/sebastien" />

  <appender name="SPRING_LOGS" class="ch.qos.logback.core.FileAppender">
    <file>${USER_HOME}/spring.log</file>
    <encoder>
      <pattern>%msg%n</pattern>
    </encoder>
  </appender>

  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>${USER_HOME}/myApp.log</file>
    <encoder>
      <pattern>%msg%n</pattern>
    </encoder>
  </appender>

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

Note that logback can read the variables from System properties or a separate properties file too. Follow the manual for more details.




回答2:


I've wasted a lot of time configuring Logback to work with Spring Boot and I'd like to share my configuration, hoping to save other people from wasting their time.

My example is similar to Andy Dufresne's above, with one key difference - no <property> tag. This was really important in my case because if you include the <property name="logs_dir" value="." /> you won't be able to override it using system properties, which I wanted to do like this:

java -jar -Dlogs_dir=~/newLogsDir yourApp.jar 

Also note the default value is set inside the path variable - ${logs_dir:-.}. Hope this helps:

<?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>%-20(%d{yyyy-MM-dd HH:mm:ss} %highlight([%-5level])) %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${logs_dir:-.}/system.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- rollover monthly -->
            <fileNamePattern>system-%d{yyyy-MM}.log.zip</fileNamePattern>
            <maxHistory>12</maxHistory>
            <totalSizeCap>3GB</totalSizeCap>
        </rollingPolicy>
        <encoder>
            <pattern>%-26(%d [%-5level]) %logger{35} - %msg%n</pattern>
        </encoder>
    </appender>

</configuration>



回答3:


I have a spring boot app, and I run the fat .jar as a systemd service.

I struggled a couple of hours on how to set the LOG_PATH relative to the user's home dir.

Here is what worked for me:

  • in application.properties I have:

logging.path=${HOME}/attach_logs

  • in logback-spring.xml I have:

<springProperty scope="context" name="LOG_PATH" source="logging.path"/>

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_PATH}/console.log</file>

References:

Getting the user home path in application.properties in Spring Boot

Accessing the application properties in logback.xml

Spring boot logging path

logback how to set destination folder for log files



来源:https://stackoverflow.com/questions/27764071/logback-how-to-set-destination-folder-for-log-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!