Module: Lumberjack::Utils
- Defined in:
- lib/lumberjack/utils.rb
Class Method Summary collapse
-
.deprecated(method, message) { ... } ⇒ Object
Print warning when deprecated methods are called the first time.
-
.expand_tags(tags) ⇒ Hash
Expand a hash of tags that may contain nested hashes or dot notation keys.
-
.flatten_tags(tag_hash) ⇒ Hash<String, Object>
Flatten a tag hash to a single level hash with dot notation for nested keys.
-
.force_utf8(str) ⇒ String
Force encode a string to UTF-8.
-
.global_pid ⇒ String
Generate a global process ID that includes the hostname and process ID.
-
.global_thread_id ⇒ String
Generate a global thread ID that includes the global process ID and the thread name.
-
.hostname ⇒ String
Get the hostname of the machine.
-
.hostname=(hostname) ⇒ void
Set the hostname to a specific value.
-
.thread_name(thread = Thread.current) ⇒ String
Get the name of a thread.
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”.
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, ) @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 = "DEPRECATION WARNING: #{message} Called from #{trace.join("\n")}" warn() 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.
125 126 127 128 129 |
# File 'lib/lumberjack/utils.rb', line 125 def () return {} unless .is_a?(Hash) () end |
.flatten_tags(tag_hash) ⇒ Hash<String, Object>
Flatten a tag hash to a single level hash with dot notation for nested keys.
110 111 112 113 114 |
# File 'lib/lumberjack/utils.rb', line 110 def (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.
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_pid ⇒ String
Generate a global process ID that includes the hostname and 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_id ⇒ String
Generate a global thread ID that includes the global process ID and the thread name.
78 79 80 |
# File 'lib/lumberjack/utils.rb', line 78 def global_thread_id "#{global_pid}-#{thread_name}" end |
.hostname ⇒ String
Get the hostname of the machine. The returned value will be in UTF-8 encoding.
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.
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.
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 |