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