1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 package org.waarp.common.logging;
34
35 import io.netty.util.internal.logging.InternalLoggerFactory;
36 import io.netty.util.internal.logging.JdkLoggerFactory;
37 import io.netty.util.internal.logging.Slf4JLoggerFactory;
38 import org.waarp.common.utility.SystemPropertyUtil;
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 public abstract class WaarpLoggerFactory {
64 private static volatile WaarpLoggerFactory defaultFactory;
65 protected static WaarpLogLevel currentLevel;
66 static String localName = null;
67
68 static {
69 SystemPropertyUtil.isFileEncodingCorrect();
70 final String name = WaarpLoggerFactory.class.getName();
71 WaarpLoggerFactory f;
72 try {
73 f = new WaarpSlf4JLoggerFactory(true);
74 f.newInstance(name)
75 .debug("Using Logback (SLF4J) as the default logging framework");
76 defaultFactory = f;
77 } catch (final Throwable t1) {
78 f = new WaarpJdkLoggerFactory(null);
79 f.newInstance(name)
80 .debug("Using java.util.logging as the default logging framework", t1);
81 }
82
83 defaultFactory = f;
84 }
85
86
87
88
89
90
91
92 public static WaarpLoggerFactory getDefaultFactory() {
93 return defaultFactory;
94 }
95
96
97
98
99
100
101
102 public static void setDefaultFactoryIfNotSame(
103 final WaarpLoggerFactory factory) {
104 final WaarpLoggerFactory current = getDefaultFactory();
105 if (current != null && current.getClass() == factory.getClass()) {
106 current.seLevelSpecific(factory.getLevelSpecific());
107 } else {
108 setDefaultFactory(factory);
109 }
110 }
111
112
113
114
115
116
117 public static void setDefaultFactory(final WaarpLoggerFactory factory) {
118 if (factory == null) {
119 throw new IllegalArgumentException("defaultFactory");
120 }
121 WaarpLoggerFactory.defaultFactory = factory;
122 if (factory instanceof WaarpJdkLoggerFactory) {
123 InternalLoggerFactory.setDefaultFactory(JdkLoggerFactory.INSTANCE);
124 } else if (factory instanceof WaarpSlf4JLoggerFactory) {
125 InternalLoggerFactory.setDefaultFactory(Slf4JLoggerFactory.INSTANCE);
126 }
127 factory.seLevelSpecific(factory.getLevelSpecific());
128 }
129
130
131
132
133
134
135
136
137 public static WaarpLogger getInstance(final Class<?> clazz) {
138 return getInstance(clazz.getName());
139 }
140
141
142
143
144
145
146
147
148 public static WaarpLogger getInstance(final String name) {
149 return getDefaultFactory().newInstance(name);
150 }
151
152
153
154
155
156
157
158
159 public static WaarpLogger getLogger(final Class<?> clazz) {
160 return getInstance(clazz.getName());
161 }
162
163
164
165
166
167
168
169
170 public static WaarpLogger getLogger(final String name) {
171 return getDefaultFactory().newInstance(name);
172 }
173
174
175
176
177 public static void setLogLevel(final WaarpLogLevel level) {
178 setInternalLogLevel(level);
179 if (currentLevel != null) {
180 getDefaultFactory().seLevelSpecific(currentLevel);
181 }
182 }
183
184 public static WaarpLogLevel getLogLevel() {
185 return currentLevel;
186 }
187
188 protected static synchronized void setInternalLogLevel(
189 final WaarpLogLevel level) {
190 if (level != null) {
191 currentLevel = level;
192 }
193 }
194
195
196
197
198 protected WaarpLoggerFactory(final WaarpLogLevel level) {
199 setInternalLogLevel(level);
200 if (currentLevel == null) {
201 setInternalLogLevel(getLevelSpecific());
202 }
203 }
204
205
206
207
208 public static void setLocalName(final String localNameNew) {
209 localName = localNameNew;
210 }
211
212
213
214
215 protected abstract WaarpLogLevel getLevelSpecific();
216
217
218
219
220
221
222 protected abstract void seLevelSpecific(WaarpLogLevel level);
223
224
225
226
227 protected abstract WaarpLogger newInstance(String name);
228 }