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 org.apache.logging.log4j.Logger; 020import org.apache.logging.log4j.core.LogEvent; 021import org.apache.logging.log4j.core.config.plugins.Plugin; 022import org.apache.logging.log4j.core.config.plugins.PluginAttribute; 023import org.apache.logging.log4j.core.config.plugins.PluginFactory; 024import org.apache.logging.log4j.status.StatusLogger; 025 026/** 027 * 028 */ 029@Plugin(name = "SizeBasedTriggeringPolicy", category = "Core", printObject = true) 030public class SizeBasedTriggeringPolicy implements TriggeringPolicy { 031 /** 032 * Allow subclasses access to the status logger without creating another instance. 033 */ 034 protected static final Logger LOGGER = StatusLogger.getLogger(); 035 036 /** 037 * Rollover threshold size in bytes. 038 */ 039 private static final long MAX_FILE_SIZE = 10 * 1024 * 1024; // let 10 MB the default max size 040 041 private final long maxFileSize; 042 043 private RollingFileManager manager; 044 045 /** 046 * Constructs a new instance. 047 */ 048 protected SizeBasedTriggeringPolicy() { 049 this.maxFileSize = MAX_FILE_SIZE; 050 } 051 052 /** 053 * Constructs a new instance. 054 * 055 * @param maxFileSize rollover threshold size in bytes. 056 */ 057 protected SizeBasedTriggeringPolicy(final long maxFileSize) { 058 this.maxFileSize = maxFileSize; 059 } 060 061 public long getMaxFileSize() { 062 return maxFileSize; 063 } 064 065 /** 066 * Initialize the TriggeringPolicy. 067 * @param aManager The RollingFileManager. 068 */ 069 @Override 070 public void initialize(final RollingFileManager aManager) { 071 this.manager = aManager; 072 } 073 074 075 /** 076 * Returns true if a rollover should occur. 077 * @param event A reference to the currently event. 078 * @return true if a rollover should take place, false otherwise. 079 */ 080 @Override 081 public boolean isTriggeringEvent(final LogEvent event) { 082 final boolean triggered = manager.getFileSize() > maxFileSize; 083 if (triggered) { 084 manager.getPatternProcessor().updateTime(); 085 } 086 return triggered; 087 } 088 089 @Override 090 public String toString() { 091 return "SizeBasedTriggeringPolicy(size=" + maxFileSize + ')'; 092 } 093 094 /** 095 * Create a SizeBasedTriggeringPolicy. 096 * @param size The size of the file before rollover is required. 097 * @return A SizeBasedTriggeringPolicy. 098 */ 099 @PluginFactory 100 public static SizeBasedTriggeringPolicy createPolicy(@PluginAttribute("size") final String size) { 101 102 final long maxSize = size == null ? MAX_FILE_SIZE : FileSize.parse(size, MAX_FILE_SIZE); 103 return new SizeBasedTriggeringPolicy(maxSize); 104 } 105 106}