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– DEBUGinfo– INFOwarn– WARNerr– ERRORfatal– FATALunk– 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
-
.as_log_level(level = nil) ⇒ Integer
The log level as an integer.
- .dbg(obj) ⇒ Boolean
- .err(obj) ⇒ Boolean
- .fatal(obj) ⇒ Boolean
- .info(obj) ⇒ Boolean
-
.instance ⇒ <Sinlog::Logger>
=> Sinlog::Logger.instance. -
.logger(level: nil, env_name: nil) ⇒ StdLogger
Configures and returns the
Sinlog.instance.logger. - .unk(obj) ⇒ Boolean
- .warn(obj) ⇒ Boolean
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.
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
36 37 38 |
# File 'lib/sinlog/08_module_short_ext.rb', line 36 def dbg(obj) obj.log_dbg end |
.err(obj) ⇒ Boolean
66 67 68 |
# File 'lib/sinlog/08_module_short_ext.rb', line 66 def err(obj) obj.log_err end |
.fatal(obj) ⇒ Boolean
76 77 78 |
# File 'lib/sinlog/08_module_short_ext.rb', line 76 def fatal(obj) obj.log_fatal end |
.info(obj) ⇒ 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.
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_LOGand 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.
- If the user runs
- 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.
- If the user runs
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。
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
86 87 88 |
# File 'lib/sinlog/08_module_short_ext.rb', line 86 def unk(obj) obj.log_unk end |
.warn(obj) ⇒ Boolean
56 57 58 |
# File 'lib/sinlog/08_module_short_ext.rb', line 56 def warn(obj) obj.log_warn end |