firehoseio/firehose

View on GitHub
lib/firehose/logging.rb

Summary

Maintainability
A
0 mins
Test Coverage
require 'logger'

module Firehose
  # Sets up logging
  def self.logger
    @logger ||= Logger.new($stdout)
  end

  def self.logger=(logger)
    @logger = logger
  end

  self.logger.level = if ENV['LOG_LEVEL']
    Logger.const_get(ENV['LOG_LEVEL'].upcase)
  else
    case ENV['RACK_ENV']
      when 'test'         then Logger::ERROR
      when 'development'  then Logger::DEBUG
      else Logger::INFO
    end
  end

  # TODO: Provide some way to allow this to be configured via an ENV variable.
  self.logger.formatter = lambda do |severity, time, name, msg|
    out_time = time.utc.strftime "%Y-%m-%d %H:%M:%S.%L"
    "[#{out_time} ##$$] #{severity} : #{msg}\n"
  end
end

# stdout gets "lost" in Foreman if this isn't here
# https://github.com/ddollar/foreman/wiki/Missing-Output
$stdout.sync = true if ENV['RACK_ENV'] == 'development' || ENV['SYNC_LOGGING']