Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I configure logback to gzip my logs automatically?

i am using logback in my java web appliaction. here's my "logback.xml" file.

<?xml version="1.0" encoding="UTF-8"?> <configuration debug="false">      <property name="LOG_DIR" value="/home/ying/.jetty_logs/vehicle" />      <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">         <encoder>             <pattern>%date [%thread] %-5level %logger{36}[%L] - %msg%n</pattern>         </encoder>     </appender>      <appender name="LAST" class="ch.qos.logback.core.rolling.RollingFileAppender">         <file>${LOG_DIR}/last.log</file>         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">             <fileNamePattern>last.%d{yyyy-MM}.gz</fileNamePattern>             <maxHistory>24</maxHistory>         </rollingPolicy>         <encoder>             <pattern>%date:%msg%n</pattern>         </encoder>     </appender>      <logger name="org.springframework" level="WARN" />     <logger name="org.apache.shiro" level="WARN" />     <logger name="org.hibernate" level="WARN" />      <logger name="ying.car.interceptor.AutoLoginInterceptor" additivity="false" level="INFO">         <appender-ref ref="LAST" />     </logger>     <logger name="ying.car.controller.LoginController" additivity="false" level="INFO">         <appender-ref ref="LAST" />     </logger>     <logger name="ying.car.controller.LogoutController" additivity="false" level="INFO">         <appender-ref ref="LAST" />     </logger>      <root level="DEBUG">         <appender-ref ref="STDOUT" />     </root>  </configuration> 

today is june 1st 2013, all my old logs are overwrited and no *.gz created. somebody help me, please.

like image 760
Zhuo YING Avatar asked Jun 01 '13 00:06

Zhuo YING


People also ask

What is rolling policy in Logback?

Size-based rolling policy allows to rollover based on file on each log file. For example, we can rollover to a new file when the log file reaches 10 MB in size. The maxFileSize is used to specify the size of each file when it gets rolled over.

Where should Logback xml be stored?

In a Spring Boot application, you can put the Logback. xml file in the resources folder. If your Logback. xml file is outside the classpath, you need to point to its location using the Logback.

What is Appender in Logback?

The Logback architecture is comprised of three classes: Logger, Appender, and Layout. A Logger is a context for log messages. This is the class that applications interact with to create log messages. Appenders place log messages in their final destinations. A Logger can have more than one Appender.


1 Answers

Try to do like this, Hope it will work for you.

<appender name="FILE"     class="ch.qos.logback.core.rolling.RollingFileAppender">     <File>${LOGDIR}/filename.log</File>     <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">         <!-- rollover daily -->         <FileNamePattern>${LOGDIR}/file.%d{yyyy-MM-dd}.%i.log.gz         </FileNamePattern>         <!-- keep 30 days' worth of history -->         <MaxHistory>30</MaxHistory>         <!-- or whenever the file size reaches 10MB -->         <timeBasedFileNamingAndTriggeringPolicy             class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">             <maxFileSize>10MB</maxFileSize>         </timeBasedFileNamingAndTriggeringPolicy>     </rollingPolicy>     <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">         <Pattern>%date [%thread] %-5level %logger{36} - %msg%n         </Pattern>     </encoder> </appender> 

The above code will compress your file on the day basis or If the log file size exceeds 10MB.

Note : I have added "%i" in filePattern, It will iterate your file name as file1,file2 etc.

like image 83
Waheed Avatar answered Nov 09 '22 08:11

Waheed