lumberjack 1.2.8 → 1.2.10
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 +4 -4
- data/CHANGELOG.md +152 -58
- data/README.md +35 -6
- data/VERSION +1 -1
- data/lib/lumberjack/context.rb +13 -0
- data/lib/lumberjack/device/date_rolling_log_file.rb +10 -1
- data/lib/lumberjack/device/log_file.rb +8 -1
- data/lib/lumberjack/device/multi.rb +2 -1
- data/lib/lumberjack/device/null.rb +1 -1
- data/lib/lumberjack/device/rolling_log_file.rb +22 -22
- data/lib/lumberjack/device/size_rolling_log_file.rb +1 -1
- data/lib/lumberjack/device/writer.rb +21 -1
- data/lib/lumberjack/device.rb +16 -1
- data/lib/lumberjack/formatter/date_time_formatter.rb +2 -1
- data/lib/lumberjack/formatter/exception_formatter.rb +4 -2
- data/lib/lumberjack/formatter/id_formatter.rb +2 -1
- data/lib/lumberjack/formatter/inspect_formatter.rb +1 -1
- data/lib/lumberjack/formatter/object_formatter.rb +1 -1
- data/lib/lumberjack/formatter/pretty_print_formatter.rb +3 -1
- data/lib/lumberjack/formatter/string_formatter.rb +1 -1
- data/lib/lumberjack/formatter/strip_formatter.rb +1 -1
- data/lib/lumberjack/formatter/structured_formatter.rb +3 -1
- data/lib/lumberjack/formatter/truncate_formatter.rb +27 -0
- data/lib/lumberjack/formatter.rb +55 -9
- data/lib/lumberjack/log_entry.rb +10 -2
- data/lib/lumberjack/logger.rb +141 -18
- data/lib/lumberjack/rack/context.rb +1 -1
- data/lib/lumberjack/rack/request_id.rb +1 -1
- data/lib/lumberjack/rack/unit_of_work.rb +1 -1
- data/lib/lumberjack/rack.rb +1 -1
- data/lib/lumberjack/severity.rb +21 -1
- data/lib/lumberjack/tag_formatter.rb +19 -0
- data/lib/lumberjack/tagged_logger_support.rb +1 -1
- data/lib/lumberjack/tags.rb +6 -0
- data/lib/lumberjack/template.rb +15 -3
- data/lib/lumberjack.rb +20 -2
- metadata +8 -7
data/lib/lumberjack/device.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Lumberjack
|
4
4
|
# This is an abstract class for logging devices. Subclasses must implement the +write+ method and
|
@@ -13,29 +13,44 @@ module Lumberjack
|
|
13
13
|
require_relative "device/null"
|
14
14
|
|
15
15
|
# Subclasses must implement this method to write a LogEntry.
|
16
|
+
#
|
17
|
+
# @param [Lumberjack::LogEntry] entry The entry to write.
|
18
|
+
# @return [void]
|
16
19
|
def write(entry)
|
17
20
|
raise NotImplementedError
|
18
21
|
end
|
19
22
|
|
20
23
|
# Subclasses may implement this method to close the device.
|
24
|
+
#
|
25
|
+
# @return [void]
|
21
26
|
def close
|
22
27
|
flush
|
23
28
|
end
|
24
29
|
|
25
30
|
# Subclasses may implement this method to reopen the device.
|
31
|
+
#
|
32
|
+
# @param [Object] logdev The log device to use.
|
33
|
+
# @return [void]
|
26
34
|
def reopen(logdev = nil)
|
27
35
|
flush
|
28
36
|
end
|
29
37
|
|
30
38
|
# Subclasses may implement this method to flush any buffers used by the device.
|
39
|
+
#
|
40
|
+
# @return [void]
|
31
41
|
def flush
|
32
42
|
end
|
33
43
|
|
34
44
|
# Subclasses may implement this method to get the format for log timestamps.
|
45
|
+
#
|
46
|
+
# @return [String] The format for log timestamps.
|
35
47
|
def datetime_format
|
36
48
|
end
|
37
49
|
|
38
50
|
# Subclasses may implement this method to set a format for log timestamps.
|
51
|
+
#
|
52
|
+
# @param [String] format The format for log timestamps.
|
53
|
+
# @return [void]
|
39
54
|
def datetime_format=(format)
|
40
55
|
end
|
41
56
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Lumberjack
|
4
4
|
class Formatter
|
@@ -7,6 +7,7 @@ module Lumberjack
|
|
7
7
|
class DateTimeFormatter
|
8
8
|
attr_reader :format
|
9
9
|
|
10
|
+
# @param [String] format The format to use when formatting the date/time object.
|
10
11
|
def initialize(format = nil)
|
11
12
|
@format = format.dup.to_s.freeze unless format.nil?
|
12
13
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Lumberjack
|
4
4
|
class Formatter
|
@@ -9,12 +9,14 @@ module Lumberjack
|
|
9
9
|
class ExceptionFormatter
|
10
10
|
attr_accessor :backtrace_cleaner
|
11
11
|
|
12
|
+
# @param [#call] backtrace_cleaner An object that responds to `call` and takes
|
13
|
+
# an array of strings (the backtrace) and returns an array of strings (the
|
12
14
|
def initialize(backtrace_cleaner = nil)
|
13
15
|
self.backtrace_cleaner = backtrace_cleaner
|
14
16
|
end
|
15
17
|
|
16
18
|
def call(exception)
|
17
|
-
message = "#{exception.class.name}: #{exception.message}"
|
19
|
+
message = +"#{exception.class.name}: #{exception.message}"
|
18
20
|
trace = exception.backtrace
|
19
21
|
if trace
|
20
22
|
trace = clean_backtrace(trace)
|
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Lumberjack
|
4
4
|
class Formatter
|
@@ -6,6 +6,7 @@ module Lumberjack
|
|
6
6
|
# as a default formatter for objects pulled from a data store. By default it will use :id as the
|
7
7
|
# id attribute.
|
8
8
|
class IdFormatter
|
9
|
+
# @param [Symbol, String] id_attribute The attribute to use as the id.
|
9
10
|
def initialize(id_attribute = :id)
|
10
11
|
@id_attribute = id_attribute
|
11
12
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "pp"
|
4
4
|
require "stringio"
|
@@ -11,6 +11,8 @@ module Lumberjack
|
|
11
11
|
|
12
12
|
# Create a new formatter. The maximum width of the message can be specified with the width
|
13
13
|
# parameter (defaults to 79 characters).
|
14
|
+
#
|
15
|
+
# @param [Integer] width The maximum width of the message.
|
14
16
|
def initialize(width = 79)
|
15
17
|
@width = width
|
16
18
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "set"
|
4
4
|
|
@@ -9,6 +9,8 @@ module Lumberjack
|
|
9
9
|
class RecusiveReferenceError < StandardError
|
10
10
|
end
|
11
11
|
|
12
|
+
# @param [Formatter] formatter The formatter to call on each element
|
13
|
+
# in the structure.
|
12
14
|
def initialize(formatter = nil)
|
13
15
|
@formatter = formatter
|
14
16
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Lumberjack
|
4
|
+
class Formatter
|
5
|
+
# Truncate a string object to a specific length. This is useful
|
6
|
+
# for formatting messages when there is a limit on the number of
|
7
|
+
# characters that can be logged per message. This formatter should
|
8
|
+
# only be used when necessary since it is a lossy formatter.
|
9
|
+
#
|
10
|
+
# When a string is truncated, it will have a unicode ellipsis
|
11
|
+
# character (U+2026) appended to the end of the string.
|
12
|
+
class TruncateFormatter
|
13
|
+
# @param [Integer] length The maximum length of the string (defaults to 32K)
|
14
|
+
def initialize(length = 32768)
|
15
|
+
@length = length
|
16
|
+
end
|
17
|
+
|
18
|
+
def call(obj)
|
19
|
+
if obj.is_a?(String) && obj.length > @length
|
20
|
+
"#{obj[0, @length - 1]}…"
|
21
|
+
else
|
22
|
+
obj
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/lumberjack/formatter.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Lumberjack
|
4
4
|
# This class controls the conversion of log entry messages into a loggable format. This allows you
|
@@ -21,11 +21,14 @@ module Lumberjack
|
|
21
21
|
require_relative "formatter/string_formatter"
|
22
22
|
require_relative "formatter/strip_formatter"
|
23
23
|
require_relative "formatter/structured_formatter"
|
24
|
+
require_relative "formatter/truncate_formatter"
|
24
25
|
|
25
26
|
class << self
|
26
27
|
# Returns a new empty formatter with no mapping. For historical reasons, a formatter
|
27
28
|
# is initialized with mappings to help output objects as strings. This will return one
|
28
29
|
# without the default mappings.
|
30
|
+
#
|
31
|
+
# @return [Lumberjack::Formatter] a new empty formatter
|
29
32
|
def empty
|
30
33
|
new.clear
|
31
34
|
end
|
@@ -45,7 +48,17 @@ module Lumberjack
|
|
45
48
|
# that responds to the +call+ method or as a symbol representing one of the predefined
|
46
49
|
# formatters, or as a block to the method call.
|
47
50
|
#
|
48
|
-
# The predefined formatters are:
|
51
|
+
# The predefined formatters are:
|
52
|
+
# - :date_time
|
53
|
+
# - :exception
|
54
|
+
# - :id
|
55
|
+
# - :inspect
|
56
|
+
# - :object
|
57
|
+
# - :pretty_print
|
58
|
+
# - :string
|
59
|
+
# - :strip
|
60
|
+
# - :structured
|
61
|
+
# - :truncate
|
49
62
|
#
|
50
63
|
# You can add multiple classes at once by passing an array of classes.
|
51
64
|
#
|
@@ -53,7 +66,18 @@ module Lumberjack
|
|
53
66
|
# help avoid loading dependency issues. This applies only to classes; modules cannot be
|
54
67
|
# passed in as strings.
|
55
68
|
#
|
56
|
-
#
|
69
|
+
# @param [Class, Module, String, Array<Class, Module, String>] klass The class or module to add a formatter for.
|
70
|
+
# @param [Symbol, Class, String, #call] formatter The formatter to use for the class.
|
71
|
+
# If a symbol is passed in, it will be used to load one of the predefined formatters.
|
72
|
+
# If a class is passed in, it will be initialized with the args passed in.
|
73
|
+
# Otherwise, the object will be used as the formatter and must respond to call method.
|
74
|
+
# @param [Array] args Arguments to pass to the formatter when it is initialized.
|
75
|
+
# @yield [obj] A block that will be used as the formatter for the class.
|
76
|
+
# @yieldparam [Object] obj The object to format.
|
77
|
+
# @yieldreturn [String] The formatted string.
|
78
|
+
# @return [self] Returns itself so that add statements can be chained together.
|
79
|
+
#
|
80
|
+
# @example
|
57
81
|
#
|
58
82
|
# # Use a predefined formatter
|
59
83
|
# formatter.add(MyClass, :pretty_print)
|
@@ -66,18 +90,27 @@ module Lumberjack
|
|
66
90
|
#
|
67
91
|
# # Add statements can be chained together
|
68
92
|
# formatter.add(MyClass, :pretty_print).add(YourClass){|obj| obj.humanize}
|
69
|
-
def add(klass, formatter = nil, &block)
|
93
|
+
def add(klass, formatter = nil, *args, &block)
|
70
94
|
formatter ||= block
|
71
95
|
if formatter.nil?
|
72
96
|
remove(klass)
|
73
97
|
else
|
98
|
+
formatter_class_name = nil
|
74
99
|
if formatter.is_a?(Symbol)
|
75
100
|
formatter_class_name = "#{formatter.to_s.gsub(/(^|_)([a-z])/) { |m| $~[2].upcase }}Formatter"
|
76
|
-
|
101
|
+
elsif formatter.is_a?(String)
|
102
|
+
formatter_class_name = formatter
|
103
|
+
end
|
104
|
+
if formatter_class_name
|
105
|
+
formatter = Formatter.const_get(formatter_class_name)
|
106
|
+
end
|
107
|
+
|
108
|
+
if formatter.is_a?(Class)
|
109
|
+
formatter = formatter.new(*args)
|
77
110
|
end
|
78
111
|
|
79
112
|
Array(klass).each do |k|
|
80
|
-
if k.
|
113
|
+
if k.instance_of?(Module)
|
81
114
|
@module_formatters[k] = formatter
|
82
115
|
else
|
83
116
|
k = k.name if k.is_a?(Class)
|
@@ -95,9 +128,12 @@ module Lumberjack
|
|
95
128
|
# You can also pass class names as strings instead of the classes themselves. This can
|
96
129
|
# help avoid loading dependency issues. This applies only to classes; modules cannot be
|
97
130
|
# passed in as strings.
|
131
|
+
#
|
132
|
+
# @param [Class, Module, String, Array<Class, Module, String>] klass The class or module to remove the formatters for.
|
133
|
+
# @return [self] Returns itself so that remove statements can be chained together.
|
98
134
|
def remove(klass)
|
99
135
|
Array(klass).each do |k|
|
100
|
-
if k.
|
136
|
+
if k.instance_of?(Module)
|
101
137
|
@module_formatters.delete(k)
|
102
138
|
else
|
103
139
|
k = k.name if k.is_a?(Class)
|
@@ -108,13 +144,18 @@ module Lumberjack
|
|
108
144
|
end
|
109
145
|
|
110
146
|
# Remove all formatters including the default formatter. Can be chained to add method calls.
|
147
|
+
#
|
148
|
+
# @return [self] Returns itself so that clear statements can be chained together.
|
111
149
|
def clear
|
112
150
|
@class_formatters.clear
|
113
151
|
@module_formatters.clear
|
114
152
|
self
|
115
153
|
end
|
116
154
|
|
117
|
-
# Format a message object
|
155
|
+
# Format a message object by applying all formatters attached to it.
|
156
|
+
#
|
157
|
+
# @param [Object] message The message object to format.
|
158
|
+
# @return [Object] The formatted object.
|
118
159
|
def format(message)
|
119
160
|
formatter = formatter_for(message.class)
|
120
161
|
if formatter&.respond_to?(:call)
|
@@ -126,6 +167,11 @@ module Lumberjack
|
|
126
167
|
|
127
168
|
# Compatibility with the Logger::Formatter signature. This method will just convert the message
|
128
169
|
# object to a string and ignores the other parameters.
|
170
|
+
#
|
171
|
+
# @param [Integer, String, Symbol] severity The severity of the message.
|
172
|
+
# @param [Time] timestamp The time the message was logged.
|
173
|
+
# @param [String] progname The name of the program logging the message.
|
174
|
+
# @param [Object] msg The message object to format.
|
129
175
|
def call(severity, timestamp, progname, msg)
|
130
176
|
"#{format(msg)}#{Lumberjack::LINE_SEPARATOR}"
|
131
177
|
end
|
@@ -133,7 +179,7 @@ module Lumberjack
|
|
133
179
|
private
|
134
180
|
|
135
181
|
# Find the formatter for a class by looking it up using the class hierarchy.
|
136
|
-
def formatter_for(klass)
|
182
|
+
def formatter_for(klass) # :nodoc:
|
137
183
|
check_modules = true
|
138
184
|
until klass.nil?
|
139
185
|
formatter = @class_formatters[klass.name]
|
data/lib/lumberjack/log_entry.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Lumberjack
|
4
4
|
# An entry in a log is a data structure that captures the log message as well as
|
@@ -10,6 +10,14 @@ module Lumberjack
|
|
10
10
|
|
11
11
|
UNIT_OF_WORK_ID = "unit_of_work_id"
|
12
12
|
|
13
|
+
# Create a new log entry.
|
14
|
+
#
|
15
|
+
# @param [Time] time The time the log entry was created.
|
16
|
+
# @param [Integer, String] severity The severity of the log entry.
|
17
|
+
# @param [String] message The message to log.
|
18
|
+
# @param [String] progname The name of the program that created the log entry.
|
19
|
+
# @param [Integer] pid The process id of the program that created the log entry.
|
20
|
+
# @param [Hash] tags A hash of tags to associate with the log entry.
|
13
21
|
def initialize(time, severity, message, progname, pid, tags)
|
14
22
|
@time = time
|
15
23
|
@severity = (severity.is_a?(Integer) ? severity : Severity.label_to_level(severity))
|
@@ -58,7 +66,7 @@ module Lumberjack
|
|
58
66
|
private
|
59
67
|
|
60
68
|
def tags_to_s
|
61
|
-
tags_string = ""
|
69
|
+
tags_string = +""
|
62
70
|
tags&.each { |name, value| tags_string << " #{name}:#{value.inspect}" }
|
63
71
|
tags_string
|
64
72
|
end
|