Class: Rage::Logger
- Inherits:
-
Object
- Object
- Rage::Logger
- Defined in:
- lib/rage/logger/logger.rb
Overview
All logs in rage
consist of two parts: keys and tags. A sample log entry might look like this:
[fecbba0735355738] timestamp=2023-10-19T11:12:56+00:00 pid=1825 level=info message=hello
In the log entry above, timestamp
, pid
, level
, and message
are keys, while fecbba0735355738
is a tag.
Use #tagged to add custom tags to an entry:
Rage.logger.tagged("ApiCall") do
perform_api_call
Rage.logger.info "success"
end
# => [fecbba0735355738][ApiCall] timestamp=2023-10-19T11:12:56+00:00 pid=1825 level=info message=success
#with_context can be used to add custom keys:
cache_key = "mykey"
Rage.logger.with_context(cache_key: cache_key) do
get_from_cache(cache_key)
Rage.logger.info "cache miss"
end
# => [fecbba0735355738] timestamp=2023-10-19T11:12:56+00:00 pid=1825 level=info cache_key=mykey message=cache miss
Rage::Logger
also implements the interface of Ruby’s native Logger:
Rage.logger.info("Initializing")
Rage.logger.debug { "This is a " + potentially + " expensive operation" }
Using the logger
The recommended approach to logging with Rage is to make sure your code always logs the same message no matter what the input is. You can achieve this by using the #with_context and #tagged methods. So, a code like this:
def process_purchase(user_id:, product_id:)
Rage.logger.info "processing purchase with user_id = #{user_id}; product_id = #{product_id}"
end
turns into this:
def process_purchase(user_id:, product_id:)
Rage.logger.with_context(user_id: user_id, product_id: product_id) do
Rage.logger.info "processing purchase"
end
end
Instance Attribute Summary collapse
-
#formatter ⇒ Object
Returns the value of attribute formatter.
-
#level ⇒ Object
Returns the value of attribute level.
Instance Method Summary collapse
- #debug? ⇒ Boolean
- #error? ⇒ Boolean
- #fatal? ⇒ Boolean
- #info? ⇒ Boolean
-
#initialize(log, level: Logger::DEBUG, formatter: Rage::TextFormatter.new, shift_age: 0, shift_size: 104857600, shift_period_suffix: "%Y%m%d", binmode: false) ⇒ Logger
constructor
Create a new logger.
-
#tagged(tag) ⇒ Object
(also: #with_tag)
Add a custom tag to an entry.
- #warn? ⇒ Boolean
-
#with_context(context) ⇒ Object
Add custom keys to an entry.
Constructor Details
#initialize(log, level: Logger::DEBUG, formatter: Rage::TextFormatter.new, shift_age: 0, shift_size: 104857600, shift_period_suffix: "%Y%m%d", binmode: false) ⇒ Logger
Create a new logger.
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/rage/logger/logger.rb', line 75 def initialize(log, level: Logger::DEBUG, formatter: Rage::TextFormatter.new, shift_age: 0, shift_size: 104857600, shift_period_suffix: "%Y%m%d", binmode: false) @logdev = if log && log != File::NULL Logger::LogDevice.new(log, shift_age:, shift_size:, shift_period_suffix:, binmode:) end if Rage.env.development? && log.respond_to?(:sync=) log.sync = true end @formatter = formatter @level = @logdev ? level : Logger::UNKNOWN define_log_methods end |
Instance Attribute Details
#formatter ⇒ Object
Returns the value of attribute formatter.
64 65 66 |
# File 'lib/rage/logger/logger.rb', line 64 def formatter @formatter end |
#level ⇒ Object
Returns the value of attribute level.
64 65 66 |
# File 'lib/rage/logger/logger.rb', line 64 def level @level end |
Instance Method Details
#debug? ⇒ Boolean
136 |
# File 'lib/rage/logger/logger.rb', line 136 def debug? = @level <= Logger::DEBUG |
#error? ⇒ Boolean
137 |
# File 'lib/rage/logger/logger.rb', line 137 def error? = @level <= Logger::ERROR |
#fatal? ⇒ Boolean
138 |
# File 'lib/rage/logger/logger.rb', line 138 def fatal? = @level <= Logger::FATAL |
#info? ⇒ Boolean
139 |
# File 'lib/rage/logger/logger.rb', line 139 def info? = @level <= Logger::INFO |
#tagged(tag) ⇒ Object Also known as: with_tag
Add a custom tag to an entry.
127 128 129 130 131 132 |
# File 'lib/rage/logger/logger.rb', line 127 def tagged(tag) (Thread.current[:rage_logger] ||= { tags: [], context: {} })[:tags] << tag yield(self) ensure Thread.current[:rage_logger][:tags].pop end |
#warn? ⇒ Boolean
140 |
# File 'lib/rage/logger/logger.rb', line 140 def warn? = @level <= Logger::WARN |
#with_context(context) ⇒ Object
Add custom keys to an entry.
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/rage/logger/logger.rb', line 106 def with_context(context) old_context = (Thread.current[:rage_logger] ||= { tags: [], context: {} })[:context] if old_context.empty? # there's nothing in the context yet Thread.current[:rage_logger][:context] = context else # it's not the first `with_context` call in the chain Thread.current[:rage_logger][:context] = old_context.merge(context) end yield(self) ensure Thread.current[:rage_logger][:context] = old_context end |