log4j2_logger_bridge 1.0.1-java
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +8 -0
- data/Rakefile +65 -0
- data/lib/log4j2_logger_bridge/backend.rb +142 -0
- data/lib/log4j2_logger_bridge/constants.rb +40 -0
- data/lib/log4j2_logger_bridge/helpers.rb +66 -0
- data/lib/log4j2_logger_bridge/injection.rb +19 -0
- data/lib/log4j2_logger_bridge/log4j2_initialization_methods.rb +72 -0
- data/lib/log4j2_logger_bridge/proxy.rb +29 -0
- data/lib/log4j2_logger_bridge/ruby_logger_initialization_methods.rb +56 -0
- data/lib/log4j2_logger_bridge/utils.rb +16 -0
- data/lib/log4j2_logger_bridge/version.rb +9 -0
- data/lib/log4j2_logger_bridge.rb +9 -0
- metadata +83 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: f03bc99125d4e93d9c2b50ffa1e105881bf26d933faeb179caa55cda476bf0c0
|
|
4
|
+
data.tar.gz: 6d2de401a6ea49ca9f932edfc2852da7152c5eb86c52fe27d0626e945b4826ac
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 544eb01b920127d42859a28a96b3e31da2091e589735de2d8ebedb9999e580fbf5c3d318b456890b885aabe1c709a3942f1250e48a041d0562b00238ae1005da
|
|
7
|
+
data.tar.gz: cb8ddfbd3ae21e86376a89695d14dbff8b4e137fb38039238d4f8e2004623b4e2f24fa1d691484d9a82f73c72daea8a73aafeedf8802777c522142ee0acd18f3
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-2026 Nels Nelson
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# log4j2_logger_bridge
|
|
2
|
+
|
|
3
|
+
This gem is a bridge between Apache log4j-2 and the Ruby logger gem.
|
|
4
|
+
|
|
5
|
+
It attempts to provide a single unified interface for both logging
|
|
6
|
+
technologies, so that switching between either backend can be accomplished
|
|
7
|
+
without much effort.
|
|
8
|
+
|
data/Rakefile
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
# frozen_string_literal: false
|
|
3
|
+
|
|
4
|
+
# -*- mode: ruby -*-
|
|
5
|
+
# vi: set ft=ruby :
|
|
6
|
+
|
|
7
|
+
require 'rake'
|
|
8
|
+
require 'rake/clean'
|
|
9
|
+
|
|
10
|
+
PROJECT = File.basename(__dir__) unless defined?(PROJECT)
|
|
11
|
+
|
|
12
|
+
load "#{PROJECT}.gemspec"
|
|
13
|
+
|
|
14
|
+
CLEAN.add File.join('tmp', '**', '*'), 'tmp'
|
|
15
|
+
CLOBBER.add '*.gem', 'pkg'
|
|
16
|
+
|
|
17
|
+
task default: %i[package]
|
|
18
|
+
|
|
19
|
+
desc 'Run the rubocop linter'
|
|
20
|
+
task :lint do
|
|
21
|
+
system('bundle', 'exec', 'rubocop') or abort
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
desc 'Run the spec tests'
|
|
25
|
+
task :test do
|
|
26
|
+
system('bundle', 'exec', 'rspec') or abort
|
|
27
|
+
end
|
|
28
|
+
task test: :lint
|
|
29
|
+
|
|
30
|
+
desc 'Explode the gem'
|
|
31
|
+
task :explode do
|
|
32
|
+
system('jgem', 'install', '--no-document', '--install-dir=tmp', '*.gem')
|
|
33
|
+
end
|
|
34
|
+
task explode: :clean
|
|
35
|
+
|
|
36
|
+
desc 'Package the gem'
|
|
37
|
+
task :package do
|
|
38
|
+
system('jgem', 'build')
|
|
39
|
+
end
|
|
40
|
+
task package: %i[clean clobber test]
|
|
41
|
+
|
|
42
|
+
desc 'Verify the gem'
|
|
43
|
+
task :verify do
|
|
44
|
+
env = {
|
|
45
|
+
'GEM_HOME' => 'tmp',
|
|
46
|
+
'GEM_PATH' => 'tmp',
|
|
47
|
+
'BUNDLE_GEMFILE' => nil,
|
|
48
|
+
'RUBYOPT' => nil
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
# cmd = ['jruby', '-S', 'rspec', 'spec/verify']
|
|
52
|
+
cmd = ['bundle', 'exec', 'rspec', 'spec/verify']
|
|
53
|
+
system(env, *cmd) or abort
|
|
54
|
+
end
|
|
55
|
+
task verify: :explode
|
|
56
|
+
|
|
57
|
+
desc 'Publish the gem'
|
|
58
|
+
task :publish do
|
|
59
|
+
system('jgem', 'push', latest_gem)
|
|
60
|
+
end
|
|
61
|
+
task publish: :verify
|
|
62
|
+
|
|
63
|
+
def latest_gem
|
|
64
|
+
`ls -t #{GEM_SPEC.name}*.gem`.strip.split("\n").first
|
|
65
|
+
end
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
# frozen_string_literal: false
|
|
3
|
+
|
|
4
|
+
# -*- mode: ruby -*-
|
|
5
|
+
# vi: set ft=ruby :
|
|
6
|
+
|
|
7
|
+
require_relative 'utils'
|
|
8
|
+
|
|
9
|
+
if Log4j2LoggerBridge::Utils.jruby?
|
|
10
|
+
require 'java'
|
|
11
|
+
require 'apache-log4j-2'
|
|
12
|
+
|
|
13
|
+
java_import 'org.apache.logging.log4j.Level'
|
|
14
|
+
java_import 'org.apache.logging.log4j.core.appender.ConsoleAppender'
|
|
15
|
+
java_import 'org.apache.logging.log4j.core.config.builder.api.' \
|
|
16
|
+
'ConfigurationBuilderFactory'
|
|
17
|
+
java_import 'org.apache.logging.log4j.core.config.Configurator'
|
|
18
|
+
java_import 'org.apache.logging.log4j.core.config.LoggerConfig'
|
|
19
|
+
java_import 'org.apache.logging.log4j.core.LoggerContext'
|
|
20
|
+
java_import 'org.apache.logging.log4j.LogManager'
|
|
21
|
+
java_import 'org.apache.logging.log4j.ThreadContext'
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
require 'fileutils'
|
|
25
|
+
require 'logger'
|
|
26
|
+
|
|
27
|
+
require_relative 'helpers'
|
|
28
|
+
require_relative 'injection'
|
|
29
|
+
require_relative 'log4j2_initialization_methods'
|
|
30
|
+
require_relative 'ruby_logger_initialization_methods'
|
|
31
|
+
require_relative 'proxy'
|
|
32
|
+
|
|
33
|
+
# The Log4j2LoggerBridge module
|
|
34
|
+
module Log4j2LoggerBridge
|
|
35
|
+
extend RubyLoggerInitializationMethods unless Utils.jruby?
|
|
36
|
+
extend Log4j2InitializationMethods if Utils.jruby?
|
|
37
|
+
|
|
38
|
+
module_function
|
|
39
|
+
|
|
40
|
+
def backend
|
|
41
|
+
@backend ||= init_backend
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def proxy
|
|
45
|
+
@proxy ||= Proxy.new
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def init_backend
|
|
49
|
+
return init_log4j_backend if Utils.jruby?
|
|
50
|
+
init_ruby_logger_backend
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def dispatch(level, *args, &block)
|
|
54
|
+
return backend.public_send(level, *args, &block) if Utils.jruby?
|
|
55
|
+
|
|
56
|
+
case level
|
|
57
|
+
when :fatal then backend.fatal(*args, &block)
|
|
58
|
+
when :warn then backend.warn(*args, &block)
|
|
59
|
+
else
|
|
60
|
+
backend.public_send(level, *args, &block)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# rubocop: disable Metrics/MethodLength
|
|
65
|
+
def with_category(category)
|
|
66
|
+
if Utils.jruby?
|
|
67
|
+
prev = ThreadContext.get('category')
|
|
68
|
+
|
|
69
|
+
begin
|
|
70
|
+
ThreadContext.put('category', category)
|
|
71
|
+
yield
|
|
72
|
+
ensure
|
|
73
|
+
if prev.nil?
|
|
74
|
+
ThreadContext.remove('category')
|
|
75
|
+
else
|
|
76
|
+
ThreadContext.put('category', prev)
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
else
|
|
80
|
+
prev = Thread.current[:logging_category]
|
|
81
|
+
|
|
82
|
+
begin
|
|
83
|
+
Thread.current[:logging_category] = category
|
|
84
|
+
yield
|
|
85
|
+
ensure
|
|
86
|
+
Thread.current[:logging_category] = prev
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
# rubocop: enable Metrics/MethodLength
|
|
91
|
+
|
|
92
|
+
def log_level
|
|
93
|
+
@log_level ||= :info
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def log_level=(level)
|
|
97
|
+
@log_level = case level
|
|
98
|
+
when Integer
|
|
99
|
+
int_to_symbol_level(level)
|
|
100
|
+
when Symbol, String
|
|
101
|
+
level.to_s.downcase.to_sym
|
|
102
|
+
else
|
|
103
|
+
:info
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
apply_backend_level(@log_level)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def apply_backend_level(sym)
|
|
110
|
+
if Utils.jruby?
|
|
111
|
+
ctx = @log4j2_context || LoggerContext.getContext(false)
|
|
112
|
+
app_cfg = app_config(ctx.getConfiguration, sym)
|
|
113
|
+
app_cfg.setLevel(symbol_to_java_level(sym))
|
|
114
|
+
ctx.updateLoggers
|
|
115
|
+
else
|
|
116
|
+
backend.level = symbol_to_ruby_logger_level(sym)
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
sym
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def app_config(context_configuration, sym)
|
|
123
|
+
app_cfg = context_configuration.getLoggerConfig(app_logger_name)
|
|
124
|
+
return app_cfg if !app_cfg.nil? && app_cfg.getName == app_logger_name
|
|
125
|
+
|
|
126
|
+
init_app_logging_config(context_configuration, app_logger_name, sym)
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def init_app_logging_config(context_configuration, app_logger_name, sym)
|
|
130
|
+
app_cfg = LoggerConfig.newBuilder.withConfig(context_configuration)
|
|
131
|
+
.withLoggerName(app_logger_name).withLevel(symbol_to_java_level(sym))
|
|
132
|
+
.withAdditivity(false).build
|
|
133
|
+
if (appender = context_configuration.getAppender('stdout')).nil?
|
|
134
|
+
app_cfg.setAdditive(true)
|
|
135
|
+
else
|
|
136
|
+
app_cfg.addAppender(appender, Level::INFO, nil)
|
|
137
|
+
end
|
|
138
|
+
context_configuration.addLogger(app_logger_name, app_cfg)
|
|
139
|
+
app_cfg
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
# end Log4j2LoggerBridge
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
# frozen_string_literal: false
|
|
3
|
+
|
|
4
|
+
# -*- mode: ruby -*-
|
|
5
|
+
# vi: set ft=ruby :
|
|
6
|
+
|
|
7
|
+
# The Log4j2LoggerBridge module
|
|
8
|
+
module Log4j2LoggerBridge
|
|
9
|
+
# The Constants module
|
|
10
|
+
module Constants
|
|
11
|
+
DEFAULT_TIME_FORMAT = '%Y-%m-%d %H:%M:%S'.freeze
|
|
12
|
+
DEFAULT_APP_NAME = 'app'.freeze
|
|
13
|
+
DEFAULT_LOG_PATTERN = '%d{ABSOLUTE} %-5p [%X{category}] %m%n'.freeze
|
|
14
|
+
DEFAULT_RUBY_LOG_FORMAT =
|
|
15
|
+
"%<timestamp>s %-5<severity>s [%<category>s] %<msg>s\n".freeze
|
|
16
|
+
EMPTY_STRING = ''.freeze
|
|
17
|
+
|
|
18
|
+
NUMERIC_TO_SYMBOL = {
|
|
19
|
+
5 => :off,
|
|
20
|
+
4 => :fatal,
|
|
21
|
+
3 => :error,
|
|
22
|
+
2 => :warn,
|
|
23
|
+
1 => :info,
|
|
24
|
+
0 => :debug,
|
|
25
|
+
-1 => :trace
|
|
26
|
+
}.freeze
|
|
27
|
+
|
|
28
|
+
SYMBOL_TO_RUBY = {
|
|
29
|
+
off: Logger::FATAL,
|
|
30
|
+
fatal: Logger::FATAL,
|
|
31
|
+
error: Logger::ERROR,
|
|
32
|
+
warn: Logger::WARN,
|
|
33
|
+
info: Logger::INFO,
|
|
34
|
+
debug: Logger::DEBUG,
|
|
35
|
+
trace: Logger::DEBUG,
|
|
36
|
+
all: Logger::DEBUG
|
|
37
|
+
}.freeze
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
# module Log4j2LoggerBridge
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
# frozen_string_literal: false
|
|
3
|
+
|
|
4
|
+
# -*- mode: ruby -*-
|
|
5
|
+
# vi: set ft=ruby :
|
|
6
|
+
|
|
7
|
+
require_relative 'constants'
|
|
8
|
+
|
|
9
|
+
# The Log4j2LoggerBridge module
|
|
10
|
+
module Log4j2LoggerBridge
|
|
11
|
+
# The Helpers module
|
|
12
|
+
module Helpers
|
|
13
|
+
include Constants
|
|
14
|
+
|
|
15
|
+
module_function
|
|
16
|
+
|
|
17
|
+
def app_name
|
|
18
|
+
DEFAULT_APP_NAME
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def app_logger_name
|
|
22
|
+
DEFAULT_APP_NAME
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def time_format
|
|
26
|
+
DEFAULT_TIME_FORMAT
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def ruby_log_format
|
|
30
|
+
DEFAULT_RUBY_LOG_FORMAT
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def java_log_pattern
|
|
34
|
+
DEFAULT_LOG_PATTERN
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def int_to_symbol_level(level)
|
|
38
|
+
NUMERIC_TO_SYMBOL.fetch(level) do
|
|
39
|
+
level > 5 ? :off : :all
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# rubocop: disable Metrics/CyclomaticComplexity
|
|
44
|
+
def symbol_to_java_level(sym)
|
|
45
|
+
case sym
|
|
46
|
+
when :off then Level::OFF
|
|
47
|
+
when :fatal then Level::FATAL
|
|
48
|
+
when :error then Level::ERROR
|
|
49
|
+
when :warn then Level::WARN
|
|
50
|
+
when :debug then Level::DEBUG
|
|
51
|
+
when :trace then Level::TRACE
|
|
52
|
+
when :all then Level::ALL
|
|
53
|
+
else Level::INFO
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
# rubocop: enable Metrics/CyclomaticComplexity
|
|
57
|
+
|
|
58
|
+
def symbol_to_ruby_logger_level(sym, default_level = Logger::INFO)
|
|
59
|
+
SYMBOL_TO_RUBY.fetch(sym, default_level)
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
# module Helpers
|
|
63
|
+
|
|
64
|
+
extend Helpers
|
|
65
|
+
end
|
|
66
|
+
# module Log4j2LoggerBridge
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
# frozen_string_literal: false
|
|
3
|
+
|
|
4
|
+
# -*- mode: ruby -*-
|
|
5
|
+
# vi: set ft=ruby :
|
|
6
|
+
|
|
7
|
+
# The Log4j2LoggerBridge module
|
|
8
|
+
module Log4j2LoggerBridge
|
|
9
|
+
# The Injection module
|
|
10
|
+
module Injection
|
|
11
|
+
# Make `log` and `logger` available everywhere, capturing receiver + callsite.
|
|
12
|
+
def logger
|
|
13
|
+
Thread.current[:logging_receiver] = self
|
|
14
|
+
Thread.current[:logging_callsite] = caller_locations(1, 1).first
|
|
15
|
+
Log4j2LoggerBridge.proxy
|
|
16
|
+
end
|
|
17
|
+
alias log logger
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
# frozen_string_literal: false
|
|
3
|
+
|
|
4
|
+
# -*- mode: ruby -*-
|
|
5
|
+
# vi: set ft=ruby :
|
|
6
|
+
|
|
7
|
+
return unless Log4j2LoggerBridge::Utils.jruby?
|
|
8
|
+
|
|
9
|
+
# The Log4j2LoggerBridge module
|
|
10
|
+
module Log4j2LoggerBridge
|
|
11
|
+
# The Log4j2InitializationMethods module
|
|
12
|
+
module Log4j2InitializationMethods
|
|
13
|
+
def init_log4j_backend
|
|
14
|
+
init_log4j2!
|
|
15
|
+
apply_backend_level(log_level)
|
|
16
|
+
LogManager.getLogger(app_logger_name)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def init_log4j2!
|
|
20
|
+
return if @log4j2_initialized
|
|
21
|
+
|
|
22
|
+
Java::java.lang.System.setProperty(
|
|
23
|
+
'log4j.shutdownHookEnabled', Java::java.lang.Boolean.toString(false))
|
|
24
|
+
|
|
25
|
+
configure_log4j
|
|
26
|
+
|
|
27
|
+
@log4j2_initialized = true
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def configure_log4j
|
|
31
|
+
@log4j2_context = Configurator.initialize(log4j_configuration.build)
|
|
32
|
+
@log4j2_context.updateLoggers
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# rubocop: disable Metrics/AbcSize
|
|
36
|
+
# rubocop: disable Metrics/MethodLength
|
|
37
|
+
def log4j_configuration(
|
|
38
|
+
config = ConfigurationBuilderFactory.newConfigurationBuilder)
|
|
39
|
+
layout = config.newLayout('PatternLayout')
|
|
40
|
+
.addAttribute('pattern', java_log_pattern)
|
|
41
|
+
console = config.newAppender('stdout', 'CONSOLE')
|
|
42
|
+
.addAttribute('target', ConsoleAppender::Target::SYSTEM_OUT).add(layout)
|
|
43
|
+
config.add(console)
|
|
44
|
+
root = config.newRootLogger(Level::INFO)
|
|
45
|
+
.add(config.newAppenderRef('stdout'))
|
|
46
|
+
config.add(root)
|
|
47
|
+
app_logger = config.newLogger(app_logger_name, Level::INFO)
|
|
48
|
+
.add(config.newAppenderRef('stdout'))
|
|
49
|
+
.addAttribute('additivity', false)
|
|
50
|
+
config.add(app_logger)
|
|
51
|
+
config
|
|
52
|
+
end
|
|
53
|
+
# rubocop: enable Metrics/AbcSize
|
|
54
|
+
# rubocop: enable Metrics/MethodLength
|
|
55
|
+
|
|
56
|
+
def derive_category(receiver, callsite)
|
|
57
|
+
if receiver.is_a?(Module)
|
|
58
|
+
receiver.name.to_s
|
|
59
|
+
elsif receiver.respond_to?(:class) && receiver.class.respond_to?(:name)
|
|
60
|
+
receiver.class.name.to_s
|
|
61
|
+
else
|
|
62
|
+
Log4j2LoggerBridge::Constants::EMPTY_STRING
|
|
63
|
+
end.tap do |cat|
|
|
64
|
+
return cat unless cat.empty?
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
File.basename(callsite.path.to_s)
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
# module RubyLoggerInitializationMethods
|
|
71
|
+
end
|
|
72
|
+
# module Log4j2LoggerBridge
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
# frozen_string_literal: false
|
|
3
|
+
|
|
4
|
+
# -*- mode: ruby -*-
|
|
5
|
+
# vi: set ft=ruby :
|
|
6
|
+
|
|
7
|
+
# The Log4j2LoggerBridge module
|
|
8
|
+
module Log4j2LoggerBridge
|
|
9
|
+
# The Proxy class
|
|
10
|
+
class Proxy
|
|
11
|
+
SEVERITIES = %i[trace debug info warn error fatal].freeze
|
|
12
|
+
|
|
13
|
+
def method_missing(method_name, *args, &block)
|
|
14
|
+
return super unless SEVERITIES.include?(method_name)
|
|
15
|
+
|
|
16
|
+
receiver = Thread.current[:logging_receiver]
|
|
17
|
+
callsite = Thread.current[:logging_callsite] || caller_locations(2, 1).first
|
|
18
|
+
category = Log4j2LoggerBridge.derive_category(receiver, callsite)
|
|
19
|
+
|
|
20
|
+
Log4j2LoggerBridge.with_category(category) do
|
|
21
|
+
Log4j2LoggerBridge.dispatch(method_name, *args, &block)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def respond_to_missing?(method_name, include_private = false)
|
|
26
|
+
SEVERITIES.include?(method_name) || super
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
# frozen_string_literal: false
|
|
3
|
+
|
|
4
|
+
return if Log4j2LoggerBridge::Utils.jruby?
|
|
5
|
+
|
|
6
|
+
# The Log4j2LoggerBridge module
|
|
7
|
+
module Log4j2LoggerBridge
|
|
8
|
+
# The RubyLoggerInitializationMethods module
|
|
9
|
+
module RubyLoggerInitializationMethods
|
|
10
|
+
TRACE_STRING = 'TRACE'.freeze
|
|
11
|
+
TRACE_SEVERITY = -1
|
|
12
|
+
|
|
13
|
+
def init_ruby_logger_backend
|
|
14
|
+
ruby_logger = Logger.new($stdout)
|
|
15
|
+
ruby_logger.formatter = method(:ruby_formatter)
|
|
16
|
+
@backend = ruby_logger
|
|
17
|
+
install_trace_level!
|
|
18
|
+
apply_backend_level(log_level)
|
|
19
|
+
ruby_logger
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def ruby_formatter(severity, datetime, _progname, msg)
|
|
23
|
+
category = (Thread.current[:logging_category] || app_name).to_s
|
|
24
|
+
timestamp = datetime.strftime(time_format)
|
|
25
|
+
format(ruby_log_format, timestamp: timestamp,
|
|
26
|
+
severity: severity_label(severity), category: category, msg: msg)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def severity_label(severity)
|
|
30
|
+
return TRACE_STRING if severity == ::Logger::Severity::TRACE
|
|
31
|
+
::Logger::SEV_LABEL[severity] || severity.to_s
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# rubocop: disable Metrics/MethodLength
|
|
35
|
+
def install_trace_level!(sev = ::Logger::Severity)
|
|
36
|
+
sev.const_set(:TRACE, TRACE_SEVERITY) unless sev.const_defined?(:TRACE)
|
|
37
|
+
|
|
38
|
+
::Logger.class_eval do
|
|
39
|
+
unless method_defined?(:trace)
|
|
40
|
+
def trace(progname = nil, &block)
|
|
41
|
+
add(sev::TRACE, nil, progname, &block)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
unless method_defined?(:trace?)
|
|
46
|
+
def trace?
|
|
47
|
+
level <= sev::TRACE
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
# rubocop: enable Metrics/MethodLength
|
|
53
|
+
end
|
|
54
|
+
# module RubyLoggerInitializationMethods
|
|
55
|
+
end
|
|
56
|
+
# module Log4j2LoggerBridge
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
# frozen_string_literal: false
|
|
3
|
+
|
|
4
|
+
# The Log4j2LoggerBridge module
|
|
5
|
+
module Log4j2LoggerBridge
|
|
6
|
+
# The Utils module
|
|
7
|
+
module Utils
|
|
8
|
+
module_function
|
|
9
|
+
|
|
10
|
+
def jruby?
|
|
11
|
+
defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby' && defined?(Java)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
extend Utils
|
|
16
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: log4j2_logger_bridge
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.1
|
|
5
|
+
platform: java
|
|
6
|
+
authors:
|
|
7
|
+
- Nels Nelson
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: apache-log4j-2
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - "~>"
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: 2.25.3
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - "~>"
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: 2.25.3
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: logger
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - "~>"
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '1.7'
|
|
33
|
+
type: :runtime
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - "~>"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '1.7'
|
|
40
|
+
description: Bridge between Log4j2 and the Ruby Logger.
|
|
41
|
+
email: nels@nelsnelson.org
|
|
42
|
+
executables: []
|
|
43
|
+
extensions: []
|
|
44
|
+
extra_rdoc_files: []
|
|
45
|
+
files:
|
|
46
|
+
- LICENSE
|
|
47
|
+
- README.md
|
|
48
|
+
- Rakefile
|
|
49
|
+
- lib/log4j2_logger_bridge.rb
|
|
50
|
+
- lib/log4j2_logger_bridge/backend.rb
|
|
51
|
+
- lib/log4j2_logger_bridge/constants.rb
|
|
52
|
+
- lib/log4j2_logger_bridge/helpers.rb
|
|
53
|
+
- lib/log4j2_logger_bridge/injection.rb
|
|
54
|
+
- lib/log4j2_logger_bridge/log4j2_initialization_methods.rb
|
|
55
|
+
- lib/log4j2_logger_bridge/proxy.rb
|
|
56
|
+
- lib/log4j2_logger_bridge/ruby_logger_initialization_methods.rb
|
|
57
|
+
- lib/log4j2_logger_bridge/utils.rb
|
|
58
|
+
- lib/log4j2_logger_bridge/version.rb
|
|
59
|
+
homepage: https://rubygems.org/gems/log4j2_logger_bridge
|
|
60
|
+
licenses:
|
|
61
|
+
- MIT
|
|
62
|
+
metadata:
|
|
63
|
+
source_code_uri: https://gitlab.com/nelsnelson/log4j2_logger_bridge
|
|
64
|
+
bug_tracker_uri: https://gitlab.com/nelsnelson/log4j2_logger_bridge/issues
|
|
65
|
+
rubygems_mfa_required: 'true'
|
|
66
|
+
rdoc_options: []
|
|
67
|
+
require_paths:
|
|
68
|
+
- lib
|
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
70
|
+
requirements:
|
|
71
|
+
- - ">="
|
|
72
|
+
- !ruby/object:Gem::Version
|
|
73
|
+
version: 2.6.8
|
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
75
|
+
requirements:
|
|
76
|
+
- - ">="
|
|
77
|
+
- !ruby/object:Gem::Version
|
|
78
|
+
version: '0'
|
|
79
|
+
requirements: []
|
|
80
|
+
rubygems_version: 3.6.9
|
|
81
|
+
specification_version: 4
|
|
82
|
+
summary: Bridge between Log4j2 and the Ruby Logger packaged as a gem.
|
|
83
|
+
test_files: []
|