KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > log > Logger


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.log;
10
11 /**
12  * Base class for logging objects.
13  *
14  * @version $Revision: 1.5 $
15  */

16 public class Logger
17 {
18    public static final int TRACE = 0;
19    public static final int DEBUG = TRACE + 10;
20    public static final int INFO = DEBUG + 10;
21    public static final int WARN = INFO + 10;
22    public static final int ERROR = WARN + 10;
23    public static final int FATAL = ERROR + 10;
24
25    private int m_priority = WARN;
26    private String JavaDoc m_category;
27
28    protected Logger()
29    {
30    }
31
32    public void setPriority(int priority)
33    {
34       m_priority = priority;
35    }
36
37    public int getPriority()
38    {
39       return m_priority;
40    }
41
42    public String JavaDoc getCategory()
43    {
44       return m_category;
45    }
46
47    protected void setCategory(String JavaDoc category)
48    {
49       m_category = category;
50    }
51
52    public final boolean isEnabledFor(int priority)
53    {
54       return priority >= getPriority();
55    }
56
57    public final void fatal(Object JavaDoc message)
58    {
59       log(FATAL, message, null);
60    }
61
62    public final void fatal(Object JavaDoc message, Throwable JavaDoc t)
63    {
64       log(FATAL, message, t);
65    }
66
67    public final void error(Object JavaDoc message)
68    {
69       log(ERROR, message, null);
70    }
71
72    public final void error(Object JavaDoc message, Throwable JavaDoc t)
73    {
74       log(ERROR, message, t);
75    }
76
77    public final void warn(Object JavaDoc message)
78    {
79       log(WARN, message, null);
80    }
81
82    public final void warn(Object JavaDoc message, Throwable JavaDoc t)
83    {
84       log(WARN, message, t);
85    }
86
87    public final void info(Object JavaDoc message)
88    {
89       log(INFO, message, null);
90    }
91
92    public final void info(Object JavaDoc message, Throwable JavaDoc t)
93    {
94       log(INFO, message, t);
95    }
96
97    public final void debug(Object JavaDoc message)
98    {
99       log(DEBUG, message, null);
100    }
101
102    public final void debug(Object JavaDoc message, Throwable JavaDoc t)
103    {
104       log(DEBUG, message, t);
105    }
106
107    public final void trace(Object JavaDoc message)
108    {
109       log(TRACE, message, null);
110    }
111
112    public final void trace(Object JavaDoc message, Throwable JavaDoc t)
113    {
114       log(TRACE, message, t);
115    }
116
117    protected void log(int priority, Object JavaDoc message, Throwable JavaDoc t)
118    {
119       if (isEnabledFor(priority))
120       {
121          System.out.println(message);
122          if (t != null)
123          {
124             t.printStackTrace(System.out);
125          }
126       }
127    }
128 }
129
Popular Tags