module Appydays::Loggable

Helpers for working with structured logging. Use this instead of calling semantic_logger directly. Generally you `include Appydays::Loggable`

Public Class Methods

[](key) click to toggle source

Return the logger for a key/object.

# File lib/appydays/loggable.rb, line 56
def self.[](key)
  return key.logger if key.respond_to?(:logger)
  (key = key.class) unless [Module, Class].include?(key.class)
  return SemanticLogger[key]
end
configure_12factor(format: nil, application: nil) click to toggle source

Configure logging for 12 factor applications. Specifically, that means setting STDOUT to synchronous, using STDOUT as the log output, and also conveniently using color formatting if using a tty or json otherwise (ie, you want to use json logging on a server).

# File lib/appydays/loggable.rb, line 68
def self.configure_12factor(format: nil, application: nil)
  format ||= $stdout.isatty ? :color : :json
  $stdout.sync = true
  SemanticLogger.application = application if application
  SemanticLogger.add_appender(io: $stdout, formatter: format.to_sym)
end
default_level=(v) click to toggle source
# File lib/appydays/loggable.rb, line 44
def self.default_level=(v)
  self.set_default_level(v)
end
ensure_stderr_appender() click to toggle source
# File lib/appydays/loggable.rb, line 81
def self.ensure_stderr_appender
  return if @stderr_appended
  SemanticLogger.add_appender(io: $stderr)
  @stderr_appended = true
end
included(target) click to toggle source
# File lib/appydays/loggable.rb, line 37
def self.included(target)
  target.include(SemanticLogger::Loggable)

  target.extend(Methods)
  target.include(Methods)
end
set_default_level(v, warning: true) click to toggle source
# File lib/appydays/loggable.rb, line 48
def self.set_default_level(v, warning: true)
  return if v == SemanticLogger.default_level
  self[self].warn "Overriding log level to %p" % v if warning
  SemanticLogger.default_level = v
end
with_log_tags(tags, &block) click to toggle source
# File lib/appydays/loggable.rb, line 75
def self.with_log_tags(tags, &block)
  return SemanticLogger.named_tagged(tags, &block)
end