001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.util.log;
018:
019: import org.apache.avalon.excalibur.logger.factory.FileTargetFactory;
020: import org.apache.avalon.framework.configuration.Configuration;
021: import org.apache.log.format.Formatter;
022:
023: /**
024: * CocoonTargetFactory class.
025: *
026: * This factory is able to create different LogTargets specific to Cocoon
027: * according to the following configuration syntax:
028: *
029: * <pre>
030: * <file id="foo">
031: * <filename>${context-key}/real-name/...</filename>
032: * <format type="raw|pattern|extended|xml|cocoon">pattern to be used if needed</format>
033: * <append>true|false</append>
034: * <rotation type="revolving|unique" init="5" max="10">
035: * <or>
036: * <size>10000000</size>
037: * <time>24:00:00</time>
038: * <time>12:00:00</time>
039: * </or>
040: * </rotate>
041: * </file>
042: * </pre>
043: *
044: * <p>Some explanations about the Elements used in the configuration:</p>
045: * <dl>
046: * <dt><filename></dt>
047: * <dd>
048: * This denotes the name of the file to log to. It can be constructed
049: * out of entries in the passed Context object as ${context-key}.
050: * This element is required.
051: * </dd>
052: * <dt><format></dt>
053: * <dd>
054: * The type attribute of the pattern element denotes the type of
055: * Formatter to be used and according to it the pattern to use for.
056: * This elements defaults to:
057: * <p>
058: * %7.7{priority} %{time} [%8.8{category}] (%{uri}) %{thread}/%{class:short}: %{message}\\n%{throwable}
059: * </p>
060: * </dd>
061: * <dt><append><dt>
062: * <dd>
063: * If the log file should be deleted every time the logger is creates
064: * (normally at the start of the applcation) or not and thus the log
065: * entries will be appended. This elements defaults to false.
066: * </dd>
067: * <dt><rotation></dt>
068: * <dd>
069: * This is an optional element.
070: * The type attribute determines which FileStrategy to user
071: * (revolving=RevolvingFileStrategy, unique=UniqueFileStrategy).
072: * The required init and max attribute are used to determine the initial and
073: * maximum rotation to use on a type="revolving" attribute.
074: * </dd>
075: * <dt><or></dt>
076: * <dd>uses the OrRotateStrategy to combine the children</dd>
077: * <dt><size></dt>
078: * <dd>
079: * The number of bytes if no suffix used or kilo bytes (1024) if suffixed with
080: * 'k' or mega bytes (1024k) if suffixed with 'm' when a file rotation should
081: * occur. It doesn't make sense to specify more than one.
082: * </dd>
083: * <dt><time></dt>
084: * <dd>
085: * The time as HH:MM:SS when a rotation should occur. If you like to rotate
086: * a logfile more than once a day put an <or> element immediately after the
087: * <rotation> element and specify the times (and one size, too) inside the
088: * <or> element.
089: * </dd>
090: * </dl>
091: *
092: * @deprecated This class will be removed in 2.2
093: * @author <a href="mailto:giacomo@apache.org">Giacomo Pati</a>
094: * @version CVS $Id: CocoonTargetFactory.java 433543 2006-08-22 06:22:54Z crossley $
095: */
096: public class CocoonTargetFactory extends FileTargetFactory {
097: //Format of default Cocoon formatter
098: private static final String CFORMAT = "%7.7{priority} %{time} [%8.8{category}] (%{uri}) %{thread}/%{class:short}: %{message}\\n%{throwable}";
099:
100: //Format of default Cocoon XML formatter
101: private static final String XFORMAT = "priority time category uri thread class message throwable";
102:
103: protected Formatter getFormatter(final Configuration conf) {
104: final String type = conf.getAttribute("type", "unknown");
105:
106: if ("cocoon".equals(type)) {
107: int depth = conf.getAttributeAsInteger("depth", 0);
108: final CocoonLogFormatter formatter = new CocoonLogFormatter(
109: depth);
110: final String format = conf.getValue(CFORMAT);
111: formatter.setFormat(format);
112: return formatter;
113: } else if ("xml".equals(type)) {
114: final XMLCocoonLogFormatter formatter = new XMLCocoonLogFormatter();
115: final String format = conf.getValue(XFORMAT);
116: formatter.setTypes(format);
117: return formatter;
118: }
119: // default formatter
120: return super.getFormatter(conf);
121: }
122: }
|