Module: Sinlog

Defined in:
lib/sinlog.rb,
lib/sinlog/version.rb,
lib/sinlog/01_consts.rb,
lib/sinlog/02_logger.rb,
lib/sinlog/04_log_ext.rb,
lib/sinlog/06_loggable.rb,
lib/sinlog/03_module_fn.rb,
lib/sinlog/05_short_ext.rb,
lib/sinlog/08_module_short_ext.rb

Overview

Provides a set of reusable unary functions that call logging methods.

  • dbg – DEBUG
  • info – INFO
  • warn – WARN
  • err – ERROR
  • fatal – FATAL
  • unk – UNKNOWN

Examples:


Sinlog.dbg 'debug'
Sinlog.info 'information'
Sinlog.warn 'warning'
Sinlog.err 'error'
Sinlog.fatal 'fatal'
Sinlog.unk 'unknown'

Defined Under Namespace

Modules: Mixin, Proc, Refin, ShortMixin, ShortRefin Classes: Logger

Constant Summary collapse

VERSION =
'0.0.7'
StdLogger =
::Logger
COLORS =

Define colors for different log levels

{
  debug: "\e[34m",   # Blue
  info: "\e[36m",    # Cyan
  warn: "\e[33m",    # Yellow
  error: "\e[31m",   # Red
  fatal: "\e[35m",   # Magenta
  unknown: "\e[0m", # Reset
}.freeze
LV =

log levels

{
  debug: StdLogger::DEBUG,
  info: StdLogger::INFO,
  warn: StdLogger::WARN,
  error: StdLogger::ERROR,
  fatal: StdLogger::FATAL,
  unknown: StdLogger::UNKNOWN,
}.freeze

Class Method Summary collapse

Class Method Details

.as_log_level(level = nil) ⇒ Integer

Note:

If level is nil, returns Sinlog.logger.level.

Returns the log level as an integer.

Examples:


require 'sinlog'

# optional values:
#    'debug', 'info', 'warn', 'error', 'fatal', 'unknown',
#    'dbg', 'information', 'warning', 'err', 'unk',
#    '0', '1', '2', '3', '4', '5'

Sinlog.as_log_level(:dbg) #=> LV[:debug] => 0
Sinlog.as_log_level('info') #=> LV[:info] => 1

Parameters:

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

    Log Level.

Returns:

  • (Integer)

    the log level as an integer.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sinlog/03_module_fn.rb', line 23

def as_log_level(level = nil) # rubocop:disable Metrics/CyclomaticComplexity,Metrics/MethodLength
  level = Sinlog.logger.level if level.nil?
  case level
    when 0..5
      level
    when '0', '1', '2', '3', '4', '5'
      level.to_i
    when 'debug', 'dbg', :dbg
      LV[:debug]
    when 'info', 'information'
      LV[:info]
    when 'warn', 'warning'
      LV[:warn]
    when 'err', 'error', :err
      LV[:error]
    when 'fatal'
      LV[:fatal]
    when 'unk', 'unknown', :unk
      LV[:unknown]
    when Symbol
      LV.fetch(level, LV[:error])
    else
      LV[:error]
  end
end

.dbg(obj) ⇒ Boolean

Examples:

Basic usage


Sinlog.dbg "This is a debug message"

Parameters:

  • obj (Object)

Returns:

  • (Boolean)


36
37
38
# File 'lib/sinlog/08_module_short_ext.rb', line 36

def dbg(obj)
  obj.log_dbg
end

.err(obj) ⇒ Boolean

Examples:


Sinlog.err "Error message"

Parameters:

  • obj (Object)

Returns:

  • (Boolean)


66
67
68
# File 'lib/sinlog/08_module_short_ext.rb', line 66

def err(obj)
  obj.log_err
end

.fatal(obj) ⇒ Boolean

Examples:


Sinlog.fatal "Fatal Error message"

Parameters:

  • obj (Object)

Returns:

  • (Boolean)


76
77
78
# File 'lib/sinlog/08_module_short_ext.rb', line 76

def fatal(obj)
  obj.log_fatal
end

.info(obj) ⇒ Boolean

Examples:


Sinlog.info "info message"

Parameters:

  • obj (Object)

Returns:

  • (Boolean)


46
47
48
# File 'lib/sinlog/08_module_short_ext.rb', line 46

def info(obj)
  obj.log_info
end

.instance<Sinlog::Logger>

Returns => Sinlog::Logger.instance.

Returns:



50
51
52
# File 'lib/sinlog/03_module_fn.rb', line 50

def instance
  Sinlog::Logger.instance
end

.logger(level: nil, env_name: nil) ⇒ StdLogger

Configures and returns the Sinlog.instance.logger.

English

You can configure the log level through parameters.

  • env_name

    • Specifies the name of an environment variable.
    • If set to nil, the program will attempt to read RUBY_LOG and set the log level accordingly.
    • Example: logger(env_name: nil)
      • If the user runs RUBY_LOG=debug ./[your-script].rb
      • The log level will be set to debug.
    • Example: logger(env_name: "XX_CLI_LOG")
      • If the user runs XX_CLI_LOG=warn ./[your-script].rb
      • The log level will be set to warn.
  • level

    • The level parameter takes precedence over env_name.
    • If both level and env_name are provided, the program will parse level first and return early.

中文

配置并获取 Sinlog.instance.logger

我们可以通过参数来配置日志级别。

  • env_name
    • 指定特定的环境变量名称
    • 当其为 nil 时,程序默认会尝试获取 RUBY_LOG 的值,并设置日志级别。
    • 假设 logger(env_name: nil)
      • 若用户调用 RUBY_LOG=debug ./[your-script].rb
      • 则日志级别为 debug
    • 假设 logger(env_name: "XX_CLI_LOG")
      • 若用户调用 XX_CLI_LOG=warn ./[your-script].rb
      • 则日志级别为 warn
  • level
    • level 的优先级要高于 env_name
    • 若 level 和 env_name 都不为 nil, 则程序会优先解析 level,并提前 return。

Examples:


require 'sinlog'

# optional level values:
#   - "debug"
#   - "dbg"
#   - "info"
#   - "warn"
#   - "error"
#   - "err"
#   - "fatal"
#   - "unknown"
#   - "unk"
#   - Integer (e.g., Sinlog::LV[:debug])

log = Sinlog.logger(level: "dbg")
log.level == Sinlog::LV[:debug]  #=> true
a = "Foo"
log.debug "a=#{a}"

using Sinlog::Refin
Sinlog.logger(level: 'warn')
log.level == Sinlog::LV[:warn]  #=> true
"Bar".log_warn

ENV["CUSTOM_LOG"] = 'info'
log = Sinlog.logger(env_name: "CUSTOM_LOG")
log.level == Sinlog::LV[:info]  #=> true
"Baz".log_info

log = Sinlog.logger(level: "error", env_name: "CUSTOM_LOG")
log.level == Sinlog::LV[:error]  #=> true
"foobar".log_err

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:



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/sinlog/03_module_fn.rb', line 135

def logger(level: nil, env_name: nil)
  std_logger = instance.logger

  # if level != nil
  return std_logger.tap { _1.level = as_log_level(level) } unless level.nil?

  # if env_name != nil
  instance.set_level_from_env!(env_name) unless env_name.nil?

  std_logger
end

.unk(obj) ⇒ Boolean

Examples:


Sinlog.unk "Unknown Level"

Parameters:

  • obj (Object)

Returns:

  • (Boolean)


86
87
88
# File 'lib/sinlog/08_module_short_ext.rb', line 86

def unk(obj)
  obj.log_unk
end

.warn(obj) ⇒ Boolean

Examples:


Sinlog.warn "warning message"

Parameters:

  • obj (Object)

Returns:

  • (Boolean)


56
57
58
# File 'lib/sinlog/08_module_short_ext.rb', line 56

def warn(obj)
  obj.log_warn
end