Class: Kscript::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/kscript/logger.rb

Overview

Structured logger for all scripts

Constant Summary collapse

LEVELS =
i[debug info warn error fatal unknown].freeze
COLORS =

Colorful output

{
  info: "\e[32m", # green
  warn: "\e[33m", # yellow
  error: "\e[31m", # red
  debug: "\e[90m", # gray
  fatal: "\e[35m", # magenta
  unknown: "\e[36m", # cyan
  reset: "\e[0m"
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(service: 'kscript', level: :info, out: $stdout, human_output: nil) ⇒ Logger

Returns a new instance of Logger.



19
20
21
22
23
24
25
26
# File 'lib/kscript/logger.rb', line 19

def initialize(service: 'kscript', level: :info, out: $stdout, human_output: nil)
  require 'json'
  require 'time'
  @service = service
  @logger = ::Logger.new(out)
  @logger.level = ::Logger.const_get(level.to_s.upcase)
  @human_output = human_output
end

Instance Method Details

#human_output?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/kscript/logger.rb', line 33

def human_output?
  @human_output == true
end

#kdebug(msg, ctx = {}) ⇒ Object



93
94
95
# File 'lib/kscript/logger.rb', line 93

def kdebug(msg, ctx = {})
  klog(:debug, msg, ctx)
end

#kerror(msg, ctx = {}) ⇒ Object



89
90
91
# File 'lib/kscript/logger.rb', line 89

def kerror(msg, ctx = {})
  klog(:error, msg, ctx)
end

#kinfo(msg, ctx = {}) ⇒ Object

便捷方法(info/warn/error/debug)



81
82
83
# File 'lib/kscript/logger.rb', line 81

def kinfo(msg, ctx = {})
  klog(:info, msg, ctx)
end

#klog(level, message, context = {}) ⇒ Object

终端输出(带颜色、trace、时间等)



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/kscript/logger.rb', line 64

def klog(level, message, context = {})
  if human_output?
    puts "[#{level.to_s.upcase}] #{message} #{context.map { |k, v| "#{k}=#{v}" }.join(' ')}".strip
  else
    ts = Time.now.strftime('%Y-%m-%d %H:%M:%S')
    lvl = level.to_s.upcase
    svc = @service || 'kscript'
    trace = context[:trace_id] || (respond_to?(:default_trace_id) ? default_trace_id : nil) || '-'
    color = COLORS[level] || COLORS[:info]
    ctx_str = context.map { |k, v| "#{k}=#{v}" }.join(' ')
    line = "[#{ts}] [#{lvl}] [#{svc}] [#{trace}] #{message}"
    line += " | #{ctx_str}" unless ctx_str.empty?
    $stdout.puts "#{color}#{line}#{COLORS[:reset]}"
  end
end

#kwarn(msg, ctx = {}) ⇒ Object



85
86
87
# File 'lib/kscript/logger.rb', line 85

def kwarn(msg, ctx = {})
  klog(:warn, msg, ctx)
end

#log(level, message, context = {}) ⇒ Object

结构化日志输出



52
53
54
55
56
57
58
59
60
61
# File 'lib/kscript/logger.rb', line 52

def log(level, message, context = {})
  entry = {
    timestamp: Time.now.utc.iso8601,
    level: level.to_s.upcase,
    service: @service,
    message: message,
    context: context
  }
  @logger.send(level, entry.to_json)
end

#log_mode?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/kscript/logger.rb', line 37

def log_mode?
  @human_output == false
end

#set_human_output(val) ⇒ Object

设置人类可读输出模式



29
30
31
# File 'lib/kscript/logger.rb', line 29

def set_human_output(val)
  @human_output = val
end