Coverage Report

Created: 2025-07-01 06:08

/src/logging-log4cxx/src/main/include/log4cxx/rolling/rollingfileappender.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 *
9
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
#if !defined(_LOG4CXX_ROLLING_ROLLING_FILE_APPENDER_H)
19
#define _LOG4CXX_ROLLING_ROLLING_FILE_APPENDER_H
20
21
#include <log4cxx/fileappender.h>
22
#include <log4cxx/spi/optionhandler.h>
23
#include <log4cxx/fileappender.h>
24
#include <log4cxx/rolling/triggeringpolicy.h>
25
#include <log4cxx/rolling/rollingpolicy.h>
26
#include <log4cxx/rolling/action.h>
27
28
namespace LOG4CXX_NS
29
{
30
namespace rolling
31
{
32
33
34
/**
35
 * <code>RollingFileAppender</code> extends {@link log4cxx::FileAppender} to backup the log files
36
 * depending on {@link log4cxx::rolling::RollingPolicy RollingPolicy} and {@link log4cxx::rolling::TriggeringPolicy TriggeringPolicy}.
37
 * <p>
38
 * To be of any use, a <code>RollingFileAppender</code> instance must have both
39
 * a <code>RollingPolicy</code> and a <code>TriggeringPolicy</code> set up.
40
 * However, if its <code>RollingPolicy</code> also implements the
41
 * <code>TriggeringPolicy</code> interface, then only the former needs to be
42
 * set up. For example, {@link log4cxx::rolling::TimeBasedRollingPolicy TimeBasedRollingPolicy} acts both as a
43
 * <code>RollingPolicy</code> and a <code>TriggeringPolicy</code>.
44
 *
45
 * <p><code>RollingFileAppender</code> can be configured programattically or
46
 * using {@link log4cxx::xml::DOMConfigurator}.
47
 * The following is a sample configuration file.
48
49
<pre>&lt;?xml version="1.0" encoding="UTF-8" ?>
50
&lt;!DOCTYPE log4j:configuration>
51
  &lt;log4j:configuration debug="true">
52
  &lt;appender name="ROLL" class="org.apache.log4j.rolling.RollingFileAppender">
53
    <b>&lt;rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
54
      &lt;param name="FileNamePattern" value="/wombat/foo.%d{yyyy-MM}.gz"/>
55
    &lt;/rollingPolicy></b>
56
    &lt;layout class="org.apache.log4j.PatternLayout">
57
      &lt;param name="ConversionPattern" value="%c{1} - %m%n"/>
58
    &lt;/layout>
59
  &lt;/appender>
60
61
  &lt;root>
62
    &lt;appender-ref ref="ROLL"/>
63
  &lt;/root>
64
&lt;/log4j:configuration>
65
</pre>
66
67
 *<p>This configuration file specifies a monthly rollover schedule including
68
 * automatic compression of the archived files. See
69
 * {@link TimeBasedRollingPolicy} for more details.
70
 *
71
 *
72
 * Note: Do *not* set the option <code>Append</code> to <code>false</code>.
73
 * Rolling over files is only relevant when you are appending.
74
 *
75
 */
76
class LOG4CXX_EXPORT RollingFileAppender : public FileAppender
77
{
78
    DECLARE_LOG4CXX_OBJECT(RollingFileAppender)
79
0
    BEGIN_LOG4CXX_CAST_MAP()
80
0
    LOG4CXX_CAST_ENTRY(RollingFileAppender)
81
0
    LOG4CXX_CAST_ENTRY_CHAIN(FileAppender)
82
0
    END_LOG4CXX_CAST_MAP()
83
  protected:
84
    struct RollingFileAppenderPriv;
85
86
  public:
87
    RollingFileAppender();
88
    RollingFileAppender( std::unique_ptr<RollingFileAppenderPriv> priv );
89
90
    /** Returns the value of the <b>MaxBackupIndex</b> option. */
91
    int getMaxBackupIndex() const;
92
93
    /** Get the maximum size that the output file is allowed to reach before being rolled over to backup files. */
94
    size_t getMaximumFileSize() const;
95
96
97
    /**
98
    Set the maximum number of backup files to keep around.
99
100
    <p>The <b>MaxBackupIndex</b> option determines how many backup
101
     files are kept before the oldest is erased. This option takes
102
     a positive integer value. If set to zero, then there will be no
103
     backup files and the log file will be truncated when it reaches <code>MaxFileSize</code>.
104
    */
105
    void setMaxBackupIndex( int maxBackupIndex );
106
107
    /**
108
    Set the maximum size that the output file is allowed to reach before being rolled over to backup files.
109
110
    <p>In configuration files, the <b>MaxFileSize</b> option takes an
111
     long integer in the range 0 - 2^63. You can specify the value with the suffixes "KB", "MB" or "GB" so that the integer is
112
     interpreted being expressed respectively in kilobytes, megabytes
113
     or gigabytes. For example, the value "10KB" will be interpreted as 10240.
114
    */
115
    void setMaxFileSize( const LogString& value );
116
117
    void setMaximumFileSize( size_t value );
118
119
    /**
120
       The <b>DatePattern</b> takes a string in the same format as
121
       expected by {@link log4cxx::helpers::SimpleDateFormat SimpleDateFormat}. This options determines the
122
       rollover schedule.
123
     */
124
    void setDatePattern(const LogString& pattern);
125
126
    LogString makeFileNamePattern(const LogString& datePattern);
127
128
    /**
129
    \copybrief FileAppender::setOption()
130
131
    Supported options | Supported values | Default value
132
    :-------------- | :----------------: | :---------------:
133
    FileDatePattern | (\ref dateChars "1") | -
134
    MaxBackupIndex | 1-12 | 0
135
    MaxFileSize | (\ref fileSz "2") | 10 MB
136
137
    \anchor dateChars (1) A pattern compatible with
138
      java.text.SimpleDateFormat, "ABSOLUTE", "DATE" or "ISO8601".
139
      For example, "HH:mm:ss,SSS", "dd MMM yyyy HH:mm:ss,SSS" or "DATE".
140
141
    \anchor fileSz (2) An integer in the range 0 - 2^63.
142
     You can specify the value with the suffixes "KB", "MB" or "GB" so that the integer is
143
     interpreted being expressed respectively in kilobytes, megabytes
144
     or gigabytes. For example, the value "10KB" will be interpreted as 10240.
145
146
    \sa FileAppender::setOption()
147
    */
148
    void setOption( const LogString& option, const LogString& value ) override;
149
150
    /**
151
    \copybrief FileAppender::activateOptions()
152
153
    \sa FileAppender::activateOptions()
154
    */
155
    void activateOptions(helpers::Pool& pool ) override;
156
157
    /**
158
       Implements the configured roll over behaviour.
159
160
       <p>If <code>MaxBackupIndex</code> is positive, then files
161
       {<code>File.1</code>, ..., <code>File.MaxBackupIndex -1</code>}
162
       are renamed to {<code>File.2</code>, ...,
163
       <code>File.MaxBackupIndex</code>}. Moreover, <code>File</code> is
164
       renamed <code>File.1</code> and closed. A new <code>File</code> is
165
       created to receive further log output.
166
167
       <p>If <code>MaxBackupIndex</code> is equal to zero, then the
168
       <code>File</code> is truncated with no backup files created.
169
170
     */
171
    bool rollover(LOG4CXX_NS::helpers::Pool& p);
172
173
  protected:
174
175
    /**
176
     Actual writing occurs here.
177
    */
178
    void subAppend(const spi::LoggingEventPtr& event, helpers::Pool& p) override;
179
180
    bool rolloverInternal(LOG4CXX_NS::helpers::Pool& p);
181
182
  public:
183
    /**
184
     * The policy that implements the scheme for rolling over a log file.
185
     */
186
    RollingPolicyPtr getRollingPolicy() const;
187
188
    /**
189
     * The policy that determine when to trigger a log file rollover.
190
     */
191
    TriggeringPolicyPtr getTriggeringPolicy() const;
192
193
    /**
194
     * Use \c policy as the scheme for rolling over log files.
195
     *
196
     * Where the \c policy also implements
197
     * {@link TriggeringPolicy}, then \c policy
198
     * will be used to determine when to trigger a log file rollover.
199
     */
200
    void setRollingPolicy(const RollingPolicyPtr& policy);
201
202
    /**
203
     * Use \c policy to determine when to trigger a log file rollover.
204
     */
205
    void setTriggeringPolicy(const TriggeringPolicyPtr& policy);
206
207
  public:
208
    /**
209
      * Close appender.  Waits for any asynchronous file compression actions to be completed.
210
    */
211
    void close() override;
212
213
  protected:
214
    /**
215
       Returns an OutputStreamWriter when passed an OutputStream.  The
216
       encoding used will depend on the value of the
217
       <code>encoding</code> property.  If the encoding value is
218
       specified incorrectly the writer will be opened using the default
219
       system encoding (an error message will be printed to the loglog.
220
     @param os output stream, may not be null.
221
     @return new writer.
222
     */
223
    helpers::WriterPtr createWriter(helpers::OutputStreamPtr& os) override;
224
225
  public:
226
    /**
227
     * Get byte length of current active log file.
228
     * @return byte length of current active log file.
229
     */
230
    size_t getFileLength() const;
231
232
    /**
233
     * Increments estimated byte length of current active log file.
234
     * @param increment additional bytes written to log file.
235
     */
236
    void incrementFileLength(size_t increment);
237
238
};
239
240
LOG4CXX_PTR_DEF(RollingFileAppender);
241
242
}
243
}
244
245
#endif
246