Class: Sinlog::Logger
- Inherits:
-
Object
- Object
- Sinlog::Logger
- Includes:
- Singleton
- Defined in:
- lib/sinlog/02_logger.rb
Overview
Logger Singleton Class
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Class Method Summary collapse
-
.logger(level = nil, env_name = nil) ⇒ StdLogger
Configures and returns the StdLogger.
Instance Method Summary collapse
-
#initialize ⇒ self
constructor
Since this is a Singleton class, you should use instance instead of
.new. -
#set_level_from_env!(env_name = 'RUBY_LOG') ⇒ Integer
Sets the
@logger.level(log level) based on the value of an environment variable.
Constructor Details
#initialize ⇒ self
Since this is a Singleton class, you should use Sinlog.instance instead of .new.
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/sinlog/02_logger.rb', line 19 def initialize @logger = StdLogger.new($stderr) set_level_from_env! @logger.formatter = Kernel.proc do |severity, datetime, progname, msg| color = COLORS[severity.downcase.to_sym] reset = COLORS[:unknown] formatted_datetime = datetime.strftime('%H:%M:%S.%L') prog = format_prog_name(progname) "[#{color}#{severity}#{reset}] #{formatted_datetime} #{prog}#{msg}\n" end end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
9 10 11 |
# File 'lib/sinlog/02_logger.rb', line 9 def logger @logger end |
Class Method Details
.logger(level = nil, env_name = nil) ⇒ StdLogger
Similar to Sinlog.logger, but uses different parameter types.
- Sinlog.logger: uses keyword arguments, e.g., (level: "info", env_name: "RUBY_LOG")
- This function: uses positional arguments, e.g., ("warn", "CUSTOM_LOG")
Configures and returns the StdLogger.
Example
log = Sinlog::Logger.logger("debug")
log.info "Information"
log.debug "This is a debug message"
The log output format will be similar to:
[INFO] 21:29:22.004 Information
[DEBUG] 21:29:22.005 This is a debug message
Where "INFO" is highlighted in cyan and "DEBUG" is highlighted in blue.
The default log level is set based on the RUBY_LOG environment variable.
If this variable is not set, the default level is DEBUG.
63 64 65 |
# File 'lib/sinlog/02_logger.rb', line 63 def self.logger(level = nil, env_name = nil) Sinlog.logger(level:, env_name:) end |
Instance Method Details
#set_level_from_env!(env_name = 'RUBY_LOG') ⇒ Integer
Sets the @logger.level (log level) based on the value of an environment variable.
If env_name is not specified, it reads the value of the RUBY_LOG environment variable.
- If the value exists, it is converted to lowercase, then to a symbol, and looked up in the LV hash;
- If it does not exist, the default level is
DEBUG(0); - If the lookup result is invalid, the level is set to
ERROR(3); - If the environment variable value is empty, the lookup result will be invalid,
and the level will be set to
ERROR(3).
90 91 92 93 94 95 96 |
# File 'lib/sinlog/02_logger.rb', line 90 def set_level_from_env!(env_name = 'RUBY_LOG') env_str = ENV[env_name.to_s]&.downcase || 'debug' Sinlog .as_log_level(env_str) .tap { @logger.level = _1 } end |