log_sinks 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +20 -0
- data/lib/log_sinks/event_drain.rb +26 -0
- data/lib/log_sinks/filter.rb +13 -0
- data/lib/log_sinks/filters/level_filter.rb +25 -0
- data/lib/log_sinks/filters/logger_name_prefix_filter.rb +21 -0
- data/lib/log_sinks/formatter.rb +16 -0
- data/lib/log_sinks/io_sink.rb +22 -0
- data/lib/log_sinks/level.rb +52 -0
- data/lib/log_sinks/log_event.rb +34 -0
- data/lib/log_sinks/logger.rb +80 -0
- data/lib/log_sinks/mdc.rb +38 -0
- data/lib/log_sinks/pumps/dedicated_thread_pump.rb +57 -0
- data/lib/log_sinks/pumps/in_thread_pump.rb +16 -0
- data/lib/log_sinks/repository.rb +51 -0
- data/lib/log_sinks/ruby_logger_adapter.rb +68 -0
- data/lib/log_sinks/sink.rb +44 -0
- data/lib/log_sinks/version.rb +5 -0
- data/lib/log_sinks.rb +9 -0
- metadata +189 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1c36d2668770385f86b0fe13843d91fae5f9f2164d53d9a42aaca1a6d0859242
|
4
|
+
data.tar.gz: 1b52160fef8827b617b7fb00b728b97b3641c3e5c70ef47b61e8e10b86e154a5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3c51e5bb67d705cbd01795883e8e8b3fa3a397e7bd82690c306f70c52fe41eea78c16a0cfc829fcbaf4a3c814716dca9b32c3bd30138f0b2ec9fdc40b25617c9
|
7
|
+
data.tar.gz: e3bbfb26c3100466d1d95232d2512467f319b32371dfe4b07e261a26e90721b1fd29753b88dc6b6dcc0374a4cfae14713e3b6b54e3c9749c03c16c7a604e1fd9
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2022 Andrey Maraev
|
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,20 @@
|
|
1
|
+
# log_sinks
|
2
|
+
|
3
|
+
## Description
|
4
|
+
|
5
|
+
**log_sinks** is a powerful yet easy to use logging library.
|
6
|
+
The library is designed with multiple base principles:
|
7
|
+
|
8
|
+
* Be a drop-in replacement for Ruby `Logger`
|
9
|
+
* Advanced API to support structured logging
|
10
|
+
* Highly efficient non-blocking logger API
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
```shell
|
15
|
+
gem install log_sinks
|
16
|
+
```
|
17
|
+
|
18
|
+
## License
|
19
|
+
|
20
|
+
The MIT License. See the [LICENSE](/LICENSE) file for the full text.
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'singleton'
|
4
|
+
|
5
|
+
module LogSinks
|
6
|
+
# EventDrain is a single receiver of messages from loggers. EventDrain forward events to registered sinks
|
7
|
+
class EventDrain
|
8
|
+
include Singleton
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@sinks = []
|
12
|
+
end
|
13
|
+
|
14
|
+
def add_sink(sink)
|
15
|
+
@sinks << sink
|
16
|
+
end
|
17
|
+
|
18
|
+
def drain(event)
|
19
|
+
@sinks.each { |s| s.receive(event) }
|
20
|
+
end
|
21
|
+
|
22
|
+
def clear_sinks
|
23
|
+
@sinks.clear
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module LogSinks
|
4
|
+
# Filter is a proc-like object that filters events to be accepted by [LogSinks::Sink]
|
5
|
+
class Filter
|
6
|
+
# Applies filter to the event
|
7
|
+
# @param event [LogSinks::Event] event to filter
|
8
|
+
# @return [Boolean] true if [LogSinks::Sink] should accept the event; false otherwise
|
9
|
+
def call(_event)
|
10
|
+
true
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../level'
|
4
|
+
require_relative '../filter'
|
5
|
+
|
6
|
+
module LogSinks
|
7
|
+
module Filters
|
8
|
+
# Filter by event level. Pass if event level is greater or equal to filter level.
|
9
|
+
class LevelFilter < Filter
|
10
|
+
# Initializes new instance of [LogSinks::Filters::LevelFilter]
|
11
|
+
# @param limit_level [LogSinks::Level|Symbol] filter level
|
12
|
+
def initialize(limit_level)
|
13
|
+
super()
|
14
|
+
@limit_level = LogSinks::Level[limit_level]
|
15
|
+
end
|
16
|
+
|
17
|
+
# Applies filter to the event
|
18
|
+
# @argument event [LogSinks::Event] event to test
|
19
|
+
# @return [Boolean] true if event pass the filter; false otherwise
|
20
|
+
def call(event)
|
21
|
+
event.level >= @limit_level
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../filter'
|
4
|
+
|
5
|
+
module LogSinks
|
6
|
+
module Filters
|
7
|
+
# Filter event by logger name. Pass if logger name starts with logger prefix.
|
8
|
+
class LoggerNamePrefixFilter < LogSinks::Filter
|
9
|
+
def initialize(prefix, include_root: true)
|
10
|
+
super()
|
11
|
+
@prefix = prefix
|
12
|
+
@include_root = include_root
|
13
|
+
end
|
14
|
+
|
15
|
+
def call(event)
|
16
|
+
name = event.logger_name
|
17
|
+
(@include_root && name == :root) || name.start_with?(@prefix)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module LogSinks
|
4
|
+
# Base class that serializes event to data format acceptable by sink (usually it's [String])
|
5
|
+
class Formatter
|
6
|
+
def initialize
|
7
|
+
@timestamp_format = '%Y-%m-%dT%H:%M:%S.%6N'
|
8
|
+
end
|
9
|
+
|
10
|
+
def call(event)
|
11
|
+
# rubocop:disable Layout/LineLength
|
12
|
+
"#{event.level.to_s[0]}, [#{event.timestamp.strftime(@timestamp_format)} ##{Process.pid}] #{event.level} -- #{event[:progname]}: #{event.message}\n"
|
13
|
+
# rubocop:enable Layout/LineLength
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'formatter'
|
4
|
+
require_relative 'sink'
|
5
|
+
|
6
|
+
module LogSinks
|
7
|
+
# Sink that writes log events to IO stream, e.g., STDOUT or file stream
|
8
|
+
class IOSink < Sink
|
9
|
+
def initialize(io, pump_class: LogSinks::Pumps::InThreadPump)
|
10
|
+
super(pump_class: pump_class)
|
11
|
+
@io = io
|
12
|
+
@formatter = ::LogSinks::Formatter.new
|
13
|
+
end
|
14
|
+
|
15
|
+
attr_writer :formatter
|
16
|
+
|
17
|
+
def drain(event)
|
18
|
+
txt = @formatter.call(event)
|
19
|
+
@io.write(txt)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module LogSinks
|
4
|
+
# Log event level.
|
5
|
+
class Level
|
6
|
+
class << self
|
7
|
+
def []=(name, level)
|
8
|
+
(@s ||= {})[name] = level
|
9
|
+
end
|
10
|
+
|
11
|
+
def [](name)
|
12
|
+
(@s ||= {})[name]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
attr_reader :name, :val
|
17
|
+
|
18
|
+
def initialize(name, val)
|
19
|
+
@val = val
|
20
|
+
@name = name
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_s
|
24
|
+
@name.to_s.upcase
|
25
|
+
end
|
26
|
+
|
27
|
+
def <(other)
|
28
|
+
@val < other.val
|
29
|
+
end
|
30
|
+
|
31
|
+
def <=(other)
|
32
|
+
@val <= other.val
|
33
|
+
end
|
34
|
+
|
35
|
+
def >(other)
|
36
|
+
@val > other.val
|
37
|
+
end
|
38
|
+
|
39
|
+
def >=(other)
|
40
|
+
@val >= other.val
|
41
|
+
end
|
42
|
+
|
43
|
+
def ==(other)
|
44
|
+
@val == other.val
|
45
|
+
end
|
46
|
+
|
47
|
+
# Define LogSinks levels
|
48
|
+
[[:debug, 1000], [:info, 2000], [:warn, 3000], [:error, 4000], [:fatal, 5000]].each do |(n, v)|
|
49
|
+
::LogSinks::Level[n] = ::LogSinks::Level.new(n, v).freeze
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module LogSinks
|
4
|
+
# Represents a log event.
|
5
|
+
class LogEvent
|
6
|
+
def initialize(timestamp, level, logger_name, msg, meta: nil)
|
7
|
+
@data = meta || {}
|
8
|
+
@data[:timestamp] = timestamp
|
9
|
+
@data[:level] = level
|
10
|
+
@data[:logger_name] = logger_name
|
11
|
+
@data[:message] = msg
|
12
|
+
end
|
13
|
+
|
14
|
+
def timestamp
|
15
|
+
self[:timestamp]
|
16
|
+
end
|
17
|
+
|
18
|
+
def level
|
19
|
+
self[:level]
|
20
|
+
end
|
21
|
+
|
22
|
+
def logger_name
|
23
|
+
self[:logger_name]
|
24
|
+
end
|
25
|
+
|
26
|
+
def message
|
27
|
+
self[:message]
|
28
|
+
end
|
29
|
+
|
30
|
+
def [](key)
|
31
|
+
@data[key]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'time'
|
4
|
+
|
5
|
+
require_relative 'event_drain'
|
6
|
+
require_relative 'level'
|
7
|
+
require_relative 'log_event'
|
8
|
+
require_relative 'mdc'
|
9
|
+
require_relative 'repository'
|
10
|
+
|
11
|
+
module LogSinks
|
12
|
+
# Logger is a object that captures log event and forward that to event sinks.
|
13
|
+
class Logger
|
14
|
+
class << self
|
15
|
+
def [](name)
|
16
|
+
::LogSinks::Repository.instance.get_or_add(name) do |logger_name|
|
17
|
+
Logger.new(logger_name)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
attr_reader :name
|
23
|
+
|
24
|
+
def initialize(name)
|
25
|
+
validate_name(name)
|
26
|
+
|
27
|
+
@name = name
|
28
|
+
@level = ::LogSinks::Level[:info]
|
29
|
+
end
|
30
|
+
|
31
|
+
def level?(level)
|
32
|
+
@level.val <= level.val
|
33
|
+
end
|
34
|
+
|
35
|
+
def level=(level)
|
36
|
+
@level = case level
|
37
|
+
when String, Symbol then ::LogSinks::Level[level.to_sym]
|
38
|
+
when ::LogSinks::Level then level
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
%i[debug info warn error fatal].each do |level|
|
43
|
+
code = <<-CODE
|
44
|
+
undef :#{level} if method_defined? :#{level}
|
45
|
+
LEVEL_#{level} = ::LogSinks::Level[:#{level}]
|
46
|
+
|
47
|
+
def #{level}(msg = nil, meta: nil, error: nil, &block)
|
48
|
+
log_event(LEVEL_#{level}, msg, meta: meta, error: error, &block)
|
49
|
+
end
|
50
|
+
CODE
|
51
|
+
class_eval(code)
|
52
|
+
end
|
53
|
+
|
54
|
+
def log_event(level, msg = nil, meta: nil, error: nil)
|
55
|
+
return false unless level?(level)
|
56
|
+
|
57
|
+
msg = yield if msg.nil? && block_given?
|
58
|
+
meta ||= {}
|
59
|
+
meta[:error] = error unless error.nil?
|
60
|
+
meta.merge!(MDC.items)
|
61
|
+
event = ::LogSinks::LogEvent.new(Time.now, level, @name, msg, meta: meta)
|
62
|
+
dispatch_event event
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def validate_name(name)
|
68
|
+
case name
|
69
|
+
when String
|
70
|
+
raise ArgumentError, 'logger name must not be empty' if name.empty?
|
71
|
+
else
|
72
|
+
raise ArgumentError, 'logger name must be a string'
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def dispatch_event(event)
|
77
|
+
EventDrain.instance.drain(event)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module LogSinks
|
4
|
+
# Mapped Diagnostic Context (MDC) is thread-aware Hash-like storage to add extra context details to logged events.
|
5
|
+
module MDC
|
6
|
+
SYNC_MUTEX = Mutex.new
|
7
|
+
|
8
|
+
class << self
|
9
|
+
def []=(key, value)
|
10
|
+
context[key] = value
|
11
|
+
end
|
12
|
+
|
13
|
+
def clear(key)
|
14
|
+
context.delete(key)
|
15
|
+
nil
|
16
|
+
end
|
17
|
+
|
18
|
+
def store(key, value)
|
19
|
+
MDC[key] = value
|
20
|
+
end
|
21
|
+
|
22
|
+
def items
|
23
|
+
context.dup.freeze
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def context
|
29
|
+
if Thread.current[:log_sinks_mdc].nil?
|
30
|
+
SYNC_MUTEX.synchronize do
|
31
|
+
Thread.current[:log_sinks_mdc] = {} if Thread.current[:log_sinks_mdc].nil?
|
32
|
+
end
|
33
|
+
end
|
34
|
+
Thread.current[:log_sinks_mdc]
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'concurrent'
|
4
|
+
require 'immutable/deque'
|
5
|
+
|
6
|
+
require_relative 'in_thread_pump'
|
7
|
+
|
8
|
+
module LogSinks
|
9
|
+
module Pumps
|
10
|
+
# A pump that drains events to the sink in a dedicated thread
|
11
|
+
class DedicatedThreadPump < InThreadPump
|
12
|
+
def initialize(sink)
|
13
|
+
super
|
14
|
+
|
15
|
+
@drain_buffer = Concurrent::AtomicReference.new
|
16
|
+
@drain_buffer.set(Immutable::Deque.empty)
|
17
|
+
@has_events_flag = Concurrent::Event.new
|
18
|
+
@drain_thread = Thread.new { drain_thread_func }
|
19
|
+
end
|
20
|
+
|
21
|
+
def receive(event)
|
22
|
+
@drain_buffer.update { |buffer| buffer.push(event) }
|
23
|
+
@has_events_flag.set
|
24
|
+
end
|
25
|
+
|
26
|
+
protected
|
27
|
+
|
28
|
+
def drain_thread_func
|
29
|
+
@has_events_flag.reset
|
30
|
+
|
31
|
+
loop do
|
32
|
+
@has_events_flag.wait(0.1)
|
33
|
+
@has_events_flag.reset
|
34
|
+
|
35
|
+
drain_backlog(snapshot_backlog)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def snapshot_backlog
|
40
|
+
backlog = nil
|
41
|
+
loop do
|
42
|
+
backlog = @drain_buffer.get
|
43
|
+
break if @drain_buffer.compare_and_set(backlog, Immutable::Deque.empty)
|
44
|
+
end
|
45
|
+
backlog
|
46
|
+
end
|
47
|
+
|
48
|
+
def drain_backlog(backlog)
|
49
|
+
until backlog.empty?
|
50
|
+
e = backlog.first
|
51
|
+
backlog = backlog.shift
|
52
|
+
@sink.drain(e)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_litral: true
|
2
|
+
|
3
|
+
module LogSinks
|
4
|
+
module Pumps
|
5
|
+
# A pump that drains events to the sink in the current thread
|
6
|
+
class InThreadPump
|
7
|
+
def initialize(sink)
|
8
|
+
@sink = sink
|
9
|
+
end
|
10
|
+
|
11
|
+
def receive(event)
|
12
|
+
@sink.drain event
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'singleton'
|
4
|
+
|
5
|
+
module LogSinks
|
6
|
+
# Repository stores references to loggers by logger name.
|
7
|
+
class Repository
|
8
|
+
include Singleton
|
9
|
+
|
10
|
+
# Initializes new instance of [LogSinks] class.
|
11
|
+
def initialize
|
12
|
+
@store = {}
|
13
|
+
@mutex = Mutex.new
|
14
|
+
end
|
15
|
+
|
16
|
+
# Tests if logger with given name exists
|
17
|
+
# @return [Boolean] true if logger exists; false otherwise.
|
18
|
+
def key?(name)
|
19
|
+
@store.key? canonize_name(name)
|
20
|
+
end
|
21
|
+
|
22
|
+
def get_or_add(name)
|
23
|
+
name = canonize_name(name)
|
24
|
+
logger = @store[name]
|
25
|
+
|
26
|
+
if logger.nil?
|
27
|
+
logger = @mutex.synchronize do
|
28
|
+
@store[name] = yield(name) unless @store.key?(name)
|
29
|
+
@store[name]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
logger
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def canonize_name(name)
|
38
|
+
case name
|
39
|
+
when :root, 'root' then 'root'
|
40
|
+
when String then name
|
41
|
+
when Symbol then name.to_s
|
42
|
+
when Module then module_name(name)
|
43
|
+
when Object then module_name(name.class)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def module_name(mod)
|
48
|
+
mod.name
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'logger'
|
4
|
+
|
5
|
+
require_relative 'level'
|
6
|
+
require_relative 'logger'
|
7
|
+
|
8
|
+
module LogSinks
|
9
|
+
# Logger is a object that captures log event and forward that to event sinks.
|
10
|
+
class Logger
|
11
|
+
def as_ruby_logger(progname: nil)
|
12
|
+
RubyLoggerAdapter.new(self, progname)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# Adapter for [LogSinks::Logger] to match [::Logger] interface.
|
17
|
+
class RubyLoggerAdapter
|
18
|
+
[
|
19
|
+
[::Logger::DEBUG, ::LogSinks::Level[:debug]],
|
20
|
+
[::Logger::INFO, ::LogSinks::Level[:info]],
|
21
|
+
[::Logger::WARN, ::LogSinks::Level[:warn]],
|
22
|
+
[::Logger::ERROR, ::LogSinks::Level[:error]],
|
23
|
+
[::Logger::FATAL, ::LogSinks::Level[:fatal]],
|
24
|
+
[::Logger::UNKNOWN, ::LogSinks::Level[:fatal]]
|
25
|
+
].each do |(n, v)|
|
26
|
+
::LogSinks::Level[n] = v
|
27
|
+
end
|
28
|
+
|
29
|
+
%i[DEBUG INFO WARN ERROR FATAL UNKNOWN].each do |l|
|
30
|
+
code = <<-CODE
|
31
|
+
LEVEL_#{l} = ::LogSinks::Level[::Logger::Severity::#{l}]
|
32
|
+
|
33
|
+
def #{l.downcase}(progname = nil, &block)
|
34
|
+
log_event(LEVEL_#{l}, nil, progname, &block)
|
35
|
+
end
|
36
|
+
CODE
|
37
|
+
class_eval(code)
|
38
|
+
end
|
39
|
+
|
40
|
+
def initialize(logger, progname)
|
41
|
+
@progname = progname
|
42
|
+
@logger = logger
|
43
|
+
@meta = { progname: @progname }
|
44
|
+
end
|
45
|
+
|
46
|
+
def add(severity, message = nil, progname = nil, &block)
|
47
|
+
severity = ::LogSinks::Level[severity] unless severity.is_a? ::LogSinks::Level
|
48
|
+
log_event(severity, message, progname, &block)
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def log_event(severity, message, progname, &block)
|
54
|
+
return true unless @logger.level?(severity)
|
55
|
+
|
56
|
+
progname = @progname if progname.nil?
|
57
|
+
|
58
|
+
if message.nil? && block.nil?
|
59
|
+
message = progname
|
60
|
+
progname = @progname
|
61
|
+
end
|
62
|
+
|
63
|
+
meta = progname == @progname ? @meta : { progname: progname }
|
64
|
+
@logger.log_event(severity, message, meta: meta, &block)
|
65
|
+
true
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'filter'
|
4
|
+
require_relative 'pumps/in_thread_pump'
|
5
|
+
|
6
|
+
module LogSinks
|
7
|
+
# Sink receives log event and writes to a permanent storage.
|
8
|
+
class Sink
|
9
|
+
def initialize(pump_class: LogSinks::Pumps::InThreadPump)
|
10
|
+
@filters = []
|
11
|
+
@pump = pump_class.new(self)
|
12
|
+
end
|
13
|
+
|
14
|
+
def flush
|
15
|
+
@has_events_flag.set if @thread_model == :dedicated_thread
|
16
|
+
end
|
17
|
+
|
18
|
+
def receive(event)
|
19
|
+
return unless accept_event?(event)
|
20
|
+
|
21
|
+
@pump.receive event
|
22
|
+
end
|
23
|
+
|
24
|
+
def add_filter(filter)
|
25
|
+
@filters << filter
|
26
|
+
end
|
27
|
+
|
28
|
+
# Receives a message and write to a permanent storage
|
29
|
+
def drain(_event)
|
30
|
+
nil
|
31
|
+
end
|
32
|
+
|
33
|
+
protected
|
34
|
+
|
35
|
+
def accept_event?(event)
|
36
|
+
proceed = true
|
37
|
+
@filters.each do |f|
|
38
|
+
proceed &&= f.call(event)
|
39
|
+
break unless proceed
|
40
|
+
end
|
41
|
+
proceed
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/lib/log_sinks.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'log_sinks/io_sink'
|
4
|
+
require_relative 'log_sinks/logger'
|
5
|
+
require_relative 'log_sinks/pumps/in_thread_pump'
|
6
|
+
require_relative 'log_sinks/pumps/dedicated_thread_pump'
|
7
|
+
require_relative 'log_sinks/version'
|
8
|
+
require_relative 'log_sinks/filters/level_filter'
|
9
|
+
require_relative 'log_sinks/filters/logger_name_prefix_filter'
|
metadata
ADDED
@@ -0,0 +1,189 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: log_sinks
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrey Maraev
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-10-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: concurrent-ruby
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.1.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.1.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: concurrent-ruby-ext
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.1.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.1.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: immutable-ruby
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.1.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.1.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: code-scanning-rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.6.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.6.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: debug
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 1.6.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 1.6.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 13.0.0
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 13.0.0
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 3.11.0
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 3.11.0
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rubocop
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 1.36.0
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 1.36.0
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: simplecov
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: 0.21.2
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: 0.21.2
|
139
|
+
description:
|
140
|
+
email:
|
141
|
+
- the_vk@thevk.net
|
142
|
+
executables: []
|
143
|
+
extensions: []
|
144
|
+
extra_rdoc_files: []
|
145
|
+
files:
|
146
|
+
- LICENSE
|
147
|
+
- README.md
|
148
|
+
- lib/log_sinks.rb
|
149
|
+
- lib/log_sinks/event_drain.rb
|
150
|
+
- lib/log_sinks/filter.rb
|
151
|
+
- lib/log_sinks/filters/level_filter.rb
|
152
|
+
- lib/log_sinks/filters/logger_name_prefix_filter.rb
|
153
|
+
- lib/log_sinks/formatter.rb
|
154
|
+
- lib/log_sinks/io_sink.rb
|
155
|
+
- lib/log_sinks/level.rb
|
156
|
+
- lib/log_sinks/log_event.rb
|
157
|
+
- lib/log_sinks/logger.rb
|
158
|
+
- lib/log_sinks/mdc.rb
|
159
|
+
- lib/log_sinks/pumps/dedicated_thread_pump.rb
|
160
|
+
- lib/log_sinks/pumps/in_thread_pump.rb
|
161
|
+
- lib/log_sinks/repository.rb
|
162
|
+
- lib/log_sinks/ruby_logger_adapter.rb
|
163
|
+
- lib/log_sinks/sink.rb
|
164
|
+
- lib/log_sinks/version.rb
|
165
|
+
homepage: https://github.com/the-vk/log_sinks
|
166
|
+
licenses:
|
167
|
+
- MIT
|
168
|
+
metadata:
|
169
|
+
rubygems_mfa_required: 'true'
|
170
|
+
post_install_message:
|
171
|
+
rdoc_options: []
|
172
|
+
require_paths:
|
173
|
+
- lib
|
174
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
175
|
+
requirements:
|
176
|
+
- - "~>"
|
177
|
+
- !ruby/object:Gem::Version
|
178
|
+
version: '3.0'
|
179
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
180
|
+
requirements:
|
181
|
+
- - ">="
|
182
|
+
- !ruby/object:Gem::Version
|
183
|
+
version: '0'
|
184
|
+
requirements: []
|
185
|
+
rubygems_version: 3.3.3
|
186
|
+
signing_key:
|
187
|
+
specification_version: 4
|
188
|
+
summary: Powerful and extensible logging library
|
189
|
+
test_files: []
|