Skip to content

Commit 9606be1

Browse files
authored
[feat] (re-invent) log4j as a logging backend (#310)
1 parent 185d42e commit 9606be1

File tree

5 files changed

+115
-7
lines changed

5 files changed

+115
-7
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,12 @@ logging system, configure `jruby.rack.logging` as follows:
297297
- `servlet_context` (default): Sends log messages to the servlet context.
298298
- `stdout`: Sends log messages to the standard output stream `System.out`.
299299
- `slf4j`: Sends log messages to SLF4J. SLF4J configuration is left up to you,
300-
please refer to http://www.slf4j.org/docs.html .
301-
- `log4j`: Sends log messages to log4J. Again, Log4J configuration is
302-
left up to you, consult http://logging.apache.org/log4j/ .
300+
please refer to https://www.slf4j.org/manual.html .
301+
- `log4j`: Sends log messages through Log4j. Only Log4j 2.x is supported, for
302+
- configuration please consult https://logging.apache.org/log4j/2.x/index.html .
303303
- `commons_logging`: Routes logs to commons-logging. You still need to configure
304-
an underlying logging implementation with JCL. We recommend using the logger
305-
library wrapper directly if possible, see http://commons.apache.org/logging/ .
304+
an underlying logging implementation with JCL.
305+
We recommend rather using the logger library wrapper directly when possible.
306306
- `jul`: Directs log messages via Java's core logging facilities (util.logging).
307307

308308
For those loggers that require a specific named logger, set it with the

pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<jruby.maven.plugins.version>3.0.6</jruby.maven.plugins.version>
2424
<gem.home>${project.build.directory}/rubygems</gem.home>
2525
<slf4j.version>2.0.17</slf4j.version>
26+
<log4j.version>2.22.1</log4j.version>
2627
<spring.version>5.3.39</spring.version>
2728
</properties>
2829

@@ -117,6 +118,18 @@
117118
<version>${slf4j.version}</version>
118119
<scope>test</scope>
119120
</dependency>
121+
<dependency>
122+
<groupId>org.apache.logging.log4j</groupId>
123+
<artifactId>log4j-api</artifactId>
124+
<version>${log4j.version}</version>
125+
<scope>provided</scope>
126+
</dependency>
127+
<dependency>
128+
<groupId>org.apache.logging.log4j</groupId>
129+
<artifactId>log4j-core</artifactId>
130+
<version>${log4j.version}</version>
131+
<scope>test</scope>
132+
</dependency>
120133
<dependency>
121134
<groupId>org.springframework</groupId>
122135
<artifactId>spring-web</artifactId>

src/main/java/org/jruby/rack/DefaultRackConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ private static Map<String,String> getLoggerTypes() {
411411
final Map<String,String> loggerTypes = new HashMap<>(8);
412412
loggerTypes.put("commons_logging", "org.jruby.rack.logging.CommonsLoggingLogger");
413413
loggerTypes.put("clogging", "org.jruby.rack.logging.CommonsLoggingLogger");
414+
loggerTypes.put("log4j", "org.jruby.rack.logging.Log4jLogger");
414415
loggerTypes.put("slf4j", "org.jruby.rack.logging.Slf4jLogger");
415416
loggerTypes.put("jul", "org.jruby.rack.logging.JulLogger");
416417
return loggerTypes;
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright (c) 2025 Karol Bucek LTD.
3+
* This source code is available under the MIT license.
4+
* See the file LICENSE.txt for details.
5+
*/
6+
package org.jruby.rack.logging;
7+
8+
import org.apache.logging.log4j.LogManager;
9+
import org.apache.logging.log4j.Logger;
10+
import org.jruby.rack.RackLogger;
11+
12+
public class Log4jLogger extends RackLogger.Base {
13+
14+
private Logger logger;
15+
16+
public Log4jLogger() {
17+
this(LogManager.ROOT_LOGGER_NAME);
18+
}
19+
20+
public Log4jLogger(String loggerName) {
21+
setLoggerName(loggerName);
22+
}
23+
24+
public Logger getLogger() {
25+
return logger;
26+
}
27+
28+
public void setLogger(Logger logger) {
29+
this.logger = logger;
30+
}
31+
32+
public void setLoggerName(String loggerName) {
33+
logger = LogManager.getLogger(loggerName);
34+
}
35+
36+
@Override
37+
public boolean isEnabled(Level level) {
38+
if ( level == null ) return logger.isInfoEnabled();
39+
switch ( level ) {
40+
case DEBUG: return logger.isDebugEnabled();
41+
case INFO: return logger.isInfoEnabled();
42+
case WARN: return logger.isWarnEnabled();
43+
case ERROR: return logger.isErrorEnabled();
44+
case FATAL: return logger.isFatalEnabled();
45+
}
46+
return logger.isTraceEnabled();
47+
}
48+
49+
@Override
50+
public void log(Level level, CharSequence message) {
51+
if ( level == null ) { logger.info(message); return; }
52+
switch ( level ) {
53+
case DEBUG: logger.debug(message); break;
54+
case INFO: logger.info(message); break;
55+
case WARN: logger.warn(message); break;
56+
case ERROR: logger.error(message); break;
57+
case FATAL: logger.fatal(message); break;
58+
}
59+
}
60+
61+
@Override
62+
public void log(Level level, CharSequence message, Throwable ex) {
63+
if ( level == null ) { logger.error(message, ex); return; }
64+
switch ( level ) {
65+
case DEBUG: logger.debug(message, ex); break;
66+
case INFO: logger.info(message, ex); break;
67+
case WARN: logger.warn(message, ex); break;
68+
case ERROR: logger.error(message, ex); break;
69+
case FATAL: logger.fatal(message, ex); break;
70+
}
71+
}
72+
73+
@Override
74+
public Level getLevel() {
75+
if ( logger.isDebugEnabled() ) return Level.DEBUG;
76+
if ( logger.isInfoEnabled() ) return Level.INFO ;
77+
if ( logger.isWarnEnabled() ) return Level.WARN ;
78+
if ( logger.isErrorEnabled() ) return Level.ERROR;
79+
if ( logger.isFatalEnabled() ) return Level.FATAL;
80+
return null;
81+
}
82+
83+
}

src/spec/ruby/rack/config_spec.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@
4141
logger.should be_a(org.jruby.rack.logging.Slf4jLogger)
4242
end
4343

44+
it "constructs a log4j logger from the context init param" do
45+
@servlet_context.should_receive(:getInitParameter).with("jruby.rack.logging").and_return "log4j"
46+
logger.should be_a(org.jruby.rack.logging.Log4jLogger)
47+
end
48+
4449
it "constructs a commons logging logger from system properties" do
4550
java.lang.System.setProperty("jruby.rack.logging", "commons_logging")
4651
logger.should be_a(org.jruby.rack.logging.CommonsLoggingLogger)
@@ -50,13 +55,19 @@
5055
@servlet_context.should_receive(:getInitParameter).with("jruby.rack.logging.name").and_return "/myapp"
5156
@servlet_context.should_receive(:getInitParameter).with("jruby.rack.logging").and_return "JUL"
5257
logger.should be_a(org.jruby.rack.logging.JulLogger)
53-
logger.logger.name.should == '/myapp'
58+
logger.getLogger.name.should == '/myapp'
5459
end
5560

5661
it "constructs a slf4j logger with default logger name" do
5762
java.lang.System.setProperty("jruby.rack.logging", "slf4j")
5863
logger.should be_a(org.jruby.rack.logging.Slf4jLogger)
59-
logger.logger.name.should == 'jruby.rack'
64+
logger.getLogger.name.should == 'jruby.rack'
65+
end
66+
67+
it "constructs a log4j logger with default logger name" do
68+
java.lang.System.setProperty("jruby.rack.logging", "log4j")
69+
logger.should be_a(org.jruby.rack.logging.Log4jLogger)
70+
logger.getLogger.name.should == 'jruby.rack'
6071
end
6172

6273
it "constructs a logger from the context init params over system properties" do

0 commit comments

Comments
 (0)