Class: DiscordRDA::Logger
- Inherits:
-
Object
- Object
- DiscordRDA::Logger
- Defined in:
- lib/discord_rda/core/logger.rb
Overview
Structured logging for DiscordRDA. Supports both simple and structured (JSON) log formats.
Defined Under Namespace
Classes: ContextualLogger
Constant Summary collapse
- LEVELS =
Log levels
{ debug: 0, info: 1, warn: 2, error: 3, fatal: 4 }.freeze
Instance Attribute Summary collapse
-
#format ⇒ Symbol
readonly
Log format (:simple or :structured).
-
#level ⇒ Symbol
readonly
Current log level.
-
#output ⇒ IO
readonly
Output destination.
Instance Method Summary collapse
-
#debug(message, **context) ⇒ Object
Log a debug message.
-
#error(message, error: nil, **context) ⇒ Object
Log an error message.
-
#fatal(message, error: nil, **context) ⇒ Object
Log a fatal message.
-
#info(message, **context) ⇒ Object
Log an info message.
-
#initialize(level: :info, format: :structured, output: STDOUT, file_path: nil, rotate_age: 7, rotate_size: 10_485_760) ⇒ Logger
constructor
Create a new logger.
-
#level_enabled?(level) ⇒ Boolean
Check if a level is enabled.
-
#warn(message, **context) ⇒ Object
Log a warning message.
-
#with_context(**context) ⇒ ContextualLogger
Create a child logger with additional context.
Constructor Details
#initialize(level: :info, format: :structured, output: STDOUT, file_path: nil, rotate_age: 7, rotate_size: 10_485_760) ⇒ Logger
Create a new logger
39 40 41 42 43 44 |
# File 'lib/discord_rda/core/logger.rb', line 39 def initialize(level: :info, format: :structured, output: STDOUT, file_path: nil, rotate_age: 7, rotate_size: 10_485_760) @level = level.to_sym @format = format.to_sym @output = build_output(output, file_path, rotate_age, rotate_size) @mutex = Mutex.new end |
Instance Attribute Details
#format ⇒ Symbol (readonly)
Returns Log format (:simple or :structured).
30 31 32 |
# File 'lib/discord_rda/core/logger.rb', line 30 def format @format end |
#level ⇒ Symbol (readonly)
Returns Current log level.
27 28 29 |
# File 'lib/discord_rda/core/logger.rb', line 27 def level @level end |
#output ⇒ IO (readonly)
Returns Output destination.
33 34 35 |
# File 'lib/discord_rda/core/logger.rb', line 33 def output @output end |
Instance Method Details
#debug(message, **context) ⇒ Object
Log a debug message
49 50 51 |
# File 'lib/discord_rda/core/logger.rb', line 49 def debug(, **context) log(:debug, , context) end |
#error(message, error: nil, **context) ⇒ Object
Log an error message
71 72 73 74 75 76 77 |
# File 'lib/discord_rda/core/logger.rb', line 71 def error(, error: nil, **context) context = context.dup context[:error_class] = error.class.name if error context[:error_message] = error. if error context[:backtrace] = error.backtrace.first(5) if error&.backtrace log(:error, , context) end |
#fatal(message, error: nil, **context) ⇒ Object
Log a fatal message
83 84 85 86 87 88 |
# File 'lib/discord_rda/core/logger.rb', line 83 def fatal(, error: nil, **context) context = context.dup context[:error_class] = error.class.name if error context[:error_message] = error. if error log(:fatal, , context) end |
#info(message, **context) ⇒ Object
Log an info message
56 57 58 |
# File 'lib/discord_rda/core/logger.rb', line 56 def info(, **context) log(:info, , context) end |
#level_enabled?(level) ⇒ Boolean
Check if a level is enabled
93 94 95 |
# File 'lib/discord_rda/core/logger.rb', line 93 def level_enabled?(level) LEVELS[level.to_sym] >= LEVELS[@level] end |
#warn(message, **context) ⇒ Object
Log a warning message
63 64 65 |
# File 'lib/discord_rda/core/logger.rb', line 63 def warn(, **context) log(:warn, , context) end |
#with_context(**context) ⇒ ContextualLogger
Create a child logger with additional context
100 101 102 |
# File 'lib/discord_rda/core/logger.rb', line 100 def with_context(**context) ContextualLogger.new(self, context) end |