Module: Fatty::Logger

Defined in:
lib/fatty/logger.rb,
lib/fatty/log_formats/json.rb,
lib/fatty/log_formats/text.rb

Defined Under Namespace

Classes: JsonFormatter, TextFormatter

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.loggerObject

Returns the value of attribute logger.



14
15
16
# File 'lib/fatty/logger.rb', line 14

def logger
  @logger
end

.pathObject

Returns the value of attribute path.



14
15
16
# File 'lib/fatty/logger.rb', line 14

def path
  @path
end

Class Method Details

.active_tagsObject



52
53
54
55
# File 'lib/fatty/logger.rb', line 52

def self.active_tags
  tags = Fatty::Config.config.dig(:log, :tags) || [:all]
  Array(tags).map(&:to_sym)
end

.configureObject



17
18
19
20
21
22
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
48
49
50
# File 'lib/fatty/logger.rb', line 17

def self.configure
  progname = Fatty::Config.progname
  cfg = Fatty::Config.config || 'fatty'
  path =
    if cfg.dig(:log, :file)
      File.expand_path(cfg.dig(:log, :file))
    elsif ENV['XDG_STATE_HOME']
      File.expand_path(File.join(ENV['XDG_STATE_HOME'], progname, "#{progname}.log"))
    else
      File.expand_path(File.join("~/.state/#{progname}", "#{progname}.log"))
    end
  dir = File.dirname(path)
  FileUtils.mkdir_p(dir)
  FileUtils.touch(path)
  unless File.readable?(path) && File.writable?(path)
    return self.logger = nil
  end

  io = File.open(path, "a")
  io.sync = true
  self.logger = ::Logger.new(io)
  self.path = path
  logger.level = severity(cfg.dig(:log, :level))
  logger.formatter =
    if cfg.dig(:log, :format).nil?
      JsonFormatter.new
    elsif cfg.dig(:log, :format)&.to_sym == :json
      JsonFormatter.new
    else
      TextFormatter.new
    end
  logger.progname = progname
  logger
end

.log(event = nil, level: :debug, tag: nil, **data) ⇒ Object

Convenience: log structured events without repeating formatting.

Fatty.log(:decode_getch, ch: 27, note: "escape")

Supported tags are: - keycode:: raw curses input / bytes / ESC buffering - keyevent:: constructed KeyEvent normalization (ctrl/meta mapping) - keybinding:: KeyMap#resolve hits/misses, contexts used - action:: Actions.call, target selection, unknown action - command:: Terminal.apply_command / :send dispatches - session:: session update calls, mode/context stack changes - render:: viewports, layout sizes, redraw triggers - perf:: timings, frame time, slow paths - all:: All of the above



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/fatty/logger.rb', line 71

def self.log(event = nil, level: :debug, tag: nil, **data)
  return unless logger

  tags = active_tags
  if tag && !tags.include?(:all)
    return unless tags.include?(tag.to_sym)
  end

  payload = { event: event, tag: tag }
  payload.merge!(data.reject { |k, _| k == :event || k == :tag })

  logger.add(severity(level), payload)
rescue StandardError => ex
  begin
    logger&.add(
      ::Logger::FATAL,
      { event: "logger_error", err: ex.class.name, msg: ex.message, bt: ex.backtrace&.take(10) },
    )
  rescue StandardError
    # swallow
  end
end

.severity(sym) ⇒ Object

Translate our severity symbols to those expected by ::Logger.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/fatty/logger.rb', line 95

def self.severity(sym)
  case sym&.to_sym
  when :debug
    # This is 0
    ::Logger::DEBUG
  when :info
    # This is 1
    ::Logger::INFO
  when :warn
    # This is 2
    ::Logger::WARN
  when :error
    # This is 3
    ::Logger::ERROR
  when :fatal
    # This is 4
    ::Logger::FATAL
  else
    ::Logger::DEBUG
  end
end