File tree Expand file tree Collapse file tree
java/com/ocpsoft/rewrite/logging/slf4j
resources/META-INF/services
test/java/com/ocpsoft/rewrite/logging/slf4j Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+ <project
3+ xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4+ xmlns =" http://maven.apache.org/POM/4.0.0" xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance" >
5+ <modelVersion >4.0.0</modelVersion >
6+ <parent >
7+ <artifactId >rewrite-parent</artifactId >
8+ <groupId >com.ocpsoft.rewrite</groupId >
9+ <version >1.0.0-SNAPSHOT</version >
10+ <relativePath >../</relativePath >
11+ </parent >
12+
13+ <artifactId >rewrite-logging-slf4j</artifactId >
14+
15+ <properties >
16+ <version .slf4j>1.6.1</version .slf4j>
17+ </properties >
18+
19+ <dependencies >
20+
21+ <dependency >
22+ <groupId >com.ocpsoft.rewrite</groupId >
23+ <artifactId >rewrite-api</artifactId >
24+ </dependency >
25+
26+ <dependency >
27+ <groupId >org.slf4j</groupId >
28+ <artifactId >slf4j-api</artifactId >
29+ <version >${version.slf4j} </version >
30+ </dependency >
31+
32+ </dependencies >
33+
34+ <build >
35+ <finalName >logging-slf4j</finalName >
36+ <plugins >
37+ <plugin >
38+ <artifactId >maven-compiler-plugin</artifactId >
39+ <version >2.3.2</version >
40+ <configuration >
41+ <source >1.6</source >
42+ <target >1.6</target >
43+ </configuration >
44+ </plugin >
45+ </plugins >
46+ </build >
47+ </project >
Original file line number Diff line number Diff line change 1+ package com .ocpsoft .rewrite .logging .slf4j ;
2+
3+ import org .slf4j .Logger ;
4+ import org .slf4j .LoggerFactory ;
5+
6+ import com .ocpsoft .rewrite .logging .Log ;
7+
8+ /**
9+ * Log adapter that delegates log events to SLF4J
10+ *
11+ * @author Christian Kaltepoth <christian@kaltepoth.de>
12+ */
13+ public class SLF4JLogAdapter extends Log
14+ {
15+
16+ private final Logger delegate ;
17+
18+ public SLF4JLogAdapter (String name )
19+ {
20+ delegate = LoggerFactory .getLogger (name );
21+ }
22+
23+ @ Override
24+ protected void log (Level level , String msg , Throwable t )
25+ {
26+ switch (level )
27+ {
28+ case TRACE :
29+ if (t == null )
30+ {
31+ delegate .trace (msg );
32+ }
33+ else
34+ {
35+ delegate .trace (msg , t );
36+ }
37+ break ;
38+
39+ case DEBUG :
40+ if (t == null )
41+ {
42+ delegate .debug (msg );
43+ }
44+ else
45+ {
46+ delegate .debug (msg , t );
47+ }
48+ break ;
49+
50+ case INFO :
51+ if (t == null )
52+ {
53+ delegate .info (msg );
54+ }
55+ else
56+ {
57+ delegate .info (msg , t );
58+ }
59+ break ;
60+
61+ case WARN :
62+ if (t == null )
63+ {
64+ delegate .warn (msg );
65+ }
66+ else
67+ {
68+ delegate .warn (msg , t );
69+ }
70+ break ;
71+
72+ case ERROR :
73+ if (t == null )
74+ {
75+ delegate .error (msg );
76+ }
77+ else
78+ {
79+ delegate .error (msg , t );
80+ }
81+ break ;
82+
83+ default :
84+ throw new IllegalArgumentException ("Cannot handle log leve: " + level );
85+ }
86+ }
87+
88+ @ Override
89+ protected boolean isEnabled (Level level )
90+ {
91+ switch (level )
92+ {
93+ case TRACE :
94+ return delegate .isTraceEnabled ();
95+ case DEBUG :
96+ return delegate .isDebugEnabled ();
97+ case INFO :
98+ return delegate .isInfoEnabled ();
99+ case WARN :
100+ return delegate .isWarnEnabled ();
101+ case ERROR :
102+ return delegate .isErrorEnabled ();
103+ default :
104+ throw new IllegalArgumentException ("Cannot handle log leve: " + level );
105+ }
106+ }
107+
108+ }
Original file line number Diff line number Diff line change 1+ package com .ocpsoft .rewrite .logging .slf4j ;
2+
3+ import com .ocpsoft .rewrite .logging .Log ;
4+ import com .ocpsoft .rewrite .spi .LogAdapterFactory ;
5+
6+ /**
7+ * Implementation of {@link LogAdapterFactory} that creates log adapters
8+ * delegating log events to SLF4J.
9+ *
10+ * @author Christian Kaltepoth <christian@kaltepoth.de>
11+ */
12+ public class SLF4JLogAdapterFactory implements LogAdapterFactory
13+ {
14+
15+ @ Override
16+ public int priority ()
17+ {
18+ return 5 ;
19+ }
20+
21+ @ Override
22+ public Log createLogAdapter (String name )
23+ {
24+ return new SLF4JLogAdapter (name );
25+ }
26+
27+ }
Original file line number Diff line number Diff line change 1+ com.ocpsoft.rewrite.logging.slf4j.SLF4JLogAdapterFactory
Original file line number Diff line number Diff line change 1+ package com .ocpsoft .rewrite .logging .slf4j ;
2+
3+ import static junit .framework .Assert .assertTrue ;
4+
5+ import org .junit .Test ;
6+
7+ import com .ocpsoft .rewrite .logging .Log ;
8+ import com .ocpsoft .rewrite .logging .LogFactory ;
9+
10+ public class SLF4JLogAdapterFactoryTest
11+ {
12+
13+ @ Test
14+ public void testSLF4JAdapterPreferedOverJDKLogger ()
15+ {
16+ Log log = LogFactory .getLog (this .getClass ());
17+ assertTrue (log instanceof SLF4JLogAdapter );
18+ }
19+
20+ }
Original file line number Diff line number Diff line change 3434 <module >test-harness</module >
3535 <module >api</module >
3636 <module >api-tests</module >
37+ <module >logging-slf4j</module >
3738 <module >config-jodatime</module >
3839 <module >config-tuckey</module >
3940 <module >impl-servlet</module >
You can’t perform that action at this time.
0 commit comments