View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.chukwa.inputtools.log4j;
20  
21  
22  import org.apache.log4j.*;
23  import org.apache.log4j.spi.LoggingEvent;
24  
25  public class OneLineLogLayout extends PatternLayout {
26  
27    char SEP = ' ';
28  
29    public String format(LoggingEvent evt) {
30  
31      String initial_s = super.format(evt);
32      StringBuilder sb = new StringBuilder();
33      for (int i = 0; i < initial_s.length() - 1; ++i) {
34        char c = initial_s.charAt(i);
35        if (c == '\n')
36          sb.append(SEP);
37        else
38          sb.append(c);
39      }
40      sb.append(SEP);
41      String[] s = evt.getThrowableStrRep();
42      if (s != null) {
43        int len = s.length;
44        for (int i = 0; i < len; i++) {
45          sb.append(s[i]);
46          sb.append(SEP);
47        }
48      }
49  
50      sb.append('\n');
51      return sb.toString();
52    }
53  
54    public boolean ignoresThrowable() {
55      return false;
56    }
57  
58    public static void main(String[] args) {
59      System.setProperty("line.separator", " ");
60      Logger l = Logger.getRootLogger();
61      l.removeAllAppenders();
62      Appender appender = new ConsoleAppender(new OneLineLogLayout());
63      appender.setName("console");
64      l.addAppender(appender);
65      l.warn("testing", new java.io.IOException("just kidding!"));
66  
67    }
68  
69  }