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
-
.logger ⇒ Object
Returns the value of attribute logger.
-
.path ⇒ Object
Returns the value of attribute path.
Class Method Summary collapse
- .active_tags ⇒ Object
- .configure ⇒ Object
-
.log(event = nil, level: :debug, tag: nil, **data) ⇒ Object
Convenience: log structured events without repeating formatting.
-
.severity(sym) ⇒ Object
Translate our severity symbols to those expected by ::Logger.
Class Attribute Details
.logger ⇒ Object
Returns the value of attribute logger.
14 15 16 |
# File 'lib/fatty/logger.rb', line 14 def logger @logger end |
.path ⇒ Object
Returns the value of attribute path.
14 15 16 |
# File 'lib/fatty/logger.rb', line 14 def path @path end |
Class Method Details
.active_tags ⇒ Object
52 53 54 55 |
# File 'lib/fatty/logger.rb', line 52 def self. = Fatty::Config.config.dig(:log, :tags) || [:all] Array().map(&:to_sym) end |
.configure ⇒ Object
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.(cfg.dig(:log, :file)) elsif ENV['XDG_STATE_HOME'] File.(File.join(ENV['XDG_STATE_HOME'], progname, "#{progname}.log")) else File.(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 = if tag && !.include?(:all) return unless .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., 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 |