001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017package org.apache.logging.log4j.core.appender.rolling; 018 019import java.text.NumberFormat; 020import java.text.ParseException; 021import java.util.Locale; 022import java.util.regex.Matcher; 023import java.util.regex.Pattern; 024 025import org.apache.logging.log4j.Logger; 026import org.apache.logging.log4j.core.LogEvent; 027import org.apache.logging.log4j.core.config.plugins.Plugin; 028import org.apache.logging.log4j.core.config.plugins.PluginAttribute; 029import org.apache.logging.log4j.core.config.plugins.PluginFactory; 030import org.apache.logging.log4j.status.StatusLogger; 031 032/** 033 * 034 */ 035@Plugin(name = "SizeBasedTriggeringPolicy", category = "Core", printObject = true) 036public class SizeBasedTriggeringPolicy implements TriggeringPolicy { 037 /** 038 * Allow subclasses access to the status logger without creating another instance. 039 */ 040 protected static final Logger LOGGER = StatusLogger.getLogger(); 041 042 private static final long KB = 1024; 043 private static final long MB = KB * KB; 044 private static final long GB = KB * MB; 045 046 /** 047 * Rollover threshold size in bytes. 048 */ 049 private static final long MAX_FILE_SIZE = 10 * 1024 * 1024; // let 10 MB the default max size 050 051 052 /** 053 * Pattern for string parsing. 054 */ 055 private static final Pattern VALUE_PATTERN = 056 Pattern.compile("([0-9]+([\\.,][0-9]+)?)\\s*(|K|M|G)B?", Pattern.CASE_INSENSITIVE); 057 058 private final long maxFileSize; 059 060 private RollingFileManager manager; 061 062 /** 063 * Constructs a new instance. 064 */ 065 protected SizeBasedTriggeringPolicy() { 066 this.maxFileSize = MAX_FILE_SIZE; 067 } 068 069 /** 070 * Constructs a new instance. 071 * 072 * @param maxFileSize rollover threshold size in bytes. 073 */ 074 protected SizeBasedTriggeringPolicy(final long maxFileSize) { 075 this.maxFileSize = maxFileSize; 076 } 077 078 public long getMaxFileSize() { 079 return maxFileSize; 080 } 081 082 /** 083 * Initialize the TriggeringPolicy. 084 * @param manager The RollingFileManager. 085 */ 086 @Override 087 public void initialize(final RollingFileManager manager) { 088 this.manager = manager; 089 } 090 091 092 /** 093 * Returns true if a rollover should occur. 094 * @param event A reference to the currently event. 095 * @return true if a rollover should take place, false otherwise. 096 */ 097 @Override 098 public boolean isTriggeringEvent(final LogEvent event) { 099 final boolean triggered = manager.getFileSize() > maxFileSize; 100 if (triggered) { 101 manager.getPatternProcessor().updateTime(); 102 } 103 return triggered; 104 } 105 106 @Override 107 public String toString() { 108 return "SizeBasedTriggeringPolicy(size=" + maxFileSize + ')'; 109 } 110 111 /** 112 * Create a SizeBasedTriggeringPolicy. 113 * @param size The size of the file before rollover is required. 114 * @return A SizeBasedTriggeringPolicy. 115 */ 116 @PluginFactory 117 public static SizeBasedTriggeringPolicy createPolicy(@PluginAttribute("size") final String size) { 118 119 final long maxSize = size == null ? MAX_FILE_SIZE : valueOf(size); 120 return new SizeBasedTriggeringPolicy(maxSize); 121 } 122 123 /** 124 * Converts a string to a number of bytes. Strings consist of a floating point value followed by 125 * K, M, or G for kilobytes, megabytes, gigabytes, respectively. The 126 * abbreviations KB, MB, and GB are also accepted. Matching is case insensitive. 127 * 128 * @param string The string to convert 129 * @return The Bytes value for the string 130 */ 131 private static long valueOf(final String string) { 132 final Matcher matcher = VALUE_PATTERN.matcher(string); 133 134 // Valid input? 135 if (matcher.matches()) { 136 try { 137 // Get double precision value 138 final long value = NumberFormat.getNumberInstance(Locale.getDefault()).parse( 139 matcher.group(1)).longValue(); 140 141 // Get units specified 142 final String units = matcher.group(3); 143 144 if (units.isEmpty()) { 145 return value; 146 } else if (units.equalsIgnoreCase("K")) { 147 return value * KB; 148 } else if (units.equalsIgnoreCase("M")) { 149 return value * MB; 150 } else if (units.equalsIgnoreCase("G")) { 151 return value * GB; 152 } else { 153 LOGGER.error("Units not recognized: " + string); 154 return MAX_FILE_SIZE; 155 } 156 } catch (final ParseException e) { 157 LOGGER.error("Unable to parse numeric part: " + string, e); 158 return MAX_FILE_SIZE; 159 } 160 } 161 LOGGER.error("Unable to parse bytes: " + string); 162 return MAX_FILE_SIZE; 163 } 164 165}