lib/rage/logger/logger.rb in rage-rb-1.0.0 vs lib/rage/logger/logger.rb in rage-rb-1.1.0
- old
+ new
@@ -31,10 +31,27 @@
# `Rage::Logger` also implements the interface of Ruby's native {https://ruby-doc.org/3.2.2/stdlibs/logger/Logger.html Logger}:
# ```ruby
# 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:
+# ```ruby
+# 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:
+# ```ruby
+# 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
+# ```
class Rage::Logger
METHODS_MAP = {
"debug" => Logger::DEBUG,
"info" => Logger::INFO,
"warn" => Logger::WARN,
@@ -125,16 +142,9 @@
if @logdev.nil? || level_val < @level
# logging is disabled or the log level is higher than the current one
<<-RUBY
def #{level_name}(msg = nil)
false
- end
- RUBY
- elsif (Rage.config.internal.rails_mode ? Rage.config.internal.rails_console : defined?(IRB))
- # the call was made from the console - don't use the formatter
- <<-RUBY
- def #{level_name}(msg = nil)
- @logdev.write((msg || yield) + "\n")
end
RUBY
elsif @formatter.class.name.start_with?("Rage::")
# the call was made from within the application and a built-in formatter is used;
# in such case we use the `gen_timestamp` method which is much faster than `Time.now.strftime`;