KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log > output > io > rotate > RotatingFileTarget


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.log.output.io.rotate;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import org.apache.log.format.Formatter;
22 import org.apache.log.output.io.FileTarget;
23
24 /**
25  * This is a basic Output log target that writes to rotating files.
26  *
27  * @author Peter Donald
28  * @author <a HREF="mailto:mcconnell@osm.net">Stephen McConnell</a>
29  * @author <a HREF="mailto:bh22351@i-one.at">Bernhard Huber</a>
30  */

31 public class RotatingFileTarget
32     extends FileTarget
33 {
34     ///Flag indicating whether or not file should be appended to
35
private boolean m_append;
36
37     ///The rotation strategy to be used.
38
private RotateStrategy m_rotateStrategy;
39
40     ///The file strategy to be used.
41
private FileStrategy m_fileStrategy;
42
43     /**
44      * Construct RotatingFileTarget object.
45      *
46      * @param formatter Formatter to be used
47      * @param rotateStrategy RotateStrategy to be used
48      * @param fileStrategy FileStrategy to be used
49      * @exception IOException if a file access or write related error occurs
50      */

51     public RotatingFileTarget( final Formatter formatter,
52                                final RotateStrategy rotateStrategy,
53                                final FileStrategy fileStrategy )
54         throws IOException JavaDoc
55     {
56         this( false, formatter, rotateStrategy, fileStrategy );
57     }
58
59     /**
60      * Construct RotatingFileTarget object.
61      *
62      * @param append true if file is to be appended to, false otherwise
63      * @param formatter Formatter to be used
64      * @param rotateStrategy RotateStrategy to be used
65      * @param fileStrategy FileStrategy to be used
66      * @exception IOException if a file access or write related error occurs
67      */

68     public RotatingFileTarget( final boolean append,
69                                final Formatter formatter,
70                                final RotateStrategy rotateStrategy,
71                                final FileStrategy fileStrategy )
72         throws IOException JavaDoc
73     {
74         super( null, append, formatter );
75
76         m_append = append;
77         m_rotateStrategy = rotateStrategy;
78         m_fileStrategy = fileStrategy;
79
80         rotate();
81     }
82
83     /**
84      * Rotates the file.
85      * @exception IOException if a file access or write related error occurs
86      */

87     protected synchronized void rotate()
88         throws IOException JavaDoc
89     {
90         close();
91
92         final File JavaDoc file = m_fileStrategy.nextFile();
93         setFile( file, m_append );
94         openFile();
95     }
96
97     /**
98      * Output the log message, and check if rotation is needed.
99      * @param data the date to write to the target
100      */

101     protected synchronized void write( final String JavaDoc data )
102     {
103         // if rotation is needed, close old File, create new File
104
if( m_rotateStrategy.isRotationNeeded( data, getFile() ) )
105         {
106             try
107             {
108                 rotate();
109
110                 m_rotateStrategy.reset();
111             }
112             catch( final IOException JavaDoc ioe )
113             {
114                 getErrorHandler().error( "Error rotating file", ioe, null );
115             }
116         }
117
118         // write the log message
119
super.write( data );
120     }
121 }
122
123
Popular Tags