KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > core > log > LogLayout


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Coridan.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46
47 package org.mr.core.log;
48
49 import java.text.DateFormat JavaDoc;
50 import java.text.SimpleDateFormat JavaDoc;
51 import java.util.Date JavaDoc;
52 import java.util.StringTokenizer JavaDoc;
53
54 import org.apache.log4j.Layout;
55 import org.apache.log4j.spi.LoggingEvent;
56
57 import org.mr.core.util.SystemTime;
58
59
60
61
62 public class LogLayout extends Layout {
63     
64   //~ Methods ==================================================================
65

66   public String JavaDoc format(LoggingEvent logEvent) {
67     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
68     sb.append(dateFormat.format(new Date JavaDoc(SystemTime.currentTimeMillis())));
69     sb.append(" [");
70     sb.append(logEvent.getThreadName());
71     sb.append("] ");
72         
73         String JavaDoc[] decorator = getDecoratorForLevel(logEvent.getLevel());
74         sb.append(decorator[0]);
75     sb.append(logEvent.getLevel());
76         sb.append(decorator[1]);
77     sb.append(" ");
78     sb.append(logEvent.getLoggerName());
79     sb.append(MESSAGE_DELIMS);
80     Object JavaDoc messageObj = logEvent.getMessage();
81     String JavaDoc message = (messageObj == null) ? "" : messageObj.toString();
82     /*
83     StringTokenizer tokenizer = new StringTokenizer(message, MESSAGE_DELIMS);
84
85     while (tokenizer.hasMoreTokens()) {
86       sb.append(margin);
87       sb.append(tokenizer.nextToken());
88       sb.append(MESSAGE_DELIMS);
89     }
90     */

91     //sb.append(MESSAGE_DELIMS);
92
sb.append(message);
93    // sb.append(MESSAGE_DELIMS);
94
if(useSeparator())
95         sb.append(getLogSeparator());
96     return sb.toString();
97   }
98
99   public boolean useSeparator() {
100     return useSeparator;
101   }
102
103   public void setUseSeparator(boolean useSeparator) {
104     this.useSeparator = useSeparator;
105   }
106
107   public String JavaDoc getLogSeparator() {
108     return logSeparator;
109   }
110
111   
112     public void setDecorators(String JavaDoc decorators){
113         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(decorators, "|");
114         while(st.hasMoreTokens()){
115             try {
116         String JavaDoc tok = st.nextToken();
117         int ind = tok.indexOf(':');
118         String JavaDoc type = tok.substring(0, ind);
119         String JavaDoc dec = tok.substring(ind+1);
120         int half = dec.length()/2;
121         String JavaDoc[] both = new String JavaDoc[2];
122         both[0] = dec.substring(0,half);
123         both[1] = dec.substring(half);
124         if("debug".equalsIgnoreCase(type)){
125             debugDecorators = both;
126         }
127         if("info".equalsIgnoreCase(type)){
128             infoDecorators = both;
129         }
130         if("warn".equalsIgnoreCase(type)){
131             warnDecorators = both;
132         }
133         if("error".equalsIgnoreCase(type)){
134             errorDecorators = both;
135         }
136         if("fatal".equalsIgnoreCase(type)){
137             fatalDecorators = both;
138         }
139       } catch (Exception JavaDoc e) {
140         //System.err.println(BLZLayout.class.getName()+": ");
141
}
142         }
143     }
144
145
146   public boolean ignoresThrowable() {
147     return true;
148   }
149
150   public void activateOptions() {
151   }
152
153     private String JavaDoc[] getDecoratorForLevel(org.apache.log4j.Level level){
154         switch (level.toInt()) {
155       case 50000 : return fatalDecorators;
156             case 40000 : return errorDecorators;
157             case 30000 : return warnDecorators;
158             case 20000 : return infoDecorators;
159             case 10000 : return debugDecorators;
160       default :return EMPTY_DECORATOR;
161     }
162     }
163     /*
164   private static String createMarginString() {
165     int length = 4;
166     StringBuffer sb = new StringBuffer();
167
168     for (int i = 0; i < (length + 1); i++) {
169       sb.append(" ");
170     }
171
172     return sb.toString();
173   }
174   */

175
176   //~ Instance variables =======================================================
177

178   protected boolean useSeparator = true;
179     protected boolean useDecorators = false;
180   protected String JavaDoc logSeparator = "\r\n___________________________________________________________________________________\r\n";
181   
182   protected String JavaDoc[] fatalDecorators = EMPTY_DECORATOR;
183     protected String JavaDoc[] errorDecorators = EMPTY_DECORATOR;
184     protected String JavaDoc[] warnDecorators = EMPTY_DECORATOR;
185     protected String JavaDoc[] infoDecorators = EMPTY_DECORATOR;
186     protected String JavaDoc[] debugDecorators = EMPTY_DECORATOR;
187
188     private static final String JavaDoc[] EMPTY_DECORATOR = {"", ""};
189
190   //~ Static variables/initializers ============================================
191

192   protected static DateFormat JavaDoc dateFormat = new SimpleDateFormat JavaDoc("MM/dd/yy h:mm:ss a");
193
194   /** this will */
195   protected static String JavaDoc MESSAGE_DELIMS = " : ";//"\r\n";
196
//protected static String margin = createMarginString();
197

198   
199 }
200
Popular Tags