/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #if !defined(_LOG4CXX_ROLLING_TIME_BASED_ROLLING_POLICY_H) #define _LOG4CXX_ROLLING_TIME_BASED_ROLLING_POLICY_H #include #include #include namespace log4cxx { namespace rolling { /** * TimeBasedRollingPolicy is both easy to configure and quite * powerful. * *

In order to use TimeBasedRollingPolicy, the * FileNamePattern option must be set. It basically specifies the name of the * rolled log files. The value FileNamePattern should consist of * the name of the file, plus a suitably placed %d conversion * specifier. The %d conversion specifier may contain a date and * time pattern as specified by the {@link log4cxx::helpers::SimpleDateFormat} class. If * the date and time pattern is ommitted, then the default pattern of * "yyyy-MM-dd" is assumed. The following examples should clarify the point. * *

* * * * * * * * * * * * * * * * *
FileNamePattern valueRollover scheduleExample
/wombat/folder/foo.%dDaily rollover (at midnight). Due to the omission of the optional * time and date pattern for the %d token specifier, the default pattern * of "yyyy-MM-dd" is assumed, which corresponds to daily rollover. * During November 23rd, 2004, logging output will go to * the file /wombat/foo.2004-11-23. At midnight and for * the rest of the 24th, logging output will be directed to * /wombat/foo.2004-11-24. *
/wombat/foo.%d{yyyy-MM}.logRollover at the beginning of each month.During the month of October 2004, logging output will go to * /wombat/foo.2004-10.log. After midnight of October 31st * and for the rest of November, logging output will be directed to * /wombat/foo.2004-11.log. *
*

Automatic file compression

* TimeBasedRollingPolicy supports automatic file compression. * This feature is enabled if the value of the FileNamePattern option * ends with .gz or .zip. *

* * * * * * * * * * * *
FileNamePattern valueRollover scheduleExample
/wombat/foo.%d.gzDaily rollover (at midnight) with automatic GZIP compression of the * arcived files.During November 23rd, 2004, logging output will go to * the file /wombat/foo.2004-11-23. However, at midnight that * file will be compressed to become /wombat/foo.2004-11-23.gz. * For the 24th of November, logging output will be directed to * /wombat/folder/foo.2004-11-24 until its rolled over at the * beginning of the next day. *
* *

Decoupling the location of the active log file and the archived log files

*

The active file is defined as the log file for the current period * whereas archived files are thos files which have been rolled over * in previous periods. * *

By setting the ActiveFileName option you can decouple the location * of the active log file and the location of the archived log files. *

* * * * * * * * * * * * * * *
FileNamePattern valueActiveFileNameRollover scheduleExample
/wombat/foo.log.%d/wombat/foo.logDaily rollover.During November 23rd, 2004, logging output will go to * the file /wombat/foo.log. However, at midnight that file * will archived as /wombat/foo.log.2004-11-23. For the 24th * of November, logging output will be directed to * /wombat/folder/foo.log until its archived as * /wombat/foo.log.2004-11-24 at the beginning of the next * day. *
*

* If configuring programatically, do not forget to call {@link #activateOptions} * method before using this policy. Moreover, {@link #activateOptions} of * TimeBasedRollingPolicy must be called before calling * the {@link #activateOptions} method of the owning * RollingFileAppender. * * * */ class LOG4CXX_EXPORT TimeBasedRollingPolicy : public RollingPolicyBase, public TriggeringPolicy { DECLARE_LOG4CXX_OBJECT(TimeBasedRollingPolicy) BEGIN_LOG4CXX_CAST_MAP() LOG4CXX_CAST_ENTRY(TimeBasedRollingPolicy) LOG4CXX_CAST_ENTRY_CHAIN(RollingPolicyBase) LOG4CXX_CAST_ENTRY_CHAIN(TriggeringPolicy) END_LOG4CXX_CAST_MAP() private: /** * Time for next determination if time for rollover. */ log4cxx_time_t nextCheck; /** * File name at last rollover. */ LogString lastFileName; /** * Length of any file type suffix (.gz, .zip). */ int suffixLength; public: TimeBasedRollingPolicy(); void addRef() const; void releaseRef() const; void activateOptions(log4cxx::helpers::Pool& ); /** * Initialize the policy and return any initial actions for rolling file appender. * * @param file current value of RollingFileAppender.getFile(). * @param append current value of RollingFileAppender.getAppend(). * @param pool pool for any required allocations. * @return Description of the initialization, may be null to indicate * no initialization needed. * @throws SecurityException if denied access to log files. */ RolloverDescriptionPtr initialize( const LogString& file, const bool append, log4cxx::helpers::Pool& pool); /** * Prepare for a rollover. This method is called prior to * closing the active log file, performs any necessary * preliminary actions and describes actions needed * after close of current log file. * * @param activeFile file name for current active log file. * @param pool pool for any required allocations. * @return Description of pending rollover, may be null to indicate no rollover * at this time. * @throws SecurityException if denied access to log files. */ RolloverDescriptionPtr rollover(const LogString& activeFile, log4cxx::helpers::Pool& pool); /** * Determines if a rollover may be appropriate at this time. If * true is returned, RolloverPolicy.rollover will be called but it * can determine that a rollover is not warranted. * * @param appender A reference to the appender. * @param event A reference to the currently event. * @param filename The filename for the currently active log file. * @param fileLength Length of the file in bytes. * @return true if a rollover should occur. */ virtual bool isTriggeringEvent( Appender* appender, const log4cxx::spi::LoggingEventPtr& event, const LogString& filename, size_t fileLength); protected: log4cxx::pattern::PatternMap getFormatSpecifiers() const; }; LOG4CXX_PTR_DEF(TimeBasedRollingPolicy); } } #endif