Module: JsonRubyLogger

Defined in:
lib/json_ruby_logger.rb,
lib/json_ruby_logger/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.1.8"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.logger(calling_class, caller_info) ⇒ Object

Global, memoized, lazy initialised instance of a logger



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/json_ruby_logger.rb', line 19

def self.logger(calling_class, caller_info)
  logger = Logger.new(STDOUT)

  logger.formatter = proc do |severity, datetime, progname, msg|
    date_format = datetime.iso8601
    JSON.dump(
      date: date_format,
      file_name: caller_info.path.split('/').last,
      calling_class: calling_class,
      function_name: caller_info.label.split(' ').last,
      lineno: caller_info.lineno,
      severity: severity,
      pid: Process.pid,
      message: msg.to_s
    ) + "\n"
  end

  logger.level = ENV.fetch('LOG_LEVEL', 'INFO').upcase
  logger
end

Instance Method Details

#loggerObject

This is the magical bit that gets mixed into your classes



13
14
15
16
# File 'lib/json_ruby_logger.rb', line 13

def logger
  # caller_locations returns an array of Thread::Backtrace::Location
  @logger ||= JsonRubyLogger.logger(self.class.name, caller_locations.first)
end