KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > util > log > CocoonTargetFactory


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

16 package org.apache.cocoon.util.log;
17
18 import org.apache.avalon.excalibur.logger.factory.FileTargetFactory;
19 import org.apache.avalon.framework.configuration.Configuration;
20 import org.apache.log.format.Formatter;
21
22 /**
23  * CocoonTargetFactory class.
24  *
25  * This factory is able to create different LogTargets specific to Cocoon
26  * according to the following configuration syntax:
27  *
28  * <pre>
29  * &lt;file id="foo"&gt;
30  * &lt;filename&gt;${context-key}/real-name/...&lt;/filename&gt;
31  * &lt;format type="raw|pattern|extended|xml|cocoon"&gt;pattern to be used if needed&lt;/format&gt;
32  * &lt;append&gt;true|false&lt;/append&gt;
33  * &lt;rotation type="revolving|unique" init="5" max="10"&gt;
34  * &lt;or&gt;
35  * &lt;size&gt;10000000&lt;/size&gt;
36  * &lt;time&gt;24:00:00&lt;/time&gt;
37  * &lt;time&gt;12:00:00&lt;/time&gt;
38  * &lt;/or&gt;
39  * &lt;/rotate&gt;
40  * &lt;/file&gt;
41  * </pre>
42  *
43  * <p>Some explanations about the Elements used in the configuration:</p>
44  * <dl>
45  * <dt>&lt;filename&gt;</dt>
46  * <dd>
47  * This denotes the name of the file to log to. It can be constructed
48  * out of entries in the passed Context object as ${context-key}.
49  * This element is required.
50  * </dd>
51  * <dt>&lt;format&gt;</dt>
52  * <dd>
53  * The type attribute of the pattern element denotes the type of
54  * Formatter to be used and according to it the pattern to use for.
55  * This elements defaults to:
56  * <p>
57  * %7.7{priority} %{time} [%8.8{category}] (%{uri}) %{thread}/%{class:short}: %{message}\\n%{throwable}
58  * </p>
59  * </dd>
60  * <dt>&lt;append&gt;<dt>
61  * <dd>
62  * If the log file should be deleted every time the logger is creates
63  * (normally at the start of the applcation) or not and thus the log
64  * entries will be appended. This elements defaults to false.
65  * </dd>
66  * <dt>&lt;rotation&gt;</dt>
67  * <dd>
68  * This is an optional element.
69  * The type attribute determines which FileStrategy to user
70  * (revolving=RevolvingFileStrategy, unique=UniqueFileStrategy).
71  * The required init and max attribute are used to determine the initial and
72  * maximum rotation to use on a type="revolving" attribute.
73  * </dd>
74  * <dt>&lt;or&gt;</dt>
75  * <dd>uses the OrRotateStrategy to combine the children</dd>
76  * <dt>&lt;size&gt;</dt>
77  * <dd>
78  * The number of bytes if no suffix used or kilo bytes (1024) if suffixed with
79  * 'k' or mega bytes (1024k) if suffixed with 'm' when a file rotation should
80  * occur. It doesn't make sense to specify more than one.
81  * </dd>
82  * <dt>&lt;time&gt;</dt>
83  * <dd>
84  * The time as HH:MM:SS when a rotation should occur. If you like to rotate
85  * a logfile more than once a day put an &lt;or&gt; element immediately after the
86  * &lt;rotation&gt; element and specify the times (and one size, too) inside the
87  * &lt;or&gt; element.
88  * </dd>
89  * </dl>
90  *
91  * @author <a HREF="mailto:giacomo@apache.org">Giacomo Pati</a>
92  * @version CVS $Id: CocoonTargetFactory.java 30932 2004-07-29 17:35:38Z vgritsenko $
93  */

94 public class CocoonTargetFactory
95     extends FileTargetFactory
96 {
97     //Format of default Cocoon formatter
98
private static final String JavaDoc CFORMAT =
99         "%7.7{priority} %{time} [%8.8{category}] (%{uri}) %{thread}/%{class:short}: %{message}\\n%{throwable}";
100
101     //Format of default Cocoon XML formatter
102
private static final String JavaDoc XFORMAT =
103         "priority time category uri thread class message throwable";
104
105     protected Formatter getFormatter(final Configuration conf) {
106         final String JavaDoc type = conf.getAttribute("type", "unknown");
107
108         if ("cocoon".equals(type)) {
109             int depth = conf.getAttributeAsInteger( "depth", 0 );
110             final CocoonLogFormatter formatter = new CocoonLogFormatter( depth );
111             final String JavaDoc format = conf.getValue(CFORMAT);
112             formatter.setFormat(format);
113             return formatter;
114         } else if ("xml".equals(type)) {
115             final XMLCocoonLogFormatter formatter = new XMLCocoonLogFormatter();
116             final String JavaDoc format = conf.getValue(XFORMAT);
117             formatter.setTypes(format);
118             return formatter;
119         }
120         // default formatter
121
return super.getFormatter(conf);
122     }
123 }
124
125
Popular Tags