lumberjack 1.2.8 → 1.2.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c7912cf1bfe32dcaa911a6a65a0b755fb3051eebc2a7e4a65ff5d422f2f61ad7
4
- data.tar.gz: b4077d2ef97be7169ed0e7bca7d107ec3f42372dde45cae1e7cdcedae1fb4e61
3
+ metadata.gz: 9d05405664f1f85b1a68eb24c21a89d3b57ea8caf09513a348a138d53f1870b0
4
+ data.tar.gz: 1ccc42fdd657ab4acf89bab8bfd7fb5e7d69d3825367483c49ee10c5c6e037d1
5
5
  SHA512:
6
- metadata.gz: a092775aec0c7670eda8ff4959a7cc928a57b0934ecc8a1190efe395b437bc0a681bdbd2f3455b98f3cf757904f5f39b31c96e24e9c8bcd7a481b806027e92c8
7
- data.tar.gz: 885f76da2738f3f6236a363d9a7dea68f83a80a03e123a3c52c578984f963e959de6fd60d0fd222d9a9933c22ed959181538abb4118163c8fd6528292e88673c
6
+ metadata.gz: b48a0035d1adf22768071e6025692f001a8d13b931314ab0b6ea9131696bcb38d321db5fc1246bf2c49623cad26770bfdb32fe0e8e169ae455322e2693dad8e8
7
+ data.tar.gz: f80ae18348c1a3e5261aa27051d4cf3e087550e0d57d3a13459da6f7641e0ea270382478108127f03f6b7596faab27d8d3f92488fbbb2cb0c50c4d247ac61910
data/CHANGELOG.md CHANGED
@@ -1,129 +1,182 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## 1.2.9
8
+
9
+ ### Added
10
+ - Allow passing in formatters as class names when adding them.
11
+ - Allow passing in formatters initialization arguments when adding them.
12
+ - Add truncate formatter for capping the length of log messages.
13
+
1
14
  ## 1.2.8
2
15
 
3
- * Add `Logger#untagged` to remove previously set logging tags from a block.
4
- * Return result of the block when a block is passed to `Logger#tag`.
16
+ ### Added
17
+ - Add `Logger#untagged` to remove previously set logging tags from a block.
18
+ - Return result of the block when a block is passed to `Logger#tag`.
5
19
 
6
20
  ## 1.2.7
7
21
 
8
- * Allow passing frozen hashes to `Logger#tag`. Tags passed to this method are now duplicated so the logger maintains it's own copy of the hash.
22
+ ### Fixed
23
+ - Allow passing frozen hashes to `Logger#tag`. Tags passed to this method are now duplicated so the logger maintains it's own copy of the hash.
9
24
 
10
25
  ## 1.2.6
11
26
 
12
- * Fix `Logger#tag` so it only ads to the current block's logger tags instead of the global tags if called inside a `Logger#tag` block.
13
- * Add Logger#remove_tag
27
+ ### Added
28
+ - Add Logger#remove_tag
29
+
30
+ ### Fixed
31
+ - Fix `Logger#tag` so it only ads to the current block's logger tags instead of the global tags if called inside a `Logger#tag` block.
32
+
14
33
 
15
34
  ## 1.2.5
16
35
 
17
- * Fix logic with recursive reference guard in StructuredFormatter so it only suppresses Enumerable references.
18
- * Add support for bang methods (error!) for setting the log level.
36
+ ### Added
37
+ - Add support for bang methods (error!) for setting the log level.
38
+
39
+ ### Fixed
40
+ - Fixed logic with recursive reference guard in StructuredFormatter so it only suppresses Enumerable references.
19
41
 
20
42
  ## 1.2.4
21
43
 
22
- * Enhance `ActiveSupport::TaggedLogging` support so code that Lumberjack loggers can be wrapped with a tagged logger.
44
+ ### Added
45
+ - Enhance `ActiveSupport::TaggedLogging` support so code that Lumberjack loggers can be wrapped with a tagged logger.
23
46
 
24
47
  ## 1.2.3
25
48
 
26
- * Fix structured formatter so no-recursive, duplicate references are allowed.
49
+ ### Fixed
50
+ - Fix structured formatter so no-recursive, duplicate references are allowed.
27
51
 
28
52
  ## 1.2.2
29
53
 
30
- * Prevent infinite loops in the structured formatter where objects have backreferences to each other.
54
+ ### Fixed
55
+ - Prevent infinite loops in the structured formatter where objects have backreferences to each other.
31
56
 
32
57
  ## 1.2.1
33
58
 
34
- * Prevent infinite loops where logging a statement triggers the logger.
59
+ ### Fixed
60
+ - Prevent infinite loops where logging a statement triggers the logger.
35
61
 
36
62
  ## 1.2.0
37
63
 
38
- * Enable compatibility with `ActiveSupport::TaggedLogger` by calling `tagged_logger!` on a logger.
39
- * Add `tag_formatter` to logger to specify formatting of tags for output.
40
- * Allow adding and removing classes by name to formatters.
41
- * Allow adding and removing multiple classes in a single call to a formatter.
42
- * Allow using symbols and strings as log level for silencing a logger.
43
- * Ensure flusher thread gets stopped when logger is closed.
44
- * Add writer for logger device attribute.
45
- * Handle passing an array of devices to a multi device.
46
- * Helper method to get a tag with a specified name.
47
- * Add strip formatter to strip whitespace from strings.
48
- * Support non-alpha numeric characters in template variables.
49
- * Add backtrace cleaner to ExceptionFormatter.
64
+ ### Added
65
+ - Enable compatibility with `ActiveSupport::TaggedLogger` by calling `tagged_logger!` on a logger.
66
+ - Add `tag_formatter` to logger to specify formatting of tags for output.
67
+ - Allow adding and removing classes by name to formatters.
68
+ - Allow adding and removing multiple classes in a single call to a formatter.
69
+ - Allow using symbols and strings as log level for silencing a logger.
70
+ - Ensure flusher thread gets stopped when logger is closed.
71
+ - Add writer for logger device attribute.
72
+ - Handle passing an array of devices to a multi device.
73
+ - Helper method to get a tag with a specified name.
74
+ - Add strip formatter to strip whitespace from strings.
75
+ - Support non-alpha numeric characters in template variables.
76
+ - Add backtrace cleaner to ExceptionFormatter.
50
77
 
51
78
  ## 1.1.1
52
79
 
53
- * Replace Procs in tag values with the value of calling the Proc in log entries.
80
+ ### Added
81
+ - Replace Procs in tag values with the value of calling the Proc in log entries.
54
82
 
55
83
  ## 1.1.0
56
84
 
57
- * Change `Lumberjack::Logger` to inherit from ::Logger
58
- * Add support for tags on log messages
59
- * Add global tag context for all loggers
60
- * Add per logger tags and tag contexts
61
- * Reimplement unit of work id as a tag on log entries
62
- * Add support for setting datetime format on log devices
63
- * Performance optimizations
64
- * Add Multi device to output to multiple devices
65
- * Add `DateTimeFormatter`, `IdFormatter`, `ObjectFormatter`, and `StructuredFormatter`
66
- * Add rack `Context` middleware for setting thread global context
67
- * End support for ruby versions < 2.3
68
- * Add support for modules in formatters
85
+ ### Added
86
+ - Change `Lumberjack::Logger` to inherit from ::Logger
87
+ - Add support for tags on log messages
88
+ - Add global tag context for all loggers
89
+ - Add per logger tags and tag contexts
90
+ - Reimplement unit of work id as a tag on log entries
91
+ - Add support for setting datetime format on log devices
92
+ - Performance optimizations
93
+ - Add Multi device to output to multiple devices
94
+ - Add `DateTimeFormatter`, `IdFormatter`, `ObjectFormatter`, and `StructuredFormatter`
95
+ - Add rack `Context` middleware for setting thread global context
96
+ - Add support for modules in formatters
97
+
98
+ ### Removed
99
+ - End support for ruby versions < 2.3
69
100
 
70
101
  ## 1.0.13
71
102
 
72
- * Reduce amount of code executed inside a mutex lock when writing to the logger stream.
73
- * Added `:min_roll_check` option to `Lumberjack::Device::RollingLogFile` to reduce file system checks. Default is now to only check if a file needs to be rolled at most once per second.
74
- * Force immutable strings for Ruby versions that support them.
103
+ ### Added
104
+ - Added `:min_roll_check` option to `Lumberjack::Device::RollingLogFile` to reduce file system checks. Default is now to only check if a file needs to be rolled at most once per second.
105
+ - Force immutable strings for Ruby versions that support them.
106
+
107
+ ### Changed
108
+ - Reduce amount of code executed inside a mutex lock when writing to the logger stream.
75
109
 
76
110
  ## 1.0.12
77
111
 
78
- * Add support for `ActionDispatch` request id for better Rails compatibility.
112
+ ### Added
113
+ - Add support for `ActionDispatch` request id for better Rails compatibility.
79
114
 
80
115
  ## 1.0.11
81
116
 
82
- * Fix Ruby 2.4 deprecation warning on Fixnum (thanks koic).
83
- * Fix gemspec files to be flat array (thanks e2).
117
+ ### Fixed
118
+ - Fix Ruby 2.4 deprecation warning on Fixnum (thanks koic).
119
+ - Fix gemspec files to be flat array (thanks e2).
84
120
 
85
121
  ## 1.0.10
86
122
 
87
- * Expose option to manually roll log files.
88
- * Minor code cleanup.
123
+ ### Added
124
+ - Expose option to manually roll log files.
125
+
126
+ ### Changed
127
+ - Minor code cleanup.
89
128
 
90
129
  ## 1.0.9
91
130
 
92
- * Add method so Formatter is compatible with `ActiveSupport` logging extensions.
131
+ ### Added
132
+ - Add method so Formatter is compatible with `ActiveSupport` logging extensions.
93
133
 
94
134
  ## 1.0.8
95
135
 
96
- * Fix another internal variable name conflict with `ActiveSupport` logging extensions.
136
+ ### Fixed
137
+ - Fix another internal variable name conflict with `ActiveSupport` logging extensions.
97
138
 
98
139
  ## 1.0.7
99
140
 
100
- * Fix broken formatter attribute method.
141
+ ### Fixed
142
+ - Fix broken formatter attribute method.
101
143
 
102
144
  ## 1.0.6
103
145
 
104
- * Fix internal variable name conflict with `ActiveSupport` logging extensions.
146
+ ### Fixed
147
+ - Fix internal variable name conflict with `ActiveSupport` logging extensions.
105
148
 
106
149
  ## 1.0.5
107
150
 
108
- * Update docs.
109
- * Remove autoload calls to make thread safe.
110
- * Make compatible with Ruby 2.1.1 Pathname.
111
- * Make compatible with standard library Logger's use of progname as default message.
151
+ ### Changed
152
+ - Update docs.
153
+ - Remove autoload calls to make thread safe.
154
+ - Make compatible with Ruby 2.1.1 Pathname.
155
+ - Make compatible with standard library Logger's use of progname as default message.
112
156
 
113
157
  ## 1.0.4
114
158
 
115
- * Add ability to supply a unit of work id for a block instead of having one generated every time.
159
+ ### Added
160
+ - Add ability to supply a unit of work id for a block instead of having one generated every time.
116
161
 
117
162
  ## 1.0.3
118
163
 
119
- * Change log file output format to binary to avoid encoding warnings.
120
- * Fixed bug in log file rolling that left the file locked.
164
+ ### Fixed
165
+ - Change log file output format to binary to avoid encoding warnings.
166
+ - Fixed bug in log file rolling that left the file locked.
121
167
 
122
168
  ## 1.0.2
123
169
 
124
- * Remove deprecation warnings under ruby 1.9.3.
125
- * Add more error checking around file rolling.
170
+ ### Fixed
171
+ - Remove deprecation warnings under ruby 1.9.3.
172
+ - Add more error checking around file rolling.
126
173
 
127
174
  ## 1.0.1
128
175
 
129
- * Writes are no longer buffered by default.
176
+ ### Fixed
177
+ - Writes are no longer buffered by default.
178
+
179
+ ## 1.0.0
180
+
181
+ ### Added
182
+ - Initial release
data/README.md CHANGED
@@ -8,7 +8,7 @@ Lumberjack is a simple, powerful, and fast logging implementation in Ruby. It us
8
8
 
9
9
  ## Usage
10
10
 
11
- This code aims to be extremely simple to use. The core interface it the Lumberjack::Logger which is used to log messages (which can be any object) with a specified Severity. Each logger has a level associated with it and messages are only written if their severity is greater than or equal to the level.
11
+ This code aims to be extremely simple to use. The core interface is the Lumberjack::Logger which is used to log messages (which can be any object) with a specified Severity. Each logger has a level associated with it and messages are only written if their severity is greater than or equal to the level.
12
12
 
13
13
  ```ruby
14
14
  logger = Lumberjack::Logger.new("logs/application.log") # Open a new log file with INFO level
@@ -41,7 +41,7 @@ The following information is recorded for each message:
41
41
 
42
42
  ### Tags
43
43
 
44
- You can use tags to provide additional meta data about a log message or the context that the log message is being made in. Using tags can keep you log messages clean. You can avoid string interoplation to add additional meta data.
44
+ You can use tags to provide additional meta data about a log message or the context that the log message is being made in. Using tags can keep your log messages clean. You can avoid string interpolation to add additional meta data.
45
45
 
46
46
  Each of the logger methods includes an additional argument that can be used to specify tags on a messsage:
47
47
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.8
1
+ 1.2.9
@@ -5,12 +5,16 @@ module Lumberjack
5
5
  class Context
6
6
  attr_reader :tags
7
7
 
8
+ # @param [Context] parent_context A parent context to inherit tags from.
8
9
  def initialize(parent_context = nil)
9
10
  @tags = {}
10
11
  @tags.merge!(parent_context.tags) if parent_context
11
12
  end
12
13
 
13
14
  # Set tags on the context.
15
+ #
16
+ # @param [Hash] tags The tags to set.
17
+ # @return [void]
14
18
  def tag(tags)
15
19
  tags.each do |key, value|
16
20
  @tags[key.to_s] = value
@@ -18,16 +22,25 @@ module Lumberjack
18
22
  end
19
23
 
20
24
  # Get a context tag.
25
+ #
26
+ # @param [String, Symbol] key The tag key.
27
+ # @return [Object] The tag value.
21
28
  def [](key)
22
29
  @tags[key.to_s]
23
30
  end
24
31
 
25
32
  # Set a context tag.
33
+ #
34
+ # @param [String, Symbol] key The tag key.
35
+ # @param [Object] value The tag value.
36
+ # @return [void]
26
37
  def []=(key, value)
27
38
  @tags[key.to_s] = value
28
39
  end
29
40
 
30
41
  # Clear all the context data.
42
+ #
43
+ # @return [void]
31
44
  def reset
32
45
  @tags.clear
33
46
  end
@@ -13,6 +13,9 @@ module Lumberjack
13
13
  # Create a new logging device to the specified file. The period to roll the file is specified
14
14
  # with the :roll option which may contain a value of :daily, :weekly,
15
15
  # or :monthly.
16
+ #
17
+ # @param [String, Pathname] path The path to the log file.
18
+ # @param [Hash] options The options for the device.
16
19
  def initialize(path, options = {})
17
20
  @manual = options[:manual]
18
21
  @file_date = Date.today
@@ -25,6 +28,9 @@ module Lumberjack
25
28
  super
26
29
  end
27
30
 
31
+ # The date based suffix for file.
32
+ #
33
+ # @return [String]
28
34
  def archive_file_suffix
29
35
  case @roll_period
30
36
  when :weekly
@@ -36,6 +42,9 @@ module Lumberjack
36
42
  end
37
43
  end
38
44
 
45
+ # Check if the file should be rolled.
46
+ #
47
+ # @return [Boolean]
39
48
  def roll_file?
40
49
  if @manual
41
50
  true
@@ -12,12 +12,19 @@ module Lumberjack
12
12
  attr_reader :path
13
13
 
14
14
  # Create a logger to the file at +path+. Options are passed through to the Writer constructor.
15
+ #
16
+ # @param [String, Pathname] path The path to the log file.
17
+ # @param [Hash] options The options for the device.
15
18
  def initialize(path, options = {})
16
19
  @path = File.expand_path(path)
17
20
  FileUtils.mkdir_p(File.dirname(@path))
18
21
  super(file_stream, options)
19
22
  end
20
23
 
24
+ # Reopen the log file.
25
+ #
26
+ # @param [Object] logdev not used
27
+ # @return [void]
21
28
  def reopen(logdev = nil)
22
29
  close
23
30
  @stream = file_stream
@@ -4,6 +4,7 @@ module Lumberjack
4
4
  class Device
5
5
  # This is a logging device that forward log entries to multiple other devices.
6
6
  class Multi < Device
7
+ # @param [Array<Lumberjack::Device>] devices The devices to write to.
7
8
  def initialize(*devices)
8
9
  @devices = devices.flatten
9
10
  end
@@ -20,10 +20,10 @@ module Lumberjack
20
20
  @keep = options[:keep]
21
21
  super(path, options)
22
22
  @file_inode = begin
23
- stream.lstat.ino
24
- rescue
25
- nil
26
- end
23
+ stream.lstat.ino
24
+ rescue
25
+ nil
26
+ end
27
27
  @@rolls = []
28
28
  @next_stat_check = Time.now.to_f
29
29
  @min_roll_check = (options[:min_roll_check] || 1.0).to_f
@@ -42,15 +42,15 @@ module Lumberjack
42
42
 
43
43
  # Roll the log file by renaming it to the archive file name and then re-opening a stream to the log
44
44
  # file path. Rolling a file is safe in multi-threaded or multi-process environments.
45
- def roll_file! #:nodoc:
45
+ def roll_file! # :nodoc:
46
46
  do_once(stream) do
47
47
  archive_file = "#{path}.#{archive_file_suffix}"
48
48
  stream.flush
49
49
  current_inode = begin
50
- File.stat(path).ino
51
- rescue
52
- nil
53
- end
50
+ File.stat(path).ino
51
+ rescue
52
+ nil
53
+ end
54
54
  if @file_inode && current_inode == @file_inode && !File.exist?(archive_file) && File.exist?(path)
55
55
  begin
56
56
  File.rename(path, archive_file)
@@ -78,10 +78,10 @@ module Lumberjack
78
78
  if @min_roll_check <= 0.0 || Time.now.to_f >= @next_stat_check
79
79
  @next_stat_check += @min_roll_check
80
80
  path_inode = begin
81
- File.lstat(path).ino
82
- rescue
83
- nil
84
- end
81
+ File.lstat(path).ino
82
+ rescue
83
+ nil
84
+ end
85
85
  if path_inode != @file_inode
86
86
  @file_inode = path_inode
87
87
  reopen_file
@@ -98,10 +98,10 @@ module Lumberjack
98
98
  new_stream = File.open(path, "a", encoding: EXTERNAL_ENCODING)
99
99
  new_stream.sync = true if buffer_size > 0
100
100
  @file_inode = begin
101
- new_stream.lstat.ino
102
- rescue
103
- nil
104
- end
101
+ new_stream.lstat.ino
102
+ rescue
103
+ nil
104
+ end
105
105
  self.stream = new_stream
106
106
  old_stream.close
107
107
  end
@@ -127,10 +127,10 @@ module Lumberjack
127
127
  end
128
128
  begin
129
129
  verify = begin
130
- file.lstat
131
- rescue
132
- nil
133
- end
130
+ file.lstat
131
+ rescue
132
+ nil
133
+ end
134
134
  # Execute only if the file we locked is still the same one that needed to be rolled
135
135
  yield if verify && verify.ino == @file_inode && verify.size > 0
136
136
  ensure
@@ -60,6 +60,9 @@ module Lumberjack
60
60
  # work id will only appear if it is present.
61
61
  #
62
62
  # The size of the internal buffer in bytes can be set by providing :buffer_size (defaults to 32K).
63
+ #
64
+ # @param [IO] stream The stream to write log entries to.
65
+ # @param [Hash] options The options for the device.
63
66
  def initialize(stream, options = {})
64
67
  @lock = Mutex.new
65
68
  @stream = stream
@@ -77,12 +80,18 @@ module Lumberjack
77
80
 
78
81
  # Set the buffer size in bytes. The device will only be physically written to when the buffer size
79
82
  # is exceeded.
83
+ #
84
+ # @param [Integer] value The size of the buffer in bytes.
85
+ # @return [void]
80
86
  def buffer_size=(value)
81
87
  @buffer_size = value
82
88
  flush
83
89
  end
84
90
 
85
91
  # Write an entry to the stream. The entry will be converted into a string using the defined template.
92
+ #
93
+ # @param [LogEntry, String] entry The entry to write to the stream.
94
+ # @return [void]
86
95
  def write(entry)
87
96
  string = (entry.is_a?(LogEntry) ? @template.call(entry) : entry)
88
97
  return if string.nil?
@@ -105,12 +114,16 @@ module Lumberjack
105
114
  end
106
115
 
107
116
  # Close the underlying stream.
117
+ #
118
+ # @return [void]
108
119
  def close
109
120
  flush
110
121
  stream.close
111
122
  end
112
123
 
113
124
  # Flush the underlying stream.
125
+ #
126
+ # @return [void]
114
127
  def flush
115
128
  lines = nil
116
129
  @lock.synchronize do
@@ -120,10 +133,17 @@ module Lumberjack
120
133
  write_to_stream(lines) if lines
121
134
  end
122
135
 
136
+ # Get the datetime format.
137
+ #
138
+ # @return [String] The datetime format.
123
139
  def datetime_format
124
140
  @template.datetime_format if @template.respond_to?(:datetime_format)
125
141
  end
126
142
 
143
+ # Set the datetime format.
144
+ #
145
+ # @param [String] format The datetime format.
146
+ # @return [void]
127
147
  def datetime_format=(format)
128
148
  if @template.respond_to?(:datetime_format=)
129
149
  @template.datetime_format = format
@@ -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
@@ -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
@@ -9,6 +9,8 @@ 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
@@ -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
@@ -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
@@ -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_literals: 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