KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infozone > tools > logger > LogWriter


1 // You can redistribute this software and/or modify it under the terms of
2
// the Infozone Software License version 2 published by the Infozone Group
3
// (http://www.infozone-group.org).
4
//
5
// Copyright (C) @year@ by The Infozone Group. All rights reserved.
6
//
7
// $Id: LogWriter.java,v 1.1 2002/05/10 08:59:12 per_nyfelt Exp $
8

9 package org.infozone.tools.logger;
10
11 import java.io.*;
12 import java.util.*;
13
14
15 /**
16 @author <a HREF="http://www.softwarebuero.de/">SMB</a>
17 @version $Revision: 1.1 $Date: 2002/05/10 08:59:12 $
18 */

19 public final class LogWriter {
20
21     public static final int INFO = 1;
22     public static final int WARN = 2;
23     public static final int ERROR = 4;
24     public static final int DEBUG = 8;
25     public static final int DEBUG2 = 16;
26     public static final int DEBUG3 = 32;
27
28     protected static final String JavaDoc INFO_PREFIX = "[info] ";
29     protected static final String JavaDoc WARN_PREFIX = "[warn] ";
30     protected static final String JavaDoc ERROR_PREFIX = "[error]";
31     protected static final String JavaDoc DEBUG_PREFIX = "[debug]";
32
33     public static final int LINE_WIDTH = 70;
34
35     protected Vector logTargets;
36
37     protected int allLevels;
38
39
40     public LogWriter() {
41         logTargets = new Vector();
42         }
43
44
45     /**
46     This method allows to quickly find out if there is any log target that
47     would receive entries of the specified level.
48     */

49     public boolean hasTarget (int level) {
50         return (allLevels & level) > 0;
51         }
52
53
54     public void addLogTarget (OutputStream _out, int _levels) {
55         // set also lower debug levels when higher levels are requested
56
_levels = ((_levels & DEBUG3) > 0) ? (_levels | DEBUG2) : _levels;
57         _levels = ((_levels & DEBUG2) > 0) ? (_levels | DEBUG) : _levels;
58
59         addLogTarget (new PrintWriter (_out), _levels);
60         }
61
62
63     public void addLogTarget (PrintWriter _writer, int _levels) {
64         // set also lower debug levels when higher levels are requested
65
_levels = ((_levels & DEBUG3) > 0) ? (_levels | DEBUG2) : _levels;
66         _levels = ((_levels & DEBUG2) > 0) ? (_levels | DEBUG) : _levels;
67
68         logTargets.addElement (new LogTarget (_writer, _levels));
69         allLevels = allLevels | _levels;
70         }
71
72
73     public void newEntry (Object JavaDoc sender, String JavaDoc msg, int levels) {
74         newEntry (sender, msg, null, levels);
75         }
76
77
78     public void newEntry (Object JavaDoc sender, String JavaDoc msg, Throwable JavaDoc e, int levels) {
79         //check if at least one of the specified levels is supported by
80
//any of the LogTargets
81
if ((allLevels & levels) > 0) {
82             for (int i=0; i<logTargets.size(); i++) {
83                 LogTarget logTarget = (LogTarget)logTargets.elementAt (i);
84                 if ((logTarget.levels & levels & INFO) > 0)
85                     printToWriter (logTarget.writer, sender, msg, INFO_PREFIX, e);
86                 if ((logTarget.levels & levels & WARN) > 0)
87                     printToWriter (logTarget.writer, sender, msg, WARN_PREFIX, e);
88                 if ((logTarget.levels & levels & ERROR) > 0)
89                     printToWriter (logTarget.writer, sender, msg, ERROR_PREFIX, e);
90                 if ((logTarget.levels & levels & (DEBUG | DEBUG2 | DEBUG3)) > 0)
91                     printToWriter (logTarget.writer, sender, msg, DEBUG_PREFIX, e);
92                 }
93             }
94         }
95
96
97     protected void printToWriter (PrintWriter writer, Object JavaDoc sender, String JavaDoc msg, String JavaDoc status, Throwable JavaDoc e) {
98         StringBuffer JavaDoc sb = new StringBuffer JavaDoc (120);
99         sb.append (status);
100         sb.append ('(');
101         String JavaDoc threadNum = String.valueOf(Thread.currentThread().hashCode());
102         sb.append (threadNum.substring (threadNum.length() - 3));
103         sb.append (") ");
104         sb.append (rawClassName (sender));
105         sb.append (": ");
106         sb.append (msg);
107         writer.println (sb);
108         writer.flush();
109         if (e != null) {
110            // writer.print (" exception: " + e.getUserName());
111
printFormatedException (writer, e, LINE_WIDTH, " ");
112             }
113         }
114
115
116     public static String JavaDoc rawClassName (Object JavaDoc obj) {
117         if (obj != null) {
118             String JavaDoc name = (obj instanceof Class JavaDoc)
119                 ?((Class JavaDoc)obj).getName()
120                 :obj.getClass().getName();
121             int index = name.lastIndexOf('.');
122             return name.substring (index+1);
123             }
124         else
125             return "(null)";
126         }
127
128
129     protected void printFormatedException (OutputStream out, Throwable JavaDoc e, int lineWidth, String JavaDoc pre) {
130         printFormatedException (new PrintWriter (out), e, lineWidth, pre);
131         }
132
133
134     protected void printFormatedException (PrintWriter out, Throwable JavaDoc e, int lineWidth, String JavaDoc pre) {
135         StringWriter buff = new StringWriter();
136         e.printStackTrace (new PrintWriter (buff));
137         StringTokenizer st = new StringTokenizer (buff.toString(), "\n");
138         while (st.hasMoreTokens()) {
139             String JavaDoc line = st.nextToken();
140             boolean firstSubLine = true;
141            // while (line.length() > lineWidth) {
142
do {
143                 int subLineLength = Math.min (lineWidth, line.length());
144                 String JavaDoc subLine = line.substring (0, subLineLength);
145                 line = line.substring (subLineLength);
146                 if (!firstSubLine)
147                     out.print (" ");
148                 out.print (pre);
149                 out.println (subLine);
150                 firstSubLine = false;
151                 }
152             while (line.length() > 0);
153             }
154         out.flush();
155         }
156
157     }
158
159
Popular Tags