lumberjack 1.2.10 → 1.3.1

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.
@@ -0,0 +1,133 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "socket"
4
+
5
+ module Lumberjack
6
+ module Utils
7
+ UNDEFINED = Object.new.freeze
8
+ private_constant :UNDEFINED
9
+
10
+ NON_SLUGGABLE_PATTERN = /[^A-Za-z0-9_.-]+/.freeze
11
+ private_constant :NON_SLUGGABLE_PATTERN
12
+
13
+ @deprecations = nil
14
+ @deprecations_lock = nil
15
+ @hostname = UNDEFINED
16
+
17
+ class << self
18
+ # Print warning when deprecated methods are called the first time. This can be disabled
19
+ # by setting the environment variable `LUMBERJACK_NO_DEPRECATION_WARNINGS` to "true".
20
+ # You can see every usage of a deprecated method along with a full stack trace by setting
21
+ # the environment variable `VERBOSE_LUMBERJACK_DEPRECATION_WARNING` to "true".
22
+ #
23
+ # @param method [String] The name of the deprecated method.
24
+ # @param message [String] Optional message to include in the warning.
25
+ # @yield The block to execute after the warning.
26
+ def deprecated(method, message)
27
+ @deprecations_lock ||= Mutex.new
28
+ unless @deprecations&.include?(method)
29
+ @deprecations_lock.synchronize do
30
+ @deprecations ||= {}
31
+ unless @deprecations.include?(method)
32
+ trace = caller[3..-1]
33
+ unless ENV["VERBOSE_LUMBERJACK_DEPRECATION_WARNING"] == "true"
34
+ trace = [trace.first]
35
+ @deprecations[method] = true
36
+ end
37
+ message = "DEPRECATION WARNING: #{message} Called from #{trace.join("\n")}"
38
+ warn(message) unless ENV["LUMBERJACK_NO_DEPRECATION_WARNINGS"] == "true"
39
+ end
40
+ end
41
+ end
42
+
43
+ yield
44
+ end
45
+
46
+ # Get the hostname of the machine. The returned value will be in UTF-8 encoding.
47
+ #
48
+ # @return [String] The hostname of the machine.
49
+ def hostname
50
+ if @hostname.equal?(UNDEFINED)
51
+ @hostname = force_utf8(Socket.gethostname)
52
+ end
53
+ @hostname
54
+ end
55
+
56
+ # Set the hostname to a specific value. If this is not specified, it will use the system hostname.
57
+ #
58
+ # @param hostname [String]
59
+ # @return [void]
60
+ def hostname=(hostname)
61
+ @hostname = force_utf8(hostname)
62
+ end
63
+
64
+ # Generate a global process ID that includes the hostname and process ID.
65
+ #
66
+ # @return [String] The global process ID.
67
+ def global_pid
68
+ if hostname
69
+ "#{hostname}-#{Process.pid}"
70
+ else
71
+ Process.pid.to_s
72
+ end
73
+ end
74
+
75
+ # Generate a global thread ID that includes the global process ID and the thread name.
76
+ #
77
+ # @return [String] The global thread ID.
78
+ def global_thread_id
79
+ "#{global_pid}-#{thread_name}"
80
+ end
81
+
82
+ # Get the name of a thread. The value will be based on the thread's name if it exists.
83
+ # Otherwise a unique id is generated based on the thread's object id. Only alphanumeric
84
+ # characters, underscores, dashes, and periods are kept in thread name.
85
+ #
86
+ # @param thread [Thread] The thread to get the name for. Defaults to the current thread.
87
+ # @return [String] The name of the thread.
88
+ def thread_name(thread = Thread.current)
89
+ thread.name ? slugify(thread.name) : thread.object_id.to_s(36)
90
+ end
91
+
92
+ # Force encode a string to UTF-8. Any invalid byte sequences will be
93
+ # ignored and replaced with an empty string.
94
+ #
95
+ # @param str [String] The string to encode.
96
+ # @return [String] The UTF-8 encoded string.
97
+ def force_utf8(str)
98
+ return nil if str.nil?
99
+
100
+ str.dup.force_encoding("ASCII-8BIT").encode("UTF-8", invalid: :replace, undef: :replace, replace: "")
101
+ end
102
+
103
+ # Flatten a tag hash to a single level hash with dot notation for nested keys.
104
+ #
105
+ # @param tag_hash [Hash] The hash to flatten.
106
+ # @return [Hash] The flattened hash.
107
+ def flatten_tags(tag_hash)
108
+ return {} unless tag_hash.is_a?(Hash)
109
+
110
+ tag_hash.each_with_object({}) do |(key, value), result|
111
+ if value.is_a?(Hash)
112
+ value.each do |sub_key, sub_value|
113
+ result["#{key}.#{sub_key}"] = sub_value
114
+ end
115
+ else
116
+ result[key] = value
117
+ end
118
+ end
119
+ end
120
+
121
+ private
122
+
123
+ def slugify(str)
124
+ return nil if str.nil?
125
+
126
+ str = str.gsub(NON_SLUGGABLE_PATTERN, "-")
127
+ str.delete_prefix!("-")
128
+ str.chomp!("-")
129
+ str
130
+ end
131
+ end
132
+ end
133
+ end
data/lib/lumberjack.rb CHANGED
@@ -22,6 +22,7 @@ module Lumberjack
22
22
  require_relative "lumberjack/tagged_logging"
23
23
  require_relative "lumberjack/template"
24
24
  require_relative "lumberjack/rack"
25
+ require_relative "lumberjack/utils"
25
26
 
26
27
  class << self
27
28
  # Define a unit of work within a block. Within the block supplied to this
@@ -34,19 +35,23 @@ module Lumberjack
34
35
  # For the common use case of treating a single web request as a unit of work, see the
35
36
  # Lumberjack::Rack::UnitOfWork class.
36
37
  #
37
- # @param [String] id The id for the unit of work.
38
+ # @param id [String] The id for the unit of work.
38
39
  # @return [void]
40
+ # @deprecated Use tags instead. This will be removed in version 2.0.
39
41
  def unit_of_work(id = nil)
40
- id ||= SecureRandom.hex(6)
41
- context do
42
- context[:unit_of_work_id] = id
43
- yield
42
+ Lumberjack::Utils.deprecated("Lumberjack.unit_of_work", "Lumberjack.unit_of_work will be removed in version 2.0. Use Lumberjack::Logger#tag(unit_of_work: id) instead.") do
43
+ id ||= SecureRandom.hex(6)
44
+ context do
45
+ context[:unit_of_work_id] = id
46
+ yield
47
+ end
44
48
  end
45
49
  end
46
50
 
47
51
  # Get the UniqueIdentifier for the current unit of work.
48
52
  #
49
53
  # @return [String, nil] The id for the current unit of work.
54
+ # @deprecated Use tags instead. This will be removed in version 2.0.
50
55
  def unit_of_work_id
51
56
  context[:unit_of_work_id]
52
57
  end
data/lumberjack.gemspec CHANGED
@@ -4,7 +4,7 @@ Gem::Specification.new do |spec|
4
4
  spec.authors = ["Brian Durand"]
5
5
  spec.email = ["bbdurand@gmail.com"]
6
6
 
7
- spec.summary = "A simple, powerful, and very fast logging utility that can be a drop in replacement for Logger or ActiveSupport::BufferedLogger."
7
+ spec.summary = "A simple, powerful, and fast logging utility with excellent structured logging support that can be a drop in replacement for the standard library Logger."
8
8
  spec.homepage = "https://github.com/bdurand/lumberjack"
9
9
  spec.license = "MIT"
10
10
 
@@ -25,7 +25,7 @@ Gem::Specification.new do |spec|
25
25
 
26
26
  spec.require_paths = ["lib"]
27
27
 
28
- spec.required_ruby_version = ">= 2.3.0"
28
+ spec.required_ruby_version = ">= 2.5.0"
29
29
 
30
30
  spec.add_development_dependency "bundler"
31
31
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lumberjack
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.10
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Durand
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-13 00:00:00.000000000 Z
11
+ date: 2025-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -31,6 +31,7 @@ executables: []
31
31
  extensions: []
32
32
  extra_rdoc_files: []
33
33
  files:
34
+ - ARCHITECTURE.md
34
35
  - CHANGELOG.md
35
36
  - MIT_LICENSE.txt
36
37
  - README.md
@@ -50,11 +51,15 @@ files:
50
51
  - lib/lumberjack/formatter/exception_formatter.rb
51
52
  - lib/lumberjack/formatter/id_formatter.rb
52
53
  - lib/lumberjack/formatter/inspect_formatter.rb
54
+ - lib/lumberjack/formatter/multiply_formatter.rb
53
55
  - lib/lumberjack/formatter/object_formatter.rb
54
56
  - lib/lumberjack/formatter/pretty_print_formatter.rb
57
+ - lib/lumberjack/formatter/redact_formatter.rb
58
+ - lib/lumberjack/formatter/round_formatter.rb
55
59
  - lib/lumberjack/formatter/string_formatter.rb
56
60
  - lib/lumberjack/formatter/strip_formatter.rb
57
61
  - lib/lumberjack/formatter/structured_formatter.rb
62
+ - lib/lumberjack/formatter/tagged_message.rb
58
63
  - lib/lumberjack/formatter/truncate_formatter.rb
59
64
  - lib/lumberjack/log_entry.rb
60
65
  - lib/lumberjack/logger.rb
@@ -68,6 +73,7 @@ files:
68
73
  - lib/lumberjack/tagged_logging.rb
69
74
  - lib/lumberjack/tags.rb
70
75
  - lib/lumberjack/template.rb
76
+ - lib/lumberjack/utils.rb
71
77
  - lumberjack.gemspec
72
78
  homepage: https://github.com/bdurand/lumberjack
73
79
  licenses:
@@ -81,16 +87,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
81
87
  requirements:
82
88
  - - ">="
83
89
  - !ruby/object:Gem::Version
84
- version: 2.3.0
90
+ version: 2.5.0
85
91
  required_rubygems_version: !ruby/object:Gem::Requirement
86
92
  requirements:
87
93
  - - ">="
88
94
  - !ruby/object:Gem::Version
89
95
  version: '0'
90
96
  requirements: []
91
- rubygems_version: 3.4.20
97
+ rubygems_version: 3.4.10
92
98
  signing_key:
93
99
  specification_version: 4
94
- summary: A simple, powerful, and very fast logging utility that can be a drop in replacement
95
- for Logger or ActiveSupport::BufferedLogger.
100
+ summary: A simple, powerful, and fast logging utility with excellent structured logging
101
+ support that can be a drop in replacement for the standard library Logger.
96
102
  test_files: []