Module: App47Logger

Included in:
Agent, SsoServer, User
Defined in:
lib/models/concerns/app47_logger.rb

Overview

Mixin for App47 objects to handle logging in both the rails environment as well as the delayed jobs environment that doesn’t know about Rails.logger

Provides ways to log at the class level as well as instance level.

Usage:

App47Logger.log_debug('meesage')

or

class ClassName

include App47Logger

def method_name
  log_debug('message')
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.log_debug(message) ⇒ Object

Log a debug messages



26
27
28
# File 'lib/models/concerns/app47_logger.rb', line 26

def self.log_debug(message)
  log_message :debug, message
end

.log_error(message, exception = nil) ⇒ Object

Log an error messages

  1. Prints the messages

  2. If an exception is passed n

2a prints the exception message
2b prints the stack trace


51
52
53
54
# File 'lib/models/concerns/app47_logger.rb', line 51

def self.log_error(message, exception = nil)
  log_message :error, message
  log_exception :error, exception
end

.log_exception(level, exception = nil, max_frame = 9) ⇒ Object

Log a given exception, but only the first 10 lines in anything but test.

In testing, we want the full stack to know the test case that failed.



61
62
63
64
65
66
67
# File 'lib/models/concerns/app47_logger.rb', line 61

def self.log_exception(level, exception = nil, max_frame = 9)
  return if exception.blank?

  max_frame = -1 if ENV['RACK_ENV'].eql?('test')
  log_message(level, exception.message)
  exception.backtrace[0..max_frame].each { |frame| log_message(level, frame) } unless exception.backtrace.blank?
end

.log_message(level, message) ⇒ Object

Log a given message at the given level



72
73
74
75
76
77
78
79
80
# File 'lib/models/concerns/app47_logger.rb', line 72

def self.log_message(level, message)
  if ENV['AWS_LAMBDA_FUNCTION_VERSION'].nil?
    puts "#{level}: #{message}"
  else
    Logger.new($stdout).send(level, message)
  end
rescue StandardError
  puts "#{level}: #{message}"
end

.log_warn(message, exception = nil) ⇒ Object

Log a warning messages

  1. Prints the messages

  2. If an exception is passed n

2a prints the exception message
2b prints the stack trace


38
39
40
41
# File 'lib/models/concerns/app47_logger.rb', line 38

def self.log_warn(message, exception = nil)
  log_message :warn, message
  log_exception :warn, exception
end

Instance Method Details

#log_debug(message) ⇒ Object

Log a debug message as part of the mixin



92
93
94
# File 'lib/models/concerns/app47_logger.rb', line 92

def log_debug(message)
  App47Logger.log_debug message
end

#log_error(message, exception = nil) ⇒ Object

Log an error message as part of the mixin



106
107
108
# File 'lib/models/concerns/app47_logger.rb', line 106

def log_error(message, exception = nil)
  App47Logger.log_error message, exception
end

#log_message(level, message) ⇒ Object

Log a debug message as part of the mixin



85
86
87
# File 'lib/models/concerns/app47_logger.rb', line 85

def log_message(level, message)
  App47Logger.log_message level, message
end

#log_warn(message, exception = nil) ⇒ Object

Log a warning message as part of the mixin



99
100
101
# File 'lib/models/concerns/app47_logger.rb', line 99

def log_warn(message, exception = nil)
  App47Logger.log_warn message, exception
end