1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sf.jour.log;
22
23 import java.util.WeakHashMap;
24
25
26
27
28
29
30
31 public class Logger {
32
33
34 private static final String FQCN = Logger.class.getName();
35
36 private static int log4jAvalable = 0;
37
38 private static WeakHashMap instances = new WeakHashMap();
39
40 protected Logger() {
41 }
42
43 private static int detectLog4j() {
44 try {
45 Class.forName("org.apache.log4j.Logger");
46 return 1;
47 } catch (ClassNotFoundException e) {
48 return -1;
49 }
50 }
51
52
53
54
55 public static Logger getLogger() {
56 String useName = null;
57 StackTraceElement[] ste = new Throwable().getStackTrace();
58 for (int i = 0; i < ste.length; i++ ) {
59 if (ste[i].getClassName().equals(FQCN)) {
60 i ++;
61 useName = ste[i].getClassName();
62 break;
63 }
64 }
65 if (useName == null) {
66 throw new Error("Can't find call origin");
67 }
68 return getLogger(useName);
69 }
70
71
72
73
74 public static Logger getLogger(String name) {
75 return createLogger(name);
76 }
77
78 private static Logger createLogger(String name) {
79 if (log4jAvalable == 0) {
80 log4jAvalable = detectLog4j();
81 }
82 if (log4jAvalable > 0) {
83 return createLoggerWrapper(name);
84 } else {
85 return new Logger();
86 }
87 }
88
89 private static Logger createLoggerWrapper(String name) {
90 org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(name);
91 if (instances.containsKey(logger)) {
92 return (Logger)instances.get(logger);
93 } else {
94 Logger log = new LoggerLog4j(logger);
95 instances.put(logger, log);
96 return log;
97 }
98 }
99
100 public static Logger getLogger(Class clazz) {
101 return getLogger(clazz.getName());
102 }
103
104 public static Logger getLog(Class clazz) {
105 return getLogger(clazz.getName());
106 }
107
108 private static class LoggerLog4j extends Logger {
109
110 private org.apache.log4j.Logger logger;
111
112 protected LoggerLog4j(org.apache.log4j.Logger log4jLogger) {
113 this.logger = log4jLogger;
114 }
115
116 public void error(Object message) {
117 this.logger.log(FQCN, org.apache.log4j.Level.ERROR, message, null);
118 }
119
120 public void error(Object message, Throwable t) {
121 this.logger.log(FQCN, org.apache.log4j.Level.ERROR, message, t);
122 }
123
124 public void warn(Object message) {
125 this.logger.log(FQCN, org.apache.log4j.Level.WARN, message, null);
126 }
127
128 public void info(Object message) {
129 this.logger.log(FQCN, org.apache.log4j.Level.INFO, message, null);
130 }
131
132 public boolean isDebugEnabled() {
133 return this.logger.isDebugEnabled();
134 }
135
136 public void debug(Object message) {
137 this.logger.log(FQCN, org.apache.log4j.Level.DEBUG, message, null);
138 }
139 }
140
141 public void error(Object message) {
142 System.err.println(message);
143 }
144
145 public void error(Object message, Throwable t) {
146 System.err.println(message);
147 if (t != null) {
148 t.printStackTrace(System.err);
149 }
150 }
151
152 public void warn(Object message) {
153 }
154
155 public void info(Object message) {
156 }
157
158 public boolean isDebugEnabled() {
159 return false;
160 }
161
162 public void debug(Object message) {
163 }
164 }