lumberjack 1.2.4 → 1.2.8

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: 477e85433c8d726c881946ab35ad3d3fc1cffc20ceeec9efc41a74f7bf91ef8d
4
- data.tar.gz: 325441e14a7f92374002069190cc3c9324e5ccae48a550aae705f177e877d62b
3
+ metadata.gz: c7912cf1bfe32dcaa911a6a65a0b755fb3051eebc2a7e4a65ff5d422f2f61ad7
4
+ data.tar.gz: b4077d2ef97be7169ed0e7bca7d107ec3f42372dde45cae1e7cdcedae1fb4e61
5
5
  SHA512:
6
- metadata.gz: 2161522abc951fd9db62a6594bb5dbea0eb392aac7f42960818dbfe673f34822fcd6d3af3037a5ba6029f837ec54880c992b959a503a133c9be30ff81ae75c94
7
- data.tar.gz: cb0bea0a04c752eb2597ec57befc6391955adfb39f26201041ce7023c458e69b83a36ccdd1bdaeaa7bb04f41686441c6e083461fa40775b563570300507032f4
6
+ metadata.gz: a092775aec0c7670eda8ff4959a7cc928a57b0934ecc8a1190efe395b437bc0a681bdbd2f3455b98f3cf757904f5f39b31c96e24e9c8bcd7a481b806027e92c8
7
+ data.tar.gz: 885f76da2738f3f6236a363d9a7dea68f83a80a03e123a3c52c578984f963e959de6fd60d0fd222d9a9933c22ed959181538abb4118163c8fd6528292e88673c
data/CHANGELOG.md CHANGED
@@ -1,6 +1,25 @@
1
+ ## 1.2.8
2
+
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`.
5
+
6
+ ## 1.2.7
7
+
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.
9
+
10
+ ## 1.2.6
11
+
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
14
+
15
+ ## 1.2.5
16
+
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.
19
+
1
20
  ## 1.2.4
2
21
 
3
- * Enhance ActiveSupport::TaggedLogging support so code that Lumberjack loggers can be wrapped with a tagged logger.
22
+ * Enhance `ActiveSupport::TaggedLogging` support so code that Lumberjack loggers can be wrapped with a tagged logger.
4
23
 
5
24
  ## 1.2.3
6
25
 
@@ -16,7 +35,7 @@
16
35
 
17
36
  ## 1.2.0
18
37
 
19
- * Enable compatibility with ActiveSupport::TaggedLogger by calling `tagged_logger!` on a logger.
38
+ * Enable compatibility with `ActiveSupport::TaggedLogger` by calling `tagged_logger!` on a logger.
20
39
  * Add `tag_formatter` to logger to specify formatting of tags for output.
21
40
  * Allow adding and removing classes by name to formatters.
22
41
  * Allow adding and removing multiple classes in a single call to a formatter.
@@ -35,7 +54,7 @@
35
54
 
36
55
  ## 1.1.0
37
56
 
38
- * Change Lumberjack::Logger to inherit from ::Logger
57
+ * Change `Lumberjack::Logger` to inherit from ::Logger
39
58
  * Add support for tags on log messages
40
59
  * Add global tag context for all loggers
41
60
  * Add per logger tags and tag contexts
@@ -43,8 +62,8 @@
43
62
  * Add support for setting datetime format on log devices
44
63
  * Performance optimizations
45
64
  * Add Multi device to output to multiple devices
46
- * Add DateTimeFormatter, IdFormatter, ObjectFormatter, and StructuredFormatter
47
- * Add rack Context middleware for setting thread global context
65
+ * Add `DateTimeFormatter`, `IdFormatter`, `ObjectFormatter`, and `StructuredFormatter`
66
+ * Add rack `Context` middleware for setting thread global context
48
67
  * End support for ruby versions < 2.3
49
68
  * Add support for modules in formatters
50
69
 
@@ -56,7 +75,7 @@
56
75
 
57
76
  ## 1.0.12
58
77
 
59
- * Add support for ActionDispatch request id for better Rails compatibility.
78
+ * Add support for `ActionDispatch` request id for better Rails compatibility.
60
79
 
61
80
  ## 1.0.11
62
81
 
@@ -70,11 +89,11 @@
70
89
 
71
90
  ## 1.0.9
72
91
 
73
- * Add method so Formatter is compatible with ActiveSupport logging extensions.
92
+ * Add method so Formatter is compatible with `ActiveSupport` logging extensions.
74
93
 
75
94
  ## 1.0.8
76
95
 
77
- * Fix another internal variable name conflict with ActiveSupport logging extensions.
96
+ * Fix another internal variable name conflict with `ActiveSupport` logging extensions.
78
97
 
79
98
  ## 1.0.7
80
99
 
@@ -82,7 +101,7 @@
82
101
 
83
102
  ## 1.0.6
84
103
 
85
- * Fix internal variable name conflict with ActiveSupport logging extensions.
104
+ * Fix internal variable name conflict with `ActiveSupport` logging extensions.
86
105
 
87
106
  ## 1.0.5
88
107
 
File without changes
data/README.md CHANGED
@@ -1,7 +1,8 @@
1
- # Lumberjack
2
-
3
- [![Build Status](https://travis-ci.org/bdurand/lumberjack.svg?branch=master)](https://travis-ci.org/bdurand/lumberjack)
1
+ ![Continuous Integration](https://github.com/bdurand/lumberjack/workflows/Continuous%20Integration/badge.svg)
4
2
  [![Maintainability](https://api.codeclimate.com/v1/badges/a0abc03721fff9b0cde1/maintainability)](https://codeclimate.com/github/bdurand/lumberjack/maintainability)
3
+ [![Ruby Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://github.com/testdouble/standard)
4
+
5
+ # Lumberjack
5
6
 
6
7
  Lumberjack is a simple, powerful, and fast logging implementation in Ruby. It uses nearly the same API as the Logger class in the Ruby standard library and as ActiveSupport::BufferedLogger in Rails.
7
8
 
@@ -59,8 +60,10 @@ You can specify tags that will only be applied to the logger in a block as well.
59
60
  ```ruby
60
61
  logger.tag(thread_id: Thread.current.object_id) do
61
62
  logger.info("here") # Will include the `thread_id` tag
63
+ logger.tag(count: 15)
64
+ logger.info("with count") # Will include the `count` tag
62
65
  end
63
- logger.info("there") # Will not include the `thread_id` tag
66
+ logger.info("there") # Will not include the `thread_id` or `count` tag
64
67
  ```
65
68
 
66
69
  You can also set tags to `Proc` objects that will be evaluated when creating a log entry.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.4
1
+ 1.2.8
data/lib/lumberjack.rb CHANGED
@@ -1,27 +1,26 @@
1
1
  # frozen_string_literals: true
2
2
 
3
- require 'rbconfig'
4
- require 'time'
5
- require 'thread'
6
- require 'securerandom'
7
- require 'logger'
3
+ require "rbconfig"
4
+ require "time"
5
+ require "securerandom"
6
+ require "logger"
8
7
 
9
8
  module Lumberjack
10
- LINE_SEPARATOR = (RbConfig::CONFIG['host_os'].match(/mswin/i) ? "\r\n" : "\n")
9
+ LINE_SEPARATOR = (RbConfig::CONFIG["host_os"] =~ /mswin/i ? "\r\n" : "\n")
11
10
 
12
- require_relative "lumberjack/severity.rb"
13
- require_relative "lumberjack/formatter.rb"
11
+ require_relative "lumberjack/severity"
12
+ require_relative "lumberjack/formatter"
14
13
 
15
- require_relative "lumberjack/context.rb"
16
- require_relative "lumberjack/log_entry.rb"
17
- require_relative "lumberjack/device.rb"
18
- require_relative "lumberjack/logger.rb"
19
- require_relative "lumberjack/tags.rb"
20
- require_relative "lumberjack/tag_formatter.rb"
21
- require_relative "lumberjack/tagged_logger_support.rb"
22
- require_relative "lumberjack/tagged_logging.rb"
23
- require_relative "lumberjack/template.rb"
24
- require_relative "lumberjack/rack.rb"
14
+ require_relative "lumberjack/context"
15
+ require_relative "lumberjack/log_entry"
16
+ require_relative "lumberjack/device"
17
+ require_relative "lumberjack/logger"
18
+ require_relative "lumberjack/tags"
19
+ require_relative "lumberjack/tag_formatter"
20
+ require_relative "lumberjack/tagged_logger_support"
21
+ require_relative "lumberjack/tagged_logging"
22
+ require_relative "lumberjack/template"
23
+ require_relative "lumberjack/rack"
25
24
 
26
25
  class << self
27
26
  # Define a unit of work within a block. Within the block supplied to this
@@ -54,31 +53,41 @@ module Lumberjack
54
53
  #
55
54
  # Otherwise, it will return the current context. If one doesn't exist, it will return a new one
56
55
  # but that context will not be in any scope.
57
- def context
56
+ def context(&block)
58
57
  current_context = Thread.current[:lumberjack_context]
59
- if block_given?
60
- Thread.current[:lumberjack_context] = Context.new(current_context)
61
- begin
62
- yield
63
- ensure
64
- Thread.current[:lumberjack_context] = current_context
65
- end
58
+ if block
59
+ use_context(Context.new(current_context), &block)
66
60
  else
67
61
  current_context || Context.new
68
62
  end
69
63
  end
70
64
 
65
+ # Set the context to use within a block.
66
+ def use_context(context, &block)
67
+ current_context = Thread.current[:lumberjack_context]
68
+ begin
69
+ Thread.current[:lumberjack_context] = (context || Context.new)
70
+ yield
71
+ ensure
72
+ Thread.current[:lumberjack_context] = current_context
73
+ end
74
+ end
75
+
76
+ # Return true if inside a context block.
77
+ def context?
78
+ !!Thread.current[:lumberjack_context]
79
+ end
80
+
71
81
  # Return the tags from the current context or nil if there are no tags.
72
82
  def context_tags
73
83
  context = Thread.current[:lumberjack_context]
74
- context.tags if context
84
+ context&.tags
75
85
  end
76
86
 
77
87
  # Set tags on the current context
78
88
  def tag(tags)
79
89
  context = Thread.current[:lumberjack_context]
80
- context.tag(tags) if context
90
+ context&.tag(tags)
81
91
  end
82
-
83
92
  end
84
93
  end
@@ -4,37 +4,37 @@ module Lumberjack
4
4
  # This is an abstract class for logging devices. Subclasses must implement the +write+ method and
5
5
  # may implement the +close+ and +flush+ methods if applicable.
6
6
  class Device
7
- require_relative "device/writer.rb"
8
- require_relative "device/log_file.rb"
9
- require_relative "device/rolling_log_file.rb"
10
- require_relative "device/date_rolling_log_file.rb"
11
- require_relative "device/size_rolling_log_file.rb"
12
- require_relative "device/multi.rb"
13
- require_relative "device/null.rb"
7
+ require_relative "device/writer"
8
+ require_relative "device/log_file"
9
+ require_relative "device/rolling_log_file"
10
+ require_relative "device/date_rolling_log_file"
11
+ require_relative "device/size_rolling_log_file"
12
+ require_relative "device/multi"
13
+ require_relative "device/null"
14
14
 
15
15
  # Subclasses must implement this method to write a LogEntry.
16
16
  def write(entry)
17
17
  raise NotImplementedError
18
18
  end
19
-
19
+
20
20
  # Subclasses may implement this method to close the device.
21
21
  def close
22
22
  flush
23
23
  end
24
-
24
+
25
25
  # Subclasses may implement this method to reopen the device.
26
26
  def reopen(logdev = nil)
27
27
  flush
28
28
  end
29
-
29
+
30
30
  # Subclasses may implement this method to flush any buffers used by the device.
31
31
  def flush
32
32
  end
33
-
33
+
34
34
  # Subclasses may implement this method to get the format for log timestamps.
35
35
  def datetime_format
36
36
  end
37
-
37
+
38
38
  # Subclasses may implement this method to set a format for log timestamps.
39
39
  def datetime_format=(format)
40
40
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literals: true
2
2
 
3
- require 'date'
3
+ require "date"
4
4
 
5
5
  module Lumberjack
6
6
  class Device
@@ -16,7 +16,7 @@ module Lumberjack
16
16
  def initialize(path, options = {})
17
17
  @manual = options[:manual]
18
18
  @file_date = Date.today
19
- if options[:roll] && options[:roll].to_s.match(/(daily)|(weekly)|(monthly)/i)
19
+ if options[:roll]&.to_s&.match(/(daily)|(weekly)|(monthly)/i)
20
20
  @roll_period = $~[0].downcase.to_sym
21
21
  options.delete(:roll)
22
22
  else
@@ -28,11 +28,11 @@ module Lumberjack
28
28
  def archive_file_suffix
29
29
  case @roll_period
30
30
  when :weekly
31
- "#{@file_date.strftime('week-of-%Y-%m-%d')}"
31
+ @file_date.strftime("week-of-%Y-%m-%d").to_s
32
32
  when :monthly
33
- "#{@file_date.strftime('%Y-%m')}"
33
+ @file_date.strftime("%Y-%m").to_s
34
34
  else
35
- "#{@file_date.strftime('%Y-%m-%d')}"
35
+ @file_date.strftime("%Y-%m-%d").to_s
36
36
  end
37
37
  end
38
38
 
@@ -54,9 +54,9 @@ module Lumberjack
54
54
  end
55
55
  end
56
56
  end
57
-
57
+
58
58
  protected
59
-
59
+
60
60
  def after_roll
61
61
  @file_date = Date.today
62
62
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literals: true
2
2
 
3
- require 'fileutils'
3
+ require "fileutils"
4
4
 
5
5
  module Lumberjack
6
6
  class Device
@@ -10,23 +10,23 @@ module Lumberjack
10
10
 
11
11
  # The absolute path of the file being logged to.
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
15
  def initialize(path, options = {})
16
16
  @path = File.expand_path(path)
17
17
  FileUtils.mkdir_p(File.dirname(@path))
18
18
  super(file_stream, options)
19
19
  end
20
-
20
+
21
21
  def reopen(logdev = nil)
22
22
  close
23
23
  @stream = file_stream
24
24
  end
25
-
25
+
26
26
  private
27
-
27
+
28
28
  def file_stream
29
- File.new(@path, 'a', :encoding => EXTERNAL_ENCODING)
29
+ File.new(@path, "a", encoding: EXTERNAL_ENCODING)
30
30
  end
31
31
  end
32
32
  end
@@ -7,35 +7,35 @@ module Lumberjack
7
7
  def initialize(*devices)
8
8
  @devices = devices.flatten
9
9
  end
10
-
10
+
11
11
  def write(entry)
12
12
  @devices.each do |device|
13
13
  device.write(entry)
14
14
  end
15
15
  end
16
-
16
+
17
17
  def flush
18
18
  @devices.each do |device|
19
19
  device.flush
20
20
  end
21
21
  end
22
-
22
+
23
23
  def close
24
24
  @devices.each do |device|
25
25
  device.close
26
26
  end
27
27
  end
28
-
28
+
29
29
  def reopen(logdev = nil)
30
30
  @devices.each do |device|
31
31
  device.reopen(logdev = nil)
32
32
  end
33
33
  end
34
-
34
+
35
35
  def datetime_format
36
36
  @devices.detect(&:datetime_format).datetime_format
37
37
  end
38
-
38
+
39
39
  def datetime_format=(format)
40
40
  @devices.each do |device|
41
41
  device.datetime_format = format
@@ -7,7 +7,7 @@ module Lumberjack
7
7
  class Null < Device
8
8
  def initialize(*args)
9
9
  end
10
-
10
+
11
11
  def write(entry)
12
12
  end
13
13
  end
@@ -14,35 +14,43 @@ module Lumberjack
14
14
  class RollingLogFile < LogFile
15
15
  attr_reader :path
16
16
  attr_accessor :keep
17
-
17
+
18
18
  def initialize(path, options = {})
19
19
  @path = File.expand_path(path)
20
20
  @keep = options[:keep]
21
21
  super(path, options)
22
- @file_inode = stream.lstat.ino rescue nil
22
+ @file_inode = begin
23
+ stream.lstat.ino
24
+ rescue
25
+ nil
26
+ end
23
27
  @@rolls = []
24
28
  @next_stat_check = Time.now.to_f
25
29
  @min_roll_check = (options[:min_roll_check] || 1.0).to_f
26
30
  end
27
-
31
+
28
32
  # Returns a suffix that will be appended to the file name when it is archived.. The suffix should
29
33
  # change after it is time to roll the file. The log file will be renamed when it is rolled.
30
34
  def archive_file_suffix
31
35
  raise NotImplementedError
32
36
  end
33
-
37
+
34
38
  # Return +true+ if the file should be rolled.
35
39
  def roll_file?
36
40
  raise NotImplementedError
37
41
  end
38
-
42
+
39
43
  # Roll the log file by renaming it to the archive file name and then re-opening a stream to the log
40
44
  # file path. Rolling a file is safe in multi-threaded or multi-process environments.
41
45
  def roll_file! #:nodoc:
42
46
  do_once(stream) do
43
47
  archive_file = "#{path}.#{archive_file_suffix}"
44
48
  stream.flush
45
- current_inode = File.stat(path).ino rescue nil
49
+ current_inode = begin
50
+ File.stat(path).ino
51
+ rescue
52
+ nil
53
+ end
46
54
  if @file_inode && current_inode == @file_inode && !File.exist?(archive_file) && File.exist?(path)
47
55
  begin
48
56
  File.rename(path, archive_file)
@@ -59,41 +67,49 @@ module Lumberjack
59
67
  end
60
68
 
61
69
  protected
62
-
70
+
63
71
  # This method will be called after a file has been rolled. Subclasses can
64
72
  # implement code to reset the state of the device. This method is thread safe.
65
73
  def after_roll
66
74
  end
67
-
75
+
68
76
  # Handle rolling the file before flushing.
69
77
  def before_flush # :nodoc:
70
78
  if @min_roll_check <= 0.0 || Time.now.to_f >= @next_stat_check
71
79
  @next_stat_check += @min_roll_check
72
- path_inode = File.lstat(path).ino rescue nil
80
+ path_inode = begin
81
+ File.lstat(path).ino
82
+ rescue
83
+ nil
84
+ end
73
85
  if path_inode != @file_inode
74
86
  @file_inode = path_inode
75
87
  reopen_file
76
- else
77
- roll_file! if roll_file?
88
+ elsif roll_file?
89
+ roll_file!
78
90
  end
79
91
  end
80
92
  end
81
-
93
+
82
94
  private
83
95
 
84
96
  def reopen_file
85
97
  old_stream = stream
86
- new_stream = File.open(path, 'a', encoding: EXTERNAL_ENCODING)
98
+ new_stream = File.open(path, "a", encoding: EXTERNAL_ENCODING)
87
99
  new_stream.sync = true if buffer_size > 0
88
- @file_inode = new_stream.lstat.ino rescue nil
100
+ @file_inode = begin
101
+ new_stream.lstat.ino
102
+ rescue
103
+ nil
104
+ end
89
105
  self.stream = new_stream
90
106
  old_stream.close
91
107
  end
92
108
  end
93
-
109
+
94
110
  def cleanup_files!
95
111
  if keep
96
- files = Dir.glob("#{path}.*").collect{|f| [f, File.ctime(f)]}.sort{|a,b| b.last <=> a.last}.collect{|a| a.first}
112
+ files = Dir.glob("#{path}.*").collect { |f| [f, File.ctime(f)] }.sort { |a, b| b.last <=> a.last }.collect { |a| a.first }
97
113
  if files.size > keep
98
114
  files[keep, files.length].each do |f|
99
115
  File.delete(f)
@@ -101,7 +117,7 @@ module Lumberjack
101
117
  end
102
118
  end
103
119
  end
104
-
120
+
105
121
  def do_once(file)
106
122
  begin
107
123
  file.flock(File::LOCK_EX)
@@ -110,11 +126,19 @@ module Lumberjack
110
126
  return
111
127
  end
112
128
  begin
113
- verify = file.lstat rescue nil
129
+ verify = begin
130
+ file.lstat
131
+ rescue
132
+ nil
133
+ end
114
134
  # Execute only if the file we locked is still the same one that needed to be rolled
115
135
  yield if verify && verify.ino == @file_inode && verify.size > 0
116
136
  ensure
117
- file.flock(File::LOCK_UN) rescue nil
137
+ begin
138
+ file.flock(File::LOCK_UN)
139
+ rescue
140
+ nil
141
+ end
118
142
  end
119
143
  end
120
144
  end