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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.log4j.pattern;
20 import org.apache.log4j.spi.LoggingEvent;
22 import java.util.ArrayList;
23 import java.util.Iterator;
24 import java.util.List;
29 * The class implements the pre log4j 1.3 org.apache.log4j.helpers.PatternConverter
30 * contract by delegating to the log4j 1.3 pattern implementation.
36 public final class BridgePatternConverter
37 extends org.apache.log4j.helpers.PatternConverter {
41 private LoggingEventPatternConverter[] patternConverters;
44 * Field widths and alignment corresponding to pattern converters.
46 private FormattingInfo[] patternFields;
49 * Does pattern process exceptions.
51 private boolean handlesExceptions;
54 * Create a new instance.
55 * @param pattern pattern, may not be null.
57 public BridgePatternConverter(
58 final String pattern) {
60 handlesExceptions = false;
62 List converters = new ArrayList();
63 List fields = new ArrayList();
64 Map converterRegistry = null;
67 pattern, converters, fields, converterRegistry,
68 PatternParser.getPatternLayoutRules());
70 patternConverters = new LoggingEventPatternConverter[converters.size()];
71 patternFields = new FormattingInfo[converters.size()];
74 Iterator converterIter = converters.iterator();
75 Iterator fieldIter = fields.iterator();
77 while (converterIter.hasNext()) {
78 Object converter = converterIter.next();
80 if (converter instanceof LoggingEventPatternConverter) {
81 patternConverters[i] = (LoggingEventPatternConverter) converter;
82 handlesExceptions |= patternConverters[i].handlesThrowable();
84 patternConverters[i] =
85 new org.apache.log4j.pattern.LiteralPatternConverter("");
88 if (fieldIter.hasNext()) {
89 patternFields[i] = (FormattingInfo) fieldIter.next();
91 patternFields[i] = FormattingInfo.getDefault();
101 protected String convert(final LoggingEvent event) {
103 // code should be unreachable.
105 StringBuffer sbuf = new StringBuffer();
108 return sbuf.toString();
112 Format event to string buffer.
113 @param sbuf string buffer to receive formatted event, may not be null.
114 @param e event to format, may not be null.
116 public void format(final StringBuffer sbuf, final LoggingEvent e) {
117 for (int i = 0; i < patternConverters.length; i++) {
118 int startField = sbuf.length();
119 patternConverters[i].format(e, sbuf);
120 patternFields[i].format(startField, sbuf);
125 * Will return false if any of the conversion specifiers in the pattern
126 * handles {@link Exception Exceptions}.
127 * @return true if the pattern formats any information from exceptions.
129 public boolean ignoresThrowable() {
130 return !handlesExceptions;