Class: Sinlog::Logger

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/sinlog/02_logger.rb

Overview

Logger Singleton Class

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeself

Since this is a Singleton class, you should use Sinlog.instance instead of .new.

Examples:


instance = Sinlog::Logger.instance
instance.logger.info "Hello"


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

#loggerObject (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

Note:

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.

Parameters:

  • level (Integer, String, Symbol, nil) (defaults to: nil)

    Log Level.

  • env_name (#to_s) (defaults to: nil)

    Name of the environment variable.

Returns:

See Also:



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).

Examples:


ENV["XX_LOG"] = "info" # or setenv in posix-sh: export XX_LOG=info

level = Sinlog.instance.set_level_from_env!("XX_LOG")
level == Sinlog::LV[:info] # => true

log = Sinlog.logger
log.debug "This message will not be displayed because the current log level is info"
log.info "Hello!"

Parameters:

  • env_name (#to_s) (defaults to: 'RUBY_LOG')

    Name of the environment variable.

Returns:

  • (Integer)

    @logger.level



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