Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

support for delegating logs to java.util.logging

  • Loading branch information...
commit 4f743cc46dc1782a766fcb6a5e52f449b67f2980 1 parent 625217c
@kares kares authored
View
1  Gemfile
@@ -11,4 +11,3 @@ gem "appraisal"
gem 'rake', :group => :test
gem 'rspec', '~> 2.10', :group => :test
gem 'jruby-openssl', :group => :test
-
View
8 src/main/java/org/jruby/rack/DefaultRackConfig.java
@@ -115,8 +115,9 @@ else if ( "stderr".equalsIgnoreCase(loggerClass) ) {
}
else {
final Map<String, String> loggerTypes = getLoggerTypes();
- if (loggerTypes.containsKey(loggerClass)) {
- loggerClass = loggerTypes.get(loggerClass);
+ final String loggerKey = loggerClass.toLowerCase();
+ if (loggerTypes.containsKey(loggerKey)) {
+ loggerClass = loggerTypes.get(loggerKey);
}
logger = createLogger(loggerClass);
}
@@ -257,8 +258,9 @@ protected static Boolean toBoolean(String value, Boolean defaultValue) {
final Map<String,String> loggerTypes = new HashMap<String, String>();
loggerTypes.put("commons_logging", "org.jruby.rack.logging.CommonsLoggingLogger");
loggerTypes.put("clogging", "org.jruby.rack.logging.CommonsLoggingLogger");
- loggerTypes.put("slf4j", "org.jruby.rack.logging.Slf4jLogger");
loggerTypes.put("log4j", "org.jruby.rack.logging.Log4jLogger");
+ loggerTypes.put("slf4j", "org.jruby.rack.logging.Slf4jLogger");
+ loggerTypes.put("jul", "org.jruby.rack.logging.JulLogger");
loggerTypes.put("servlet_context", "org.jruby.rack.logging.ServletContextLogger");
return loggerTypes;
}
View
58 src/main/java/org/jruby/rack/logging/JulLogger.java
@@ -0,0 +1,58 @@
+/*
+ * This source code is available under the MIT license.
+ * See the file LICENSE.txt for details.
+ */
+package org.jruby.rack.logging;
+
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import org.jruby.rack.RackLogger;
+
+/**
+ * java.util.logging based logger implementation
+ *
+ * @see Logger
+ * @author kares
+ */
+public class JulLogger implements RackLogger {
+
+ private Logger logger;
+
+ public JulLogger() {
+ setLoggerName(""); // "" - root logger name
+ }
+
+ public JulLogger(String loggerName) {
+ setLoggerName(loggerName);
+ }
+
+ public void setLoggerName(String loggerName) {
+ logger = Logger.getLogger(loggerName);
+ }
+
+ public void log(String message) {
+ logger.log( Level.INFO, message );
+ }
+
+ public void log(String message, Throwable e) {
+ logger.log( Level.SEVERE, message, e );
+ }
+
+ public void log(String level, String message) {
+ logger.log( mapLevel(level, Level.INFO), message );
+ }
+
+ public void log(String level, String message, Throwable e) {
+ logger.log( mapLevel(level, Level.SEVERE), message, e );
+ }
+
+ private Level mapLevel(String level, Level defaultLevel) {
+ if ( level == ERROR ) return Level.SEVERE;
+ if ( level == WARN ) return Level.WARNING;
+ if ( level == INFO ) return Level.INFO;
+ if ( level == DEBUG ) return Level.FINE;
+ return defaultLevel;
+ }
+
+}
View
50 src/spec/ruby/rack/config_spec.rb
@@ -7,41 +7,71 @@
require File.expand_path('spec_helper', File.dirname(__FILE__) + '/..')
-import org.jruby.rack.servlet.ServletRackConfig
-
-describe ServletRackConfig do
- let(:config) { ServletRackConfig.new(@servlet_context).tap {|c| c.quiet = true } }
+describe org.jruby.rack.servlet.ServletRackConfig do
+
+ let(:config) do
+ config = org.jruby.rack.servlet.ServletRackConfig.new(@servlet_context)
+ config.quiet = true; config
+ end
- describe "getLogger" do
+ context "getLogger" do
+
let(:logger) { config.getLogger }
after :each do
java.lang.System.clearProperty("jruby.rack.logging")
end
+ it "constructs a slf4j logger from the context init param" do
+ @servlet_context.should_receive(:getInitParameter).with("jruby.rack.logging").and_return "slf4j"
+ logger.should be_a(org.jruby.rack.logging.Slf4jLogger)
+ end
+
+ it "constructs a commons logging logger from system properties" do
+ java.lang.System.setProperty("jruby.rack.logging", "commons_logging")
+ logger.should be_a(org.jruby.rack.logging.CommonsLoggingLogger)
+ end
+
+ org.jruby.rack.logging.JulLogger.class_eval { field_reader :logger }
+
+ it "constructs a jul logger with logger name" do
+ @servlet_context.should_receive(:getInitParameter).with("jruby.rack.logging.name").and_return "/myapp"
+ @servlet_context.should_receive(:getInitParameter).with("jruby.rack.logging").and_return "JUL"
+ logger.should be_a(org.jruby.rack.logging.JulLogger)
+ logger.logger.name.should == '/myapp'
+ end
+
+ org.jruby.rack.logging.Log4jLogger.class_eval { field_reader :logger }
+
+ it "constructs a slf4j logger with default logger name" do
+ java.lang.System.setProperty("jruby.rack.logging", "Log4J")
+ logger.should be_a(org.jruby.rack.logging.Log4jLogger)
+ logger.logger.name.should == 'jruby.rack'
+ end
+
it "constructs a logger from the context init params over system properties" do
@servlet_context.should_receive(:getInitParameter).with("jruby.rack.logging").and_return "clogging"
java.lang.System.setProperty("jruby.rack.logging", "stdout")
- logger.should be_kind_of(org.jruby.rack.logging.CommonsLoggingLogger)
+ logger.should be_a(org.jruby.rack.logging.CommonsLoggingLogger)
end
it "constructs a standard out logger when the logging attribute is unrecognized" do
java.lang.System.setProperty("jruby.rack.logging", "other")
- logger.should be_kind_of(org.jruby.rack.logging.StandardOutLogger)
+ logger.should be_a(org.jruby.rack.logging.StandardOutLogger)
end
it "constructs a standard out logger when the logger can't be instantiated" do
java.lang.System.setProperty("jruby.rack.logging", "java.lang.String")
- logger.should be_kind_of(org.jruby.rack.logging.StandardOutLogger)
+ logger.should be_a(org.jruby.rack.logging.StandardOutLogger)
end
it "constructs a servlet context logger by default" do
- logger.should be_kind_of(org.jruby.rack.logging.ServletContextLogger)
+ logger.should be_a(org.jruby.rack.logging.ServletContextLogger)
end
it "allows specifying a class name in the logging attribute" do
java.lang.System.setProperty("jruby.rack.logging", "org.jruby.rack.logging.CommonsLoggingLogger")
- logger.should be_kind_of(org.jruby.rack.logging.CommonsLoggingLogger)
+ logger.should be_a(org.jruby.rack.logging.CommonsLoggingLogger)
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.