Module: Log2Json

Defined in:
lib/log2json/railslogger.rb

Class Method Summary collapse

Class Method Details

.create_custom_rails_logger(config) ⇒ Object

Create a custom logger that uses its own formatting for easier parsing by a log2json log monitoring script.



28
29
30
31
32
33
34
35
36
37
# File 'lib/log2json/railslogger.rb', line 28

def self.create_custom_rails_logger(config)
  path = config.paths["log"].first
  unless File.exist? File.dirname path
    FileUtils.mkdir_p File.dirname path
  end
  config.colorize_logging = false
  logger = ::Logger.new(path)
  logger.formatter = ::Log2Json::log_formatter
  ActiveSupport::TaggedLogging.new(logger)
end

.create_custom_unicorn_logger(config) ⇒ Object

Simiar to the custom rails logger, but for unicorn.



41
42
43
44
45
# File 'lib/log2json/railslogger.rb', line 41

def self.create_custom_unicorn_logger(config)
  logger = ::Logger.new(config.set[:stderr_path])
  logger.formatter = ::Log2Json::log_formatter
  logger
end

.log_formatterObject



18
19
20
21
22
23
# File 'lib/log2json/railslogger.rb', line 18

def self.log_formatter
  proc do |severity, datetime, progname, msg|
    "#{datetime.strftime('%Y-%m-%dT%H:%M:%S%z')}: [#{severity}] #{$$} #{msg.gsub(/\n/, '#012')}\n"
    # Note: Following rsyslog's convention, all newlines are converted to '#012'.
  end
end