Module: MotherBrain::Logging

Defined Under Namespace

Classes: BasicFormat

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_argument_headerObject

Places the arguments given surrounded by whitespace at the top of the logfile. This makes it easier to scan logs for the beginning of a command.



14
15
16
17
18
19
20
21
# File 'lib/mb/logging.rb', line 14

def add_argument_header
  return if dev == STDOUT

  logger.unknown nil
  logger.unknown nil
  logger.unknown ARGV.join(" ")
  logger.unknown nil
end

.devIO?

Returns the currrent logging device

Returns:

  • (IO, nil)


26
27
28
# File 'lib/mb/logging.rb', line 26

def dev
  logdev.dev
end

.filenameString?

Returns the filename of the current logger

Returns:

  • (String, nil)


33
34
35
# File 'lib/mb/logging.rb', line 33

def filename
  logdev.filename
end

.loggerLogger

Returns:

  • (Logger)


38
39
40
# File 'lib/mb/logging.rb', line 38

def logger
  @logger ||= setup
end

.resetnil

Returns:

  • (nil)


43
44
45
46
# File 'lib/mb/logging.rb', line 43

def reset
  @logger = nil
  @preserved_options = nil
end

.set_logger(obj) ⇒ Logger

Parameters:

  • obj (Logger, nil)

Returns:

  • (Logger)


92
93
94
# File 'lib/mb/logging.rb', line 92

def set_logger(obj)
  @logger = (obj.nil? ? Logger.new('/dev/null') : obj)
end

.setup(options = {}) ⇒ Logger

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :level (String, Integer) — default: INFO
  • :location (String, IO)

Returns:

  • (Logger)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/mb/logging.rb', line 52

def setup(options = {})
  options = options.keep_if { |key, value| value }
  options = preserve(options).reverse_merge(
    level: INFO,
    location: FileSystem.logs.join('application.log')
  )

  level    = options[:level].is_a?(String) ? options[:level].upcase : options[:level]
  location = options[:location]

  if %w[DEBUG INFO WARN ERROR FATAL].include?(level)
    level = const_get(level)
  end

  if %w[STDERR STDOUT].include?(location)
    location = location.constantize
  end

  unless [STDERR, STDOUT].include?(location)
    setup_logdir(location)
  end

  if jruby? && location.is_a?(Pathname)
    location = location.to_s
  end

  @logger = Ridley::Logging::Logger.new(location).tap do |log|
    log.level = level
    log.formatter = BasicFormat.new
  end

  Ridley.logger = @logger
  Celluloid.logger = ENV["DEBUG_CELLULOID"] ? @logger : nil

  @logger
end

Instance Method Details

#log_exception(ex) ⇒ Object

Log an exception and it’s backtrace to FATAL

Parameters:

  • ex (Exception)


127
128
129
130
131
132
# File 'lib/mb/logging.rb', line 127

def log_exception(ex)
  ex = ex.respond_to?(:cause) ? ex.cause : ex

  log.fatal { "#{ex.class}: #{ex}" }
  log.fatal { ex.backtrace.join("\n") } unless ex.backtrace.nil?
end

#loggerLogger Also known as: log

Returns:

  • (Logger)


119
120
121
# File 'lib/mb/logging.rb', line 119

def logger
  MB::Logging.logger
end