Skip to content

Commit 2b8deda

Browse files
committed
Removed LoggerFactory passthrough, added JBoss Logging adapter
1 parent 1c8ac58 commit 2b8deda

20 files changed

Lines changed: 372 additions & 164 deletions

File tree

‎api/src/main/java/com/ocpsoft/rewrite/config/ConfigurationLoader.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.List;
2020

2121
import com.ocpsoft.rewrite.logging.Logger;
22-
import com.ocpsoft.rewrite.logging.LoggerFactory;
2322
import com.ocpsoft.rewrite.pattern.WeightedComparator;
2423
import com.ocpsoft.rewrite.services.ServiceLoader;
2524
import com.ocpsoft.rewrite.util.Iterators;
@@ -30,7 +29,7 @@
3029
*/
3130
public class ConfigurationLoader
3231
{
33-
public static Logger log = LoggerFactory.getLogger(ConfigurationLoader.class);
32+
public static Logger log = Logger.getLogger(ConfigurationLoader.class);
3433

3534
@SuppressWarnings({ "rawtypes", "unchecked" })
3635
public static Configuration loadConfiguration(final Object context)
Lines changed: 106 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
package com.ocpsoft.rewrite.logging;
22

3+
import java.util.Collections;
4+
import java.util.List;
5+
import java.util.ServiceLoader;
6+
7+
import com.ocpsoft.rewrite.pattern.WeightedComparator;
8+
import com.ocpsoft.rewrite.spi.LogAdapterFactory;
9+
import com.ocpsoft.rewrite.util.Iterators;
10+
311
/**
412
* Class to create log messages.
513
*
@@ -8,7 +16,8 @@
816
public abstract class Logger
917
{
1018

11-
public enum Level {
19+
public enum Level
20+
{
1221
TRACE, DEBUG, INFO, WARN, ERROR
1322
}
1423

@@ -21,27 +30,27 @@ public boolean isTraceEnabled()
2130
return isEnabled(Level.TRACE);
2231
}
2332

24-
public void trace(String msg)
33+
public void trace(final String msg)
2534
{
2635
log(Level.TRACE, msg, null);
2736
}
2837

29-
public void trace(String msg, Object arg)
38+
public void trace(final String msg, final Object arg)
3039
{
3140
log(Level.TRACE, format(msg, new Object[] { arg }), null);
3241
}
3342

34-
public void trace(String msg, Object arg1, Object arg2)
43+
public void trace(final String msg, final Object arg1, final Object arg2)
3544
{
3645
log(Level.TRACE, format(msg, new Object[] { arg1, arg2 }), null);
3746
}
3847

39-
public void trace(String msg, Object[] argArray)
48+
public void trace(final String msg, final Object[] argArray)
4049
{
4150
log(Level.TRACE, format(msg, argArray), null);
4251
}
4352

44-
public void trace(String msg, Throwable t)
53+
public void trace(final String msg, final Throwable t)
4554
{
4655
log(Level.TRACE, msg, t);
4756
}
@@ -51,27 +60,27 @@ public boolean isDebugEnabled()
5160
return isEnabled(Level.DEBUG);
5261
}
5362

54-
public void debug(String msg)
63+
public void debug(final String msg)
5564
{
5665
log(Level.DEBUG, msg, null);
5766
}
5867

59-
public void debug(String msg, Object arg)
68+
public void debug(final String msg, final Object arg)
6069
{
6170
log(Level.DEBUG, format(msg, new Object[] { arg }), null);
6271
}
6372

64-
public void debug(String msg, Object arg1, Object arg2)
73+
public void debug(final String msg, final Object arg1, final Object arg2)
6574
{
6675
log(Level.DEBUG, format(msg, new Object[] { arg1, arg2 }), null);
6776
}
6877

69-
public void debug(String msg, Object[] argArray)
78+
public void debug(final String msg, final Object[] argArray)
7079
{
7180
log(Level.DEBUG, format(msg, argArray), null);
7281
}
7382

74-
public void debug(String msg, Throwable t)
83+
public void debug(final String msg, final Throwable t)
7584
{
7685
log(Level.DEBUG, msg, t);
7786
}
@@ -81,27 +90,27 @@ public boolean isInfoEnabled()
8190
return isEnabled(Level.INFO);
8291
}
8392

84-
public void info(String msg)
93+
public void info(final String msg)
8594
{
8695
log(Level.INFO, msg, null);
8796
}
8897

89-
public void info(String msg, Object arg)
98+
public void info(final String msg, final Object arg)
9099
{
91100
log(Level.INFO, format(msg, new Object[] { arg }), null);
92101
}
93102

94-
public void info(String msg, Object arg1, Object arg2)
103+
public void info(final String msg, final Object arg1, final Object arg2)
95104
{
96105
log(Level.INFO, format(msg, new Object[] { arg1, arg2 }), null);
97106
}
98107

99-
public void info(String msg, Object[] argArray)
108+
public void info(final String msg, final Object[] argArray)
100109
{
101110
log(Level.INFO, format(msg, argArray), null);
102111
}
103112

104-
public void info(String msg, Throwable t)
113+
public void info(final String msg, final Throwable t)
105114
{
106115
log(Level.INFO, msg, t);
107116
}
@@ -111,27 +120,27 @@ public boolean isWarnEnabled()
111120
return isEnabled(Level.WARN);
112121
}
113122

114-
public void warn(String msg)
123+
public void warn(final String msg)
115124
{
116125
log(Level.WARN, msg, null);
117126
}
118127

119-
public void warn(String msg, Object arg)
128+
public void warn(final String msg, final Object arg)
120129
{
121130
log(Level.WARN, format(msg, new Object[] { arg }), null);
122131
}
123132

124-
public void warn(String msg, Object arg1, Object arg2)
133+
public void warn(final String msg, final Object arg1, final Object arg2)
125134
{
126135
log(Level.WARN, format(msg, new Object[] { arg1, arg2 }), null);
127136
}
128137

129-
public void warn(String msg, Object[] argArray)
138+
public void warn(final String msg, final Object[] argArray)
130139
{
131140
log(Level.WARN, format(msg, argArray), null);
132141
}
133142

134-
public void warn(String msg, Throwable t)
143+
public void warn(final String msg, final Throwable t)
135144
{
136145
log(Level.WARN, msg, t);
137146
}
@@ -141,32 +150,32 @@ public boolean isErrorEnabled()
141150
return isEnabled(Level.ERROR);
142151
}
143152

144-
public void error(String msg)
153+
public void error(final String msg)
145154
{
146155
log(Level.ERROR, msg, null);
147156
}
148157

149-
public void error(String msg, Object arg)
158+
public void error(final String msg, final Object arg)
150159
{
151160
log(Level.ERROR, format(msg, new Object[] { arg }), null);
152161
}
153162

154-
public void error(String msg, Object arg1, Object arg2)
163+
public void error(final String msg, final Object arg1, final Object arg2)
155164
{
156165
log(Level.ERROR, format(msg, new Object[] { arg1, arg2 }), null);
157166
}
158167

159-
public void error(String msg, Object[] argArray)
168+
public void error(final String msg, final Object[] argArray)
160169
{
161170
log(Level.ERROR, format(msg, argArray), null);
162171
}
163172

164-
public void error(String msg, Throwable t)
173+
public void error(final String msg, final Throwable t)
165174
{
166175
log(Level.ERROR, msg, t);
167176
}
168177

169-
protected String format(String msg, Object[] args)
178+
protected String format(final String msg, final Object[] args)
170179
{
171180

172181
StringBuilder builder = new StringBuilder(msg);
@@ -183,4 +192,74 @@ protected String format(String msg, Object[] args)
183192
return builder.toString();
184193
}
185194

195+
/**
196+
* The LogAdapterFactory to use. Only {@link #getAdapterFactory()} should use this field. It is declared as volatile
197+
* so that the double-checked locking implemented in {@link #getAdapterFactory()} works correctly.
198+
*/
199+
private static volatile LogAdapterFactory _adapterFactory = null;
200+
201+
/**
202+
* Create a {@link Logger} instance for a specific class
203+
*
204+
* @param clazz The class to create the log for
205+
* @return The {@link Logger} instance
206+
*/
207+
public static Logger getLogger(final Class<?> clazz)
208+
{
209+
return getLogger(clazz.getName());
210+
}
211+
212+
/**
213+
* Create a {@link Logger} instance for a specific logger name
214+
*
215+
* @param logger the logger name
216+
* @return The {@link Logger} instance
217+
*/
218+
public static Logger getLogger(final String logger)
219+
{
220+
LogAdapterFactory adapterFactory = getAdapterFactory();
221+
return adapterFactory.createLogAdapter(logger);
222+
}
223+
224+
/**
225+
* This method provides access to the {@link LogAdapterFactory}. It will obtain the {@link LogAdapterFactory} lazily
226+
* using {@link #createAdapterFactory()}.
227+
*/
228+
private static LogAdapterFactory getAdapterFactory()
229+
{
230+
// double-checked locking
231+
if (_adapterFactory == null)
232+
{
233+
synchronized (Logger.class)
234+
{
235+
if (_adapterFactory == null)
236+
{
237+
_adapterFactory = createAdapterFactory();
238+
}
239+
}
240+
}
241+
return _adapterFactory;
242+
}
243+
244+
/**
245+
* Called one by {@link #getAdapterFactory()} to obtain the {@link LogAdapterFactory} with the highest priority.
246+
*/
247+
private static LogAdapterFactory createAdapterFactory()
248+
{
249+
250+
// use the ServiceLoader to get all LogAdapterFactories
251+
List<LogAdapterFactory> factories = Iterators.asUniqueList(ServiceLoader.load(LogAdapterFactory.class));
252+
if (factories.isEmpty())
253+
{
254+
throw new IllegalStateException("Log logging implementations found!");
255+
}
256+
257+
// sort by priority
258+
Collections.sort(factories, new WeightedComparator());
259+
260+
// we will use the factory with the highest priority
261+
return factories.get(0);
262+
263+
}
264+
186265
}

‎api/src/main/java/com/ocpsoft/rewrite/logging/LoggerFactory.java‎

Lines changed: 0 additions & 94 deletions
This file was deleted.

‎api/src/main/java/com/ocpsoft/rewrite/services/ServiceLoader.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import java.util.Set;
3131

3232
import com.ocpsoft.rewrite.logging.Logger;
33-
import com.ocpsoft.rewrite.logging.LoggerFactory;
3433
import com.ocpsoft.rewrite.spi.ServiceEnricher;
3534
import com.ocpsoft.rewrite.util.Iterators;
3635
import com.ocpsoft.rewrite.util.ServiceLogger;
@@ -55,7 +54,7 @@ public class ServiceLoader<S> implements Iterable<S>
5554
{
5655
private static final String SERVICES = "META-INF/services";
5756

58-
Logger log = LoggerFactory.getLogger(ServiceLoader.class);
57+
Logger log = Logger.getLogger(ServiceLoader.class);
5958

6059
/**
6160
* Creates a new service loader for the given service type, using the current thread's context class loader.

0 commit comments

Comments
 (0)