1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.customLogger;
19
20
21 import org.apache.log4j.*;
22 import org.apache.log4j.spi.OptionHandler;
23 import org.apache.log4j.spi.LoggingEvent;
24 import org.apache.log4j.spi.LoggerFactory;
25 import org.apache.log4j.helpers.LogLog;
26 import org.apache.log4j.xml.XLevel;
27
28 /***
29 A simple example showing Logger sub-classing. It shows the
30 minimum steps necessary to implement one's {@link LoggerFactory}.
31 Note that sub-classes follow the hierarchy even if its loggers
32 belong to different classes.
33 */
34 public class XLogger extends Logger implements OptionHandler {
35
36
37
38
39
40 private static String FQCN = XLogger.class.getName() + ".";
41
42
43 private static XFactory factory = new XFactory();
44
45 String suffix = "";
46
47 /***
48 Just calls the parent constuctor.
49 */
50 protected XLogger(String name) {
51 super(name);
52 }
53
54 /***
55 Nothing to activate.
56 */
57 public
58 void activateOptions() {
59 }
60
61 /***
62 Overrides the standard debug method by appending the value of
63 suffix variable to each message.
64 */
65 public
66 void debug(String message) {
67 super.log(FQCN, Level.DEBUG, message + " " + suffix, null);
68 }
69
70 /***
71 We introduce a new printing method in order to support {@link
72 XLevel#LETHAL}. */
73 public
74 void lethal(String message, Throwable t) {
75 if(repository.isDisabled(XLevel.LETHAL_INT))
76 return;
77 if(XLevel.LETHAL.isGreaterOrEqual(this.getEffectiveLevel()))
78 forcedLog(FQCN, XLevel.LETHAL, message, t);
79 }
80
81 /***
82 We introduce a new printing method in order to support {@link
83 XLevel#LETHAL}. */
84 public
85 void lethal(String message) {
86 if(repository.isDisabled(XLevel.LETHAL_INT))
87 return;
88 if(XLevel.LETHAL.isGreaterOrEqual(this.getEffectiveLevel()))
89 forcedLog(FQCN, XLevel.LETHAL, message, null);
90 }
91
92 static
93 public
94 Logger getLogger(String name) {
95 return LogManager.getLogger(name, factory);
96 }
97
98 static
99 public
100 Logger getLogger(Class clazz) {
101 return XLogger.getLogger(clazz.getName());
102 }
103
104
105 public
106 String getSuffix() {
107 return suffix;
108 }
109
110 public
111 void setSuffix(String suffix) {
112 this.suffix = suffix;
113 }
114
115 /***
116 We introduce a new printing method that takes the TRACE level.
117 */
118 public
119 void trace(String message, Throwable t) {
120 if(repository.isDisabled(XLevel.TRACE_INT))
121 return;
122 if(XLevel.TRACE.isGreaterOrEqual(this.getEffectiveLevel()))
123 forcedLog(FQCN, XLevel.TRACE, message, t);
124 }
125
126 /***
127 We introduce a new printing method that takes the TRACE level.
128 */
129 public
130 void trace(String message) {
131 if(repository.isDisabled(XLevel.TRACE_INT))
132 return;
133 if(XLevel.TRACE.isGreaterOrEqual(this.getEffectiveLevel()))
134 forcedLog(FQCN, XLevel.TRACE, message, null);
135 }
136
137
138
139
140
141 public static class XFactory implements LoggerFactory {
142
143 public XFactory() {
144 }
145
146 public
147 Logger makeNewLoggerInstance(String name) {
148 return new XLogger(name);
149 }
150 }
151 }
152
153