Module: Log4j2LoggerBridge

Extended by:
Helpers, Log4j2InitializationMethods, RubyLoggerInitializationMethods, Utils
Defined in:
lib/log4j2_logger_bridge/proxy.rb,
lib/log4j2_logger_bridge/utils.rb,
lib/log4j2_logger_bridge/backend.rb,
lib/log4j2_logger_bridge/helpers.rb,
lib/log4j2_logger_bridge/version.rb,
lib/log4j2_logger_bridge/constants.rb,
lib/log4j2_logger_bridge/injection.rb,
lib/log4j2_logger_bridge/destination.rb,
lib/log4j2_logger_bridge/log4j2_rolling_file.rb,
lib/log4j2_logger_bridge/log4j2_initialization_methods.rb,
lib/log4j2_logger_bridge/ruby_logger_initialization_methods.rb

Overview

The Log4j2LoggerBridge module

Defined Under Namespace

Modules: Constants, Destination, Helpers, Injection, Log4j2InitializationMethods, Log4j2RollingFile, RubyLoggerInitializationMethods, Utils Classes: Proxy

Constant Summary collapse

VERSION =
'1.1.1'.freeze

Constants included from RubyLoggerInitializationMethods

RubyLoggerInitializationMethods::TRACE_SEVERITY, RubyLoggerInitializationMethods::TRACE_STRING

Constants included from Constants

Constants::DEFAULT_APP_NAME, Constants::DEFAULT_LOGS_DIR_NAME, Constants::DEFAULT_LOG_PATTERN, Constants::DEFAULT_ROLLOVER_SCHEDULE, Constants::DEFAULT_ROLLOVER_SIZE, Constants::DEFAULT_RUBY_LOG_FORMAT, Constants::DEFAULT_TIMESTAMP_FORMAT, Constants::EMPTY_STRING, Constants::NUMERIC_TO_SYMBOL, Constants::SYMBOL_TO_RUBY

Class Method Summary collapse

Methods included from Utils

jruby?

Methods included from RubyLoggerInitializationMethods

init_ruby_logger_backend, install_trace_level!, ruby_formatter, severity_label

Methods included from Log4j2InitializationMethods

configure_log4j, derive_category, init_config, init_log4j2!, init_log4j_backend, log4j_configuration

Methods included from Helpers

app_logger_name, app_name, int_to_symbol_level, java_log_pattern, ruby_log_format, symbol_to_java_level, symbol_to_ruby_logger_level, timestamp_format

Class Method Details

.app_config(context_configuration, sym) ⇒ Object



146
147
148
149
150
151
# File 'lib/log4j2_logger_bridge/backend.rb', line 146

def app_config(context_configuration, sym)
  app_cfg = context_configuration.getLoggerConfig(app_logger_name)
  return app_cfg if !app_cfg.nil? && app_cfg.getName == app_logger_name

  init_app_logging_config(context_configuration, app_logger_name, sym)
end

.apply_backend_level(sym) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/log4j2_logger_bridge/backend.rb', line 133

def apply_backend_level(sym)
  if Utils.jruby?
    ctx = @log4j2_context || LoggerContext.getContext(false)
    app_cfg = app_config(ctx.getConfiguration, sym)
    app_cfg.setLevel(symbol_to_java_level(sym))
    ctx.updateLoggers
  else
    backend.level = symbol_to_ruby_logger_level(sym)
  end

  sym
end

.backendObject



41
42
43
# File 'lib/log4j2_logger_bridge/backend.rb', line 41

def backend
  @backend ||= init_backend
end

.configure_destination(&block) ⇒ Object

Public API: configure file destination + rollover. Example: Log4j2LoggerBridge.configure_destination do |c| c.project_dir_path = File.realpath(dir) c.rollover_size = '100M' end



60
61
62
# File 'lib/log4j2_logger_bridge/backend.rb', line 60

def configure_destination(&block)
  Log4j2LoggerBridge::Destination.configure(&block)
end

.dispatch(level, *args, &block) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/log4j2_logger_bridge/backend.rb', line 77

def dispatch(level, *args, &block)
  return backend.public_send(level, *args, &block) if Utils.jruby?

  case level
  when :fatal then backend.fatal(*args, &block)
  when :warn then backend.warn(*args, &block)
  else
    backend.public_send(level, *args, &block)
  end
end

.init_app_logging_config(context_configuration, app_logger_name, sym) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/log4j2_logger_bridge/backend.rb', line 153

def init_app_logging_config(context_configuration, app_logger_name, sym)
  app_cfg = LoggerConfig.newBuilder.withConfig(context_configuration)
    .withLoggerName(app_logger_name).withLevel(symbol_to_java_level(sym))
    .withAdditivity(false).build
  if (appender = context_configuration.getAppender('stdout')).nil?
    app_cfg.setAdditive(true)
  else
    app_cfg.addAppender(appender, Level::INFO, nil)
  end
  context_configuration.addLogger(app_logger_name, app_cfg)
  app_cfg
end

.init_backendObject



49
50
51
52
# File 'lib/log4j2_logger_bridge/backend.rb', line 49

def init_backend
  return init_log4j_backend if Utils.jruby?
  init_ruby_logger_backend
end

.log_file_pathObject



73
74
75
# File 'lib/log4j2_logger_bridge/backend.rb', line 73

def log_file_path
  Log4j2LoggerBridge::Destination.log_file_path
end

.log_levelObject

rubocop: enable Metrics/MethodLength



116
117
118
# File 'lib/log4j2_logger_bridge/backend.rb', line 116

def log_level
  @log_level ||= :info
end

.log_level=(level) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/log4j2_logger_bridge/backend.rb', line 120

def log_level=(level)
  @log_level = case level
  when Integer
    int_to_symbol_level(level)
  when Symbol, String
    level.to_s.downcase.to_sym
  else
    :info
  end

  apply_backend_level(@log_level)
end

.logs_dir_pathObject



69
70
71
# File 'lib/log4j2_logger_bridge/backend.rb', line 69

def logs_dir_path
  Log4j2LoggerBridge::Destination.logs_dir_path
end

.proxyObject



45
46
47
# File 'lib/log4j2_logger_bridge/backend.rb', line 45

def proxy
  @proxy ||= Proxy.new
end

.rolloverObject

Convenience: direct access to rollover settings.



65
66
67
# File 'lib/log4j2_logger_bridge/backend.rb', line 65

def rollover
  Log4j2LoggerBridge::Destination.rollover
end

.with_category(category) ⇒ Object

rubocop: disable Metrics/MethodLength



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/log4j2_logger_bridge/backend.rb', line 89

def with_category(category)
  if Utils.jruby?
    prev = ThreadContext.get('category')

    begin
      ThreadContext.put('category', category)
      yield
    ensure
      if prev.nil?
        ThreadContext.remove('category')
      else
        ThreadContext.put('category', prev)
      end
    end
  else
    prev = Thread.current[:logging_category]

    begin
      Thread.current[:logging_category] = category
      yield
    ensure
      Thread.current[:logging_category] = prev
    end
  end
end