active_logger 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/.rubocop_todo.yml +1 -1
- data/Gemfile.lock +1 -1
- data/README.md +53 -0
- data/lib/active_logger.rb +7 -0
- data/lib/active_logger/formatters/base.rb +59 -0
- data/lib/active_logger/formatters/default.rb +12 -0
- data/lib/active_logger/formatters/json.rb +24 -0
- data/lib/active_logger/helpers/appender.rb +0 -4
- data/lib/active_logger/helpers/formatter.rb +37 -0
- data/lib/active_logger/helpers/level.rb +36 -0
- data/lib/active_logger/logger.rb +7 -0
- data/lib/active_logger/tagged_logging.rb +1 -51
- data/lib/active_logger/version.rb +1 -1
- metadata +6 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 329985023815903e40757931bee2d0fef32a2f0e9e703570306b58acc403db34
|
4
|
+
data.tar.gz: 30d2bc94716af0bbb5781fa8d43404154bd5e11cda6fc94b7d3564e0d0d17a0a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bc955d714639abbec8d7452915e4b6b166a7b5768b37cc29f14a61404aac48fd06d6d066e9c6f601824c8092dab56daefe6bc0ea834e760a2dca75dd44f180e7
|
7
|
+
data.tar.gz: 36fac211a1ffc773b51614d53ff72dcf0d3c83b6adebc511b8694a77bfa9d3e7dd0c00c17e710d111881594da046a76f0bf06dd8ea1bbfaa6232a5a879689ad9
|
data/.rubocop.yml
CHANGED
data/.rubocop_todo.yml
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# This configuration was generated by
|
2
2
|
# `rubocop --auto-gen-config`
|
3
|
-
# on 2020-04-22
|
3
|
+
# on 2020-04-22 17:28:02 +0300 using RuboCop version 0.82.0.
|
4
4
|
# The point is for the user to remove these configuration records
|
5
5
|
# one by one as the offenses are removed from the code base.
|
6
6
|
# Note that changes in the inspected code, or installation of new
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -61,6 +61,59 @@ end
|
|
61
61
|
logger.info 'test'
|
62
62
|
```
|
63
63
|
|
64
|
+
### Example: Tagging
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
logger = ActiveLogger.new STDOUT
|
68
|
+
logger.tagged('API').info 'test'
|
69
|
+
|
70
|
+
# or
|
71
|
+
|
72
|
+
logger.tagged('API') do
|
73
|
+
logger.info 'test'
|
74
|
+
end
|
75
|
+
```
|
76
|
+
|
77
|
+
### Example: Setting the Log Level
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
ActiveLogger.new STDOUT, level: :info
|
81
|
+
|
82
|
+
# or
|
83
|
+
|
84
|
+
ActiveLogger.new do |al|
|
85
|
+
al.level = :debug
|
86
|
+
|
87
|
+
al.appender :stdout
|
88
|
+
end
|
89
|
+
```
|
90
|
+
|
91
|
+
### Example: Formatters
|
92
|
+
|
93
|
+
There are 2 standard formatters: `:default` and `:json`.
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
ActiveLogger.new STDOUT, formatter: :json
|
97
|
+
|
98
|
+
# or
|
99
|
+
|
100
|
+
ActiveLogger.new do |al|
|
101
|
+
al.formatter = :json
|
102
|
+
|
103
|
+
al.appender :stdout
|
104
|
+
end
|
105
|
+
|
106
|
+
# or custom formatter
|
107
|
+
|
108
|
+
class Formatter < ActiveLogger::Formatters::Base
|
109
|
+
def call(severity, timestamp, progname, msg)
|
110
|
+
"[#{severity}] [#{timestamp}] #{msg}"
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
ActiveLogger.new STDOUT, formatter: Formatter
|
115
|
+
```
|
116
|
+
|
64
117
|
## Development
|
65
118
|
|
66
119
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/active_logger.rb
CHANGED
@@ -11,8 +11,15 @@ module ActiveLogger # :nodoc:
|
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
|
+
# Formatters
|
15
|
+
require File.dirname(__FILE__) + '/active_logger/formatters/base'
|
16
|
+
require File.dirname(__FILE__) + '/active_logger/formatters/default'
|
17
|
+
require File.dirname(__FILE__) + '/active_logger/formatters/json'
|
18
|
+
|
14
19
|
# Helpers
|
15
20
|
require File.dirname(__FILE__) + '/active_logger/helpers/base'
|
21
|
+
require File.dirname(__FILE__) + '/active_logger/helpers/level'
|
22
|
+
require File.dirname(__FILE__) + '/active_logger/helpers/formatter'
|
16
23
|
require File.dirname(__FILE__) + '/active_logger/helpers/appender'
|
17
24
|
|
18
25
|
require File.dirname(__FILE__) + '/active_logger/tagged_logging'
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'English'
|
4
|
+
|
5
|
+
module ActiveLogger
|
6
|
+
module Formatters
|
7
|
+
class Base < ActiveSupport::Logger::Formatter # :nodoc:
|
8
|
+
def call(severity, timestamp, progname, msg)
|
9
|
+
super(severity, timestamp, progname, msg)
|
10
|
+
end
|
11
|
+
|
12
|
+
def tagged(*tags)
|
13
|
+
new_tags = push_tags(*tags)
|
14
|
+
yield self
|
15
|
+
ensure
|
16
|
+
pop_tags(new_tags.size)
|
17
|
+
end
|
18
|
+
|
19
|
+
def push_tags(*tags)
|
20
|
+
@tags_text = nil
|
21
|
+
tags.flatten!
|
22
|
+
tags.reject!(&:blank?)
|
23
|
+
current_tags.concat tags
|
24
|
+
tags
|
25
|
+
end
|
26
|
+
|
27
|
+
def pop_tags(size = 1)
|
28
|
+
@tags_text = nil
|
29
|
+
current_tags.pop size
|
30
|
+
end
|
31
|
+
|
32
|
+
def clear_tags!
|
33
|
+
@tags_text = nil
|
34
|
+
current_tags.clear
|
35
|
+
end
|
36
|
+
|
37
|
+
def current_tags
|
38
|
+
# We use our object ID here to avoid conflicting with other instances
|
39
|
+
thread_key = @thread_key ||= "activelogger_tagged_logging_tags:#{object_id}"
|
40
|
+
Thread.current[thread_key] ||= []
|
41
|
+
end
|
42
|
+
|
43
|
+
def tags_text
|
44
|
+
@tags_text ||= begin
|
45
|
+
tags = current_tags
|
46
|
+
if tags.one?
|
47
|
+
"[#{tags[0]}] "
|
48
|
+
elsif tags.any?
|
49
|
+
tags.collect { |tag| "[#{tag}] " }.join
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def pid
|
55
|
+
$PID
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
module ActiveLogger
|
5
|
+
module Formatters
|
6
|
+
class Default < Base # :nodoc:
|
7
|
+
def call(severity, timestamp, progname, msg)
|
8
|
+
super(severity, timestamp, progname, "#{tags_text}#{msg}")
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module ActiveLogger
|
6
|
+
module Formatters
|
7
|
+
class Json < Base # :nodoc:
|
8
|
+
def call(severity, timestamp, progname, msg)
|
9
|
+
{
|
10
|
+
progname: progname,
|
11
|
+
severity: severity,
|
12
|
+
timestamp: timestamp.utc.strftime(datetime_format),
|
13
|
+
tags: current_tags,
|
14
|
+
pid: pid,
|
15
|
+
message: msg
|
16
|
+
}.to_json + "\n"
|
17
|
+
end
|
18
|
+
|
19
|
+
def datetime_format
|
20
|
+
'%FT%T.%6NZ'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveLogger #:nodoc:
|
4
|
+
module Helpers # :nodoc:
|
5
|
+
module Formatter #:nodoc:
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
class FormatterNotFound < StandardError; end
|
9
|
+
|
10
|
+
class_methods do
|
11
|
+
def formatter=(formatter)
|
12
|
+
@__formatter__ =
|
13
|
+
case formatter
|
14
|
+
when :default then ActiveLogger::Formatters::Default.new
|
15
|
+
when :json then ActiveLogger::Formatters::Json.new
|
16
|
+
else
|
17
|
+
raise FormatterNotFound unless formatter.class.ancestors.include?(ActiveLogger::Formatters::Base)
|
18
|
+
|
19
|
+
formatter
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def formatter
|
24
|
+
@__formatter__
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def reset!
|
30
|
+
@__formatter__ = ActiveLogger::Formatters::Default.new
|
31
|
+
|
32
|
+
super if defined?(super)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveLogger #:nodoc:
|
4
|
+
module Helpers # :nodoc:
|
5
|
+
module Level #:nodoc:
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
SEVERITIES = %w[DEBUG INFO WARN ERROR FATAL UNKNOWN].freeze
|
9
|
+
|
10
|
+
class LevelNotFound < StandardError; end
|
11
|
+
|
12
|
+
class_methods do
|
13
|
+
def level=(severity)
|
14
|
+
@__level__ =
|
15
|
+
case severity
|
16
|
+
when Integer then severity
|
17
|
+
when Symbol, String then SEVERITIES.index(severity.to_s.upcase)
|
18
|
+
else raise LevelNotFound
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def level
|
23
|
+
@__level__
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def reset!
|
29
|
+
@__level__ = ActiveSupport::Logger::DEBUG
|
30
|
+
|
31
|
+
super
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/active_logger/logger.rb
CHANGED
@@ -5,6 +5,8 @@ module ActiveLogger
|
|
5
5
|
module_function
|
6
6
|
|
7
7
|
include ActiveLogger::Helpers::Base
|
8
|
+
include ActiveLogger::Helpers::Level
|
9
|
+
include ActiveLogger::Helpers::Formatter
|
8
10
|
include ActiveLogger::Helpers::Appender
|
9
11
|
|
10
12
|
class AppenderNotFound < StandardError; end
|
@@ -16,6 +18,9 @@ module ActiveLogger
|
|
16
18
|
|
17
19
|
reset!
|
18
20
|
|
21
|
+
self.formatter = options[:formatter] unless options[:formatter].nil?
|
22
|
+
self.level = options[:level] unless options[:level].nil?
|
23
|
+
|
19
24
|
if block_given?
|
20
25
|
block.arity.positive? ? block.call(self) : instance_eval(&block)
|
21
26
|
else
|
@@ -47,6 +52,8 @@ module ActiveLogger
|
|
47
52
|
end
|
48
53
|
|
49
54
|
logger = ActiveSupport::Logger.new(*parameters)
|
55
|
+
logger.level = level
|
56
|
+
logger.formatter = formatter
|
50
57
|
logger
|
51
58
|
end
|
52
59
|
end
|
@@ -5,55 +5,6 @@ require 'active_support/core_ext/object/blank'
|
|
5
5
|
|
6
6
|
module ActiveLogger
|
7
7
|
module TaggedLogging # :nodoc:
|
8
|
-
module Formatter # :nodoc:
|
9
|
-
# This method is invoked when a log event occurs.
|
10
|
-
def call(severity, timestamp, progname, msg)
|
11
|
-
super(severity, timestamp, progname, "#{tags_text}#{msg}")
|
12
|
-
end
|
13
|
-
|
14
|
-
def tagged(*tags)
|
15
|
-
new_tags = push_tags(*tags)
|
16
|
-
yield self
|
17
|
-
ensure
|
18
|
-
pop_tags(new_tags.size)
|
19
|
-
end
|
20
|
-
|
21
|
-
def push_tags(*tags)
|
22
|
-
@tags_text = nil
|
23
|
-
tags.flatten!
|
24
|
-
tags.reject!(&:blank?)
|
25
|
-
current_tags.concat tags
|
26
|
-
tags
|
27
|
-
end
|
28
|
-
|
29
|
-
def pop_tags(size = 1)
|
30
|
-
@tags_text = nil
|
31
|
-
current_tags.pop size
|
32
|
-
end
|
33
|
-
|
34
|
-
def clear_tags!
|
35
|
-
@tags_text = nil
|
36
|
-
current_tags.clear
|
37
|
-
end
|
38
|
-
|
39
|
-
def current_tags
|
40
|
-
# We use our object ID here to avoid conflicting with other instances
|
41
|
-
thread_key = @thread_key ||= "activesupport_tagged_logging_tags:#{object_id}"
|
42
|
-
Thread.current[thread_key] ||= []
|
43
|
-
end
|
44
|
-
|
45
|
-
def tags_text
|
46
|
-
@tags_text ||= begin
|
47
|
-
tags = current_tags
|
48
|
-
if tags.one?
|
49
|
-
"[#{tags[0]}] "
|
50
|
-
elsif tags.any?
|
51
|
-
tags.collect { |tag| "[#{tag}] " }.join
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
8
|
module LocalTagStorage # :nodoc:
|
58
9
|
attr_accessor :current_tags
|
59
10
|
|
@@ -70,10 +21,9 @@ module ActiveLogger
|
|
70
21
|
logger.formatter.dup
|
71
22
|
else
|
72
23
|
# Ensure we set a default formatter so we aren't extending nil!
|
73
|
-
|
24
|
+
ActiveLogger::Formatters::Default.new
|
74
25
|
end
|
75
26
|
|
76
|
-
logger.formatter.extend Formatter
|
77
27
|
logger.extend(self)
|
78
28
|
end
|
79
29
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_logger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yury Snegirev
|
@@ -46,8 +46,13 @@ files:
|
|
46
46
|
- bin/console
|
47
47
|
- bin/setup
|
48
48
|
- lib/active_logger.rb
|
49
|
+
- lib/active_logger/formatters/base.rb
|
50
|
+
- lib/active_logger/formatters/default.rb
|
51
|
+
- lib/active_logger/formatters/json.rb
|
49
52
|
- lib/active_logger/helpers/appender.rb
|
50
53
|
- lib/active_logger/helpers/base.rb
|
54
|
+
- lib/active_logger/helpers/formatter.rb
|
55
|
+
- lib/active_logger/helpers/level.rb
|
51
56
|
- lib/active_logger/logger.rb
|
52
57
|
- lib/active_logger/tagged_logging.rb
|
53
58
|
- lib/active_logger/version.rb
|