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 package org.apache.cocoon.util.log;
18
19 import org.apache.avalon.excalibur.logger.factory.FileTargetFactory;
20 import org.apache.avalon.framework.configuration.Configuration;
21 import org.apache.log.format.Formatter;
22
23 /**
24 * CocoonTargetFactory class.
25 *
26 * This factory is able to create different LogTargets specific to Cocoon
27 * according to the following configuration syntax:
28 *
29 * <pre>
30 * <file id="foo">
31 * <filename>${context-key}/real-name/...</filename>
32 * <format type="raw|pattern|extended|xml|cocoon">pattern to be used if needed</format>
33 * <append>true|false</append>
34 * <rotation type="revolving|unique" init="5" max="10">
35 * <or>
36 * <size>10000000</size>
37 * <time>24:00:00</time>
38 * <time>12:00:00</time>
39 * </or>
40 * </rotate>
41 * </file>
42 * </pre>
43 *
44 * <p>Some explanations about the Elements used in the configuration:</p>
45 * <dl>
46 * <dt><filename></dt>
47 * <dd>
48 * This denotes the name of the file to log to. It can be constructed
49 * out of entries in the passed Context object as ${context-key}.
50 * This element is required.
51 * </dd>
52 * <dt><format></dt>
53 * <dd>
54 * The type attribute of the pattern element denotes the type of
55 * Formatter to be used and according to it the pattern to use for.
56 * This elements defaults to:
57 * <p>
58 * %7.7{priority} %{time} [%8.8{category}] (%{uri}) %{thread}/%{class:short}: %{message}\\n%{throwable}
59 * </p>
60 * </dd>
61 * <dt><append><dt>
62 * <dd>
63 * If the log file should be deleted every time the logger is creates
64 * (normally at the start of the applcation) or not and thus the log
65 * entries will be appended. This elements defaults to false.
66 * </dd>
67 * <dt><rotation></dt>
68 * <dd>
69 * This is an optional element.
70 * The type attribute determines which FileStrategy to user
71 * (revolving=RevolvingFileStrategy, unique=UniqueFileStrategy).
72 * The required init and max attribute are used to determine the initial and
73 * maximum rotation to use on a type="revolving" attribute.
74 * </dd>
75 * <dt><or></dt>
76 * <dd>uses the OrRotateStrategy to combine the children</dd>
77 * <dt><size></dt>
78 * <dd>
79 * The number of bytes if no suffix used or kilo bytes (1024) if suffixed with
80 * 'k' or mega bytes (1024k) if suffixed with 'm' when a file rotation should
81 * occur. It doesn't make sense to specify more than one.
82 * </dd>
83 * <dt><time></dt>
84 * <dd>
85 * The time as HH:MM:SS when a rotation should occur. If you like to rotate
86 * a logfile more than once a day put an <or> element immediately after the
87 * <rotation> element and specify the times (and one size, too) inside the
88 * <or> element.
89 * </dd>
90 * </dl>
91 *
92 * @deprecated This class will be removed in 2.2
93 * @author <a href="mailto:giacomo@apache.org">Giacomo Pati</a>
94 * @version CVS $Id: CocoonTargetFactory.java 433543 2006-08-22 06:22:54Z crossley $
95 */
96 public class CocoonTargetFactory
97 extends FileTargetFactory
98 {
99 //Format of default Cocoon formatter
100 private static final String CFORMAT =
101 "%7.7{priority} %{time} [%8.8{category}] (%{uri}) %{thread}/%{class:short}: %{message}\\n%{throwable}";
102
103 //Format of default Cocoon XML formatter
104 private static final String XFORMAT =
105 "priority time category uri thread class message throwable";
106
107 protected Formatter getFormatter(final Configuration conf) {
108 final String type = conf.getAttribute("type", "unknown");
109
110 if ("cocoon".equals(type)) {
111 int depth = conf.getAttributeAsInteger( "depth", 0 );
112 final CocoonLogFormatter formatter = new CocoonLogFormatter( depth );
113 final String format = conf.getValue(CFORMAT);
114 formatter.setFormat(format);
115 return formatter;
116 } else if ("xml".equals(type)) {
117 final XMLCocoonLogFormatter formatter = new XMLCocoonLogFormatter();
118 final String format = conf.getValue(XFORMAT);
119 formatter.setTypes(format);
120 return formatter;
121 }
122 // default formatter
123 return super.getFormatter(conf);
124 }
125 }
126