Module: Trinidad::Logging

Defined in:
lib/trinidad/logging.rb

Constant Summary collapse

JUL =
Java::JavaUtilLogging
LogFactory =
Java::OrgApacheJuliLogging::LogFactory
@@configured =
nil

Class Method Summary collapse

Class Method Details

.configure(log_level = nil) ⇒ Object

Configure the “global” Trinidad logging.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/trinidad/logging.rb', line 13

def self.configure(log_level = nil)
  return false if @@configured
  @@configured = true

  root_logger = JUL::Logger.getLogger('')
  level = parse_log_level(log_level, :INFO)

  out_handler = new_console_handler JRuby.runtime.out
  out_handler.formatter = console_formatter

  root_logger.synchronized do
    root_logger.handlers.to_a.each do |handler|
      root_logger.remove_handler(handler) if handler.is_a?(JUL::ConsoleHandler)
    end

    root_logger.add_handler(out_handler)
    if JRuby.runtime.out != Java::JavaLang::System.out ||
       JRuby.runtime.err != Java::JavaLang::System.err
     # NOTE: only add err handler if customized STDOUT or STDERR :
    err_handler = new_console_handler JRuby.runtime.err
    err_handler.formatter = console_formatter
    err_handler.level = level.intValue > JUL::Level::WARNING.intValue ?
      level : JUL::Level::WARNING # only >= WARNING on STDERR

     root_logger.add_handler(err_handler)
    end
    set_log_level(root_logger, level)
  end
  silence_tomcat_loggers

  root_logger
end

.configure!(log_level = nil) ⇒ Object

Force logging (re-)configuration.

See Also:

  • #configure


48
49
50
# File 'lib/trinidad/logging.rb', line 48

def self.configure!(log_level = nil)
  ( @@configured = false ) || configure(log_level)
end

.configure_web_app(web_app, context, reset = nil) ⇒ Object

Configure logging for a web application.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/trinidad/logging.rb', line 57

def self.configure_web_app(web_app, context, reset = nil)
  param_name, param_value = 'jruby.rack.logging', 'JUL'
  # 1. delegate (jruby-rack) servlet log to JUL
  if set_value = web_app_context_param(web_app, context, param_name)
    return nil if set_value.upcase != param_value
  else
    context.add_parameter(param_name, param_value)
  end
  # 2. use Tomcat's JUL logger name (unless set) :
  param_name = 'jruby.rack.logging.name'
  unless logger_name = web_app_context_param(web_app, context, param_name)
    # for a context path e.g. '/foo' most likely smt of the following :
    # org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/foo]
    context.add_parameter(param_name, logger_name = context.send(:logName))
  end
  configure # make sure 'global' logging is configured

  logger = JUL::Logger.getLogger(logger_name) # exclusive for web app
  logger.handlers.each { |h| logger.remove_handler(h); h.close } if reset
  # avoid duplicate calls - do not configure (e.g. FileHandler) twice :
  return false unless logger.handlers.empty?

  logging = web_app.logging

  logger.level = parse_log_level(logging[:level], nil)
  # delegate to root (console) output only in development mode :
  logger.use_parent_handlers = logging[:use_parent_handlers]
  # logging:
  #  file:
  #    dir: log # [RAILS_ROOT]/log
  #    prefix: production
  #    suffix: .log
  if file = logging[:file]
    prefix, suffix = file[:prefix], file[:suffix] # {prefix}{date}{suffix}
    file_handler = FileHandler.new(file[:dir] || file[:directory], prefix, suffix)
    file_handler.rotatable = file.key?(:rotatable) ? file[:rotatable] : file[:rotate]
    file_handler.buffer_size = file[:buffer_size] if file[:buffer_size]
    format = file.key?(:format) ? file[:format] : logging[:format]
    file_handler.formatter = web_app_formatter(format) # nil uses default
    logger.add_handler(file_handler)
  end
  logger
end

.configure_web_app!(web_app, context) ⇒ Object



52
53
54
# File 'lib/trinidad/logging.rb', line 52

def self.configure_web_app!(web_app, context)
  configure_web_app!(web_app, context, true)
end