Created
April 14, 2018 20:51
-
-
Save JonasGroeger/34bb004b30d18d8bc12642ba03141ac6 to your computer and use it in GitHub Desktop.
OnShutdownCompressingRollingFileAppender the compresses on logging context stop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package de.jonasgroeger.logback; | |
| import ch.qos.logback.core.LogbackException; | |
| import ch.qos.logback.core.rolling.RollingFileAppender; | |
| import ch.qos.logback.core.rolling.RollingPolicy; | |
| import ch.qos.logback.core.rolling.TimeBasedFileNamingAndTriggeringPolicy; | |
| import ch.qos.logback.core.rolling.TimeBasedRollingPolicy; | |
| import ch.qos.logback.core.rolling.helper.CompressionMode; | |
| import ch.qos.logback.core.rolling.helper.Compressor; | |
| import static ch.qos.logback.core.CoreConstants.UNBOUND_HISTORY; | |
| /** | |
| * The logging context around a Spring application is larger than the application itself. This is based on the | |
| * requirement that Spring wants to log things when it starts. | |
| * <p> | |
| * The default RollingFileAppender will only compress on rollover and on application start if the filename stays the | |
| * same. Since when deploying applications in a container environment like OpenShift, the hostname of a machine | |
| * always something like service-1-asidj. When using this hostname as the logfile name, the RollingFileAppender cannot | |
| * roll old logs on start. This is what the OnShutdownCompressingRollingFileAppender tries to solve. | |
| * <p> | |
| * The OnShutdownCompressingRollingFileAppender compresses the logs after Spring has shutdown and the logging context is | |
| * closing. This is when stop() is called. | |
| * | |
| * @author Jonas Gröger | |
| */ | |
| public class OnShutdownCompressingRollingFileAppender<E> extends RollingFileAppender<E> { | |
| @Override | |
| public void start() { | |
| RollingPolicy rollingPolicy = getRollingPolicy(); | |
| if (rollingPolicy == null) { | |
| throw new LogbackException("No RollingPolicy was set for the OnShutdownCompressingRollingFileAppender named " + getName()); | |
| } | |
| if (!(rollingPolicy instanceof TimeBasedRollingPolicy)) { | |
| throw new LogbackException("The OnShutdownCompressingRollingFileAppender only works with the TimeBasedRollingPolicy."); | |
| } | |
| if (((TimeBasedRollingPolicy) rollingPolicy).getMaxHistory() != UNBOUND_HISTORY) { | |
| throw new LogbackException( | |
| "The OnShutdownCompressingRollingFileAppender is not compatible with the <maxHistory> setting " + | |
| "since future application starts cannot find older logfiles based on the logfile name " + | |
| "pattern. The OnShutdownCompressingFileAppender will however work when you do not " + | |
| "redeploy your application." | |
| ); | |
| } | |
| if (((TimeBasedRollingPolicy) rollingPolicy).isCleanHistoryOnStart()) { | |
| throw new LogbackException( | |
| "The OnShutdownCompressingRollingFileAppender is not fully compatible with the <cleanHistoryOnStart> setting " + | |
| "since future application starts cannot find older logfiles based on the logfile name " + | |
| "pattern. The OnShutdownCompressingRollingFileAppender will however work when you do not " + | |
| "redeploy your application." | |
| ); | |
| } | |
| super.start(); | |
| } | |
| @Override | |
| public void stop() { | |
| super.stop(); | |
| TimeBasedRollingPolicy rollingPolicy = (TimeBasedRollingPolicy) getRollingPolicy(); | |
| TimeBasedFileNamingAndTriggeringPolicy triggeringPolicy = rollingPolicy.getTimeBasedFileNamingAndTriggeringPolicy(); | |
| CompressionMode compressionMode = rollingPolicy.getCompressionMode(); | |
| String logFile = getFile(); | |
| String logFileCompressed = triggeringPolicy.getCurrentPeriodsFileNameWithoutCompressionSuffix(); | |
| Compressor c = new Compressor(compressionMode); | |
| c.setContext(this.context); | |
| c.compress(logFile, logFileCompressed, logFileCompressed); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment