Module: Lumberjack::Utils

Defined in:
lib/lumberjack/utils.rb

Class Method Summary collapse

Class Method Details

.deprecated(method, message) { ... } ⇒ Object

Print warning when deprecated methods are called the first time. This can be disabled by setting the environment variable ‘LUMBERJACK_NO_DEPRECATION_WARNINGS` to “true”. You can see every usage of a deprecated method along with a full stack trace by setting the environment variable `VERBOSE_LUMBERJACK_DEPRECATION_WARNING` to “true”.

Parameters:

  • method (String)

    The name of the deprecated method.

  • message (String)

    Optional message to include in the warning.

Yields:

  • The block to execute after the warning.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/lumberjack/utils.rb', line 26

def deprecated(method, message)
  @deprecations_lock ||= Mutex.new
  unless @deprecations&.include?(method)
    @deprecations_lock.synchronize do
      @deprecations ||= {}
      unless @deprecations.include?(method)
        trace = caller[3..-1]
        unless ENV["VERBOSE_LUMBERJACK_DEPRECATION_WARNING"] == "true"
          trace = [trace.first]
          @deprecations[method] = true
        end
        message = "DEPRECATION WARNING: #{message} Called from #{trace.join("\n")}"
        warn(message) unless ENV["LUMBERJACK_NO_DEPRECATION_WARNINGS"] == "true"
      end
    end
  end

  yield
end

.expand_tags(tags) ⇒ Hash

Expand a hash of tags that may contain nested hashes or dot notation keys. Dot notation tags will be expanded into nested hashes.

Examples:

expand_tags({"user.id" => 123, "user.name" => "Alice", "action" => "login"})
# => {"user" => {"id" => 123, "name" => "Alice"}, "action" => "login"}

Parameters:

  • tags (Hash)

    The hash of tags to expand.

Returns:

  • (Hash)

    The expanded hash with dot notation keys.



125
126
127
128
129
# File 'lib/lumberjack/utils.rb', line 125

def expand_tags(tags)
  return {} unless tags.is_a?(Hash)

  expand_dot_notation_hash(tags)
end

.flatten_tags(tag_hash) ⇒ Hash<String, Object>

Flatten a tag hash to a single level hash with dot notation for nested keys.

Examples:

expand_tags(user: {id: 123, name: "Alice"}, action: "login")})
# => {"user.id" => 123, "user.name" => "Alice", "action" => "login"}

Parameters:

  • tag_hash (Hash)

    The hash to flatten.

Returns:

  • (Hash<String, Object>)

    The flattened hash.



110
111
112
113
114
# File 'lib/lumberjack/utils.rb', line 110

def flatten_tags(tag_hash)
  return {} unless tag_hash.is_a?(Hash)

  flatten_hash_recursive(tag_hash)
end

.force_utf8(str) ⇒ String

Force encode a string to UTF-8. Any invalid byte sequences will be ignored and replaced with an empty string.

Parameters:

  • str (String)

    The string to encode.

Returns:

  • (String)

    The UTF-8 encoded string.



97
98
99
100
101
# File 'lib/lumberjack/utils.rb', line 97

def force_utf8(str)
  return nil if str.nil?

  str.dup.force_encoding("ASCII-8BIT").encode("UTF-8", invalid: :replace, undef: :replace, replace: "")
end

.global_pidString

Generate a global process ID that includes the hostname and process ID.

Returns:

  • (String)

    The global process ID.



67
68
69
70
71
72
73
# File 'lib/lumberjack/utils.rb', line 67

def global_pid
  if hostname
    "#{hostname}-#{Process.pid}"
  else
    Process.pid.to_s
  end
end

.global_thread_idString

Generate a global thread ID that includes the global process ID and the thread name.

Returns:

  • (String)

    The global thread ID.



78
79
80
# File 'lib/lumberjack/utils.rb', line 78

def global_thread_id
  "#{global_pid}-#{thread_name}"
end

.hostnameString

Get the hostname of the machine. The returned value will be in UTF-8 encoding.

Returns:

  • (String)

    The hostname of the machine.



49
50
51
52
53
54
# File 'lib/lumberjack/utils.rb', line 49

def hostname
  if @hostname.equal?(UNDEFINED)
    @hostname = force_utf8(Socket.gethostname)
  end
  @hostname
end

.hostname=(hostname) ⇒ void

This method returns an undefined value.

Set the hostname to a specific value. If this is not specified, it will use the system hostname.

Parameters:

  • hostname (String)


60
61
62
# File 'lib/lumberjack/utils.rb', line 60

def hostname=(hostname)
  @hostname = force_utf8(hostname)
end

.thread_name(thread = Thread.current) ⇒ String

Get the name of a thread. The value will be based on the thread’s name if it exists. Otherwise a unique id is generated based on the thread’s object id. Only alphanumeric characters, underscores, dashes, and periods are kept in thread name.

Parameters:

  • thread (Thread) (defaults to: Thread.current)

    The thread to get the name for. Defaults to the current thread.

Returns:

  • (String)

    The name of the thread.



88
89
90
# File 'lib/lumberjack/utils.rb', line 88

def thread_name(thread = Thread.current)
  thread.name ? slugify(thread.name) : thread.object_id.to_s(36)
end