lib/trinidad/server.rb in trinidad-0.9.9 vs lib/trinidad/server.rb in trinidad-0.9.10

- old
+ new

@@ -1,8 +1,6 @@ module Trinidad - JSystem = java.lang.System - JContext = javax.naming.Context class Server attr_reader :tomcat, :config def default_options @@ -13,11 +11,12 @@ :classes_dir => 'classes', :default_web_xml => 'config/web.xml', :port => 3000, :jruby_min_runtimes => 1, :jruby_max_runtimes => 5, - :address => 'localhost' + :address => 'localhost', + :log => 'INFO' } end def initialize(config = {}) load_config(config) @@ -35,11 +34,11 @@ @tomcat.hostname = @config[:address] @tomcat.server.address = @config[:address] @tomcat.port = @config[:port].to_i @tomcat.base_dir = Dir.pwd @tomcat.host.app_base = Dir.pwd - enable_naming + @tomcat.enable_naming add_http_connector if http_configured? add_ssl_connector if ssl_enabled? add_ajp_connector if ajp_enabled? @@ -53,10 +52,11 @@ app_context = @tomcat.addWebapp(app_config[:context_path], app_config[:web_app_dir]) remove_defaults(app_context) web_app = WebApp.create(@config, app_config) + configure_logging(web_app) Trinidad::Extensions.configure_webapp_extensions(web_app.extensions, @tomcat, app_context) app_context.add_lifecycle_listener(WebAppLifecycleListener.new(web_app)) end end @@ -153,35 +153,19 @@ def add_default_web_app!(config) unless (config.has_key?(:web_apps)) default_app = { :context_path => config[:context_path] || '/', - :web_app_dir => config[:web_app_dir] || Dir.pwd + :web_app_dir => config[:web_app_dir] || Dir.pwd, + :log => config[:log] } default_app[:rackup] = config[:rackup] if (config.has_key?(:rackup)) config[:web_apps] = { :default => default_app } end end - def enable_naming - @tomcat.server.add_lifecycle_listener(Trinidad::Tomcat::NamingContextListener.new) - - JSystem.set_property("catalina.useNaming", "true") - - value = "org.apache.naming" - old_value = JSystem.get_property(JContext::URL_PKG_PREFIXES) || value - - value = value + ":" + old_value unless old_value.include?(value) - JSystem.set_property(JContext::URL_PKG_PREFIXES, value) - - value = JSystem.get_property(JContext::INITIAL_CONTEXT_FACTORY) - unless value - JSystem.set_property(JContext::INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory") - end - end - def remove_defaults(app_context) default_servlet = app_context.find_child('default') app_context.remove_child(default_servlet) if default_servlet jsp_servlet = app_context.find_child('jsp') @@ -190,8 +174,40 @@ app_context.remove_servlet_mapping('/') app_context.remove_servlet_mapping('*.jspx') app_context.remove_servlet_mapping('*.jsp') app_context.process_tlds = false + end + + def configure_logging(web_app) + log_path = File.join(web_app.web_app_dir, 'log', "#{web_app.environment}.log") + log_file = java.io.File.new(log_path) + + unless log_file.exists + log_file.parent_file.mkdirs + log_file.create_new_file + end + + jlogging = java.util.logging + + log_handler = jlogging.FileHandler.new(log_path, true) + logger = jlogging.Logger.get_logger("") + + log_level = web_app.log + unless %w{ALL CONFIG FINE FINER FINEST INFO OFF SEVERE WARNING}.include?(log_level) + puts "Invalid log level #{log_level}, using default: INFO" + log_level = 'INFO' + end + + level = jlogging.Level.parse(log_level) + + logger.handlers.each do |handler| + handler.level = level + end + + logger.level = level + + log_handler.formatter = jlogging.SimpleFormatter.new + logger.add_handler(log_handler) end end end