Class: Betterlog::Log

Inherits:
Object
  • Object
show all
Extended by:
ComplexConfig::Provider::Shortcuts
Includes:
Tins::SexySingleton
Defined in:
lib/betterlog/log.rb,
lib/betterlog/log/event.rb,
lib/betterlog/log/severity.rb,
lib/betterlog/log/event_formatter.rb,
lib/betterlog/log/legacy_event_formatter.rb

Overview

A flexible, framework-agnostic logging solution that provides a clean API for structured logging with automatic Rails integration and error recovery.

This class implements a singleton pattern using Tins::SexySingleton and automatically detects Rails environment to use its logger when available. It supports structured logging with contextual information, location tracking, and event notification capabilities.

Examples:

Basic usage

Betterlog::Log.info("User logged in", meta: { user_id: 123 })

Defined Under Namespace

Classes: Event, EventFormatter, LegacyEventFormatter, Severity

Instance Method Summary collapse

Instance Method Details

#debug(object, **rest) ⇒ Log

Logs a message on severity debug.



67
68
69
70
71
72
# File 'lib/betterlog/log.rb', line 67

def debug(object, **rest)
  protect do
    rest = { severity: __method__ } | rest
    emit Log::Event.ify(object, **rest)
  end
end

#emit(event) ⇒ Betterlog::Log

Emits a log event by adding contextual information, notifying subscribers, and logging through the configured logger.

This method enhances the provided event with location information if available, sets the emitter identifier, triggers any registered notifiers, and finally logs the event using the application’s logger at the event’s severity level.



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/betterlog/log.rb', line 120

def emit(event)
  l = caller_locations.reverse_each.each_cons(3).find { |c, n1, n2|
    n2.absolute_path =~ /betterlog\/log\.rb/ and break c # TODO check if this still works
  }
  if l
    event[:location] = [ l.absolute_path, l.lineno ] * ?:
  end
  event[:emitter] = self.class.name.downcase
  notify(event)
  logger.send(event.severity.to_sym, JSON.generate(event))
  self
end

#error(object, **rest) ⇒ Log

Logs a message on severity error.



79
80
81
82
83
84
# File 'lib/betterlog/log.rb', line 79

def error(object, **rest)
  protect do
    rest = { severity: __method__ } | rest
    emit Log::Event.ify(object, **rest)
  end
end

#fatal(object, **rest) ⇒ Log

Logs a message on severity fatal.



91
92
93
94
95
96
# File 'lib/betterlog/log.rb', line 91

def fatal(object, **rest)
  protect do
    rest = { severity: __method__ } | rest
    emit Log::Event.ify(object, **rest)
  end
end

#info(object, **rest) ⇒ Log

Logs a message on severity info.



43
44
45
46
47
48
# File 'lib/betterlog/log.rb', line 43

def info(object, **rest)
  protect do
    rest = { severity: __method__ } | rest
    emit Log::Event.ify(object, **rest)
  end
end

#loggerLogger

Returns the appropriate logger instance for the application.

This method checks if Rails is defined and has a logger available, falling back to a default logger otherwise.



34
35
36
# File 'lib/betterlog/log.rb', line 34

def logger
  defined?(Rails) && Rails.respond_to?(:logger) ? Rails.logger : self.class.default_logger
end

#output(object, **rest) ⇒ Log

Logs a message on severity debug, by default, this can be changed by passing the severity: keyword.



104
105
106
107
108
# File 'lib/betterlog/log.rb', line 104

def output(object, **rest)
  protect do
    emit Log::Event.ify(object, **rest)
  end
end

#warn(object, **rest) ⇒ Log

Logs a message on severity warn.



55
56
57
58
59
60
# File 'lib/betterlog/log.rb', line 55

def warn(object, **rest)
  protect do
    rest = { severity: __method__ } | rest
    emit Log::Event.ify(object, **rest)
  end
end