001/** 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v1.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014package ch.qos.logback.core.rolling; 015 016import ch.qos.logback.core.FileAppender; 017import ch.qos.logback.core.rolling.helper.CompressionMode; 018import ch.qos.logback.core.rolling.helper.FileNamePattern; 019import ch.qos.logback.core.spi.ContextAwareBase; 020 021import static ch.qos.logback.core.util.Loader.isClassLoadable; 022 023/** 024 * Implements methods common to most, it not all, rolling policies. Currently 025 * such methods are limited to a compression mode getter/setter. 026 * 027 * @author Ceki Gülcü 028 */ 029public abstract class RollingPolicyBase extends ContextAwareBase implements RollingPolicy { 030 protected CompressionMode compressionMode = CompressionMode.NONE; 031 032 FileNamePattern fileNamePattern; 033 // fileNamePatternStr is always slashified, see setter 034 protected String fileNamePatternStr; 035 036 private FileAppender<?> parent; 037 038 // use to name files within zip file, i.e. the zipEntry 039 FileNamePattern zipEntryFileNamePattern; 040 private boolean started; 041 042 /** 043 * Given the FileNamePattern string, this method determines the compression mode 044 * depending on last letters of the fileNamePatternStr. Patterns ending with .gz 045 * imply GZIP compression, endings with '.zip' imply ZIP compression, endings with 046 * .xz imply XZ compression. Otherwise and by default, there is no compression. 047 * 048 */ 049 protected void determineCompressionMode() { 050 if (fileNamePatternStr.endsWith(CompressionMode.GZ_SUFFIX)) { 051 addInfo("Will use gz compression"); 052 compressionMode = CompressionMode.GZ; 053 } else if (fileNamePatternStr.endsWith(CompressionMode.ZIP_SUFFIX)) { 054 addInfo("Will use zip compression"); 055 compressionMode = CompressionMode.ZIP; 056 } else if (fileNamePatternStr.endsWith(CompressionMode.XZ_SUFFIX)) { 057 addInfo("Will use xz compression"); 058 compressionMode = CompressionMode.XZ; 059 } else { 060 addInfo("No compression will be used"); 061 compressionMode = CompressionMode.NONE; 062 } 063 } 064 065 /** 066 * If compression mode is XZ but the XZ library is missing, then fallback to GZ compression. 067 */ 068 protected void adjustCompressionModeAndFileNamePatternStrIfNecessary() { 069 if (compressionMode == compressionMode.XZ) { 070 boolean xzLibraryLoadable = isClassLoadable("org.tukaani.xz.XZOutputStream", getContext()); 071 if (!xzLibraryLoadable) { 072 addWarn("XZ library missing, falling back to GZ compression"); 073 compressionMode = CompressionMode.GZ; 074 fileNamePatternStr = replaceSuffix(fileNamePatternStr, CompressionMode.XZ_SUFFIX, CompressionMode.GZ_SUFFIX); 075 } 076 } 077 } 078 079 private String replaceSuffix(String input, String existingSuffix, String newSuffix) { 080 int existingSuffixLen = existingSuffix.length(); 081 if (input.endsWith(existingSuffix)) { 082 return input.substring(0, input.length() - existingSuffixLen) + newSuffix; 083 } else { 084 // unreachable code 085 throw new IllegalArgumentException("[" + input + "] should end with "+existingSuffix); 086 } 087 } 088 089 public void setFileNamePattern(String fnp) { 090 fileNamePatternStr = fnp; 091 } 092 093 public String getFileNamePattern() { 094 return fileNamePatternStr; 095 } 096 097 public CompressionMode getCompressionMode() { 098 return compressionMode; 099 } 100 101 public boolean isStarted() { 102 return started; 103 } 104 105 public void start() { 106 started = true; 107 } 108 109 public void stop() { 110 started = false; 111 } 112 113 public void setParent(FileAppender<?> appender) { 114 this.parent = appender; 115 } 116 117 public boolean isParentPrudent() { 118 return parent.isPrudent(); 119 } 120 121 public String getParentsRawFileProperty() { 122 return parent.rawFileProperty(); 123 } 124}