require "bbc/cosmos/logger/version" require 'log4r' require 'log4r-gelf' # Monkey Patch Log4r to allow us to use is as a Rack::CommonLogger module Log4r class Logger def write(value); info(value) end def <<(value); info(value) end end end module BBC module Cosmos module Logger class SimpleFormatter < Log4r::Formatter def format(event) "[#{Time.now}] #{event.name} #{Log4r::LNAMES[event.level]} #{event.data}\n" end end DEFAULT_LOG_LOCATION = 'debug.log' DEFAULT_MAX_BACKUPS = 2 DEFAULT_MAXTIME = 600 DEFAULT_GELF_SERVER = 'localhost' DEFAULT_GELF_PORT = '12201' def self.level case "#{ENV['APP_LOG_LEVEL']}".upcase when 'DEBUG' Log4r::DEBUG when 'INFO' Log4r::INFO when 'WARN' Log4r::WARN when 'ERROR' Log4r::ERROR when 'FATAL' Log4r::FATAL else Log4r::INFO end end def self.create(id) log = Log4r::Logger.new(id) # Add File Outputter Log4r::RollingFileOutputter.new( 'logfile', :formatter => SimpleFormatter, :filename => ENV['APP_LOG_LOCATION'] || DEFAULT_LOG_LOCATION, :max_backups => ENV['LOG_MAX_BACKUPS'] || DEFAULT_MAX_BACKUPS, :maxtime => ENV['LOG_MAXTIME'] || DEFAULT_MAXTIME, :trunc => true) log.add('logfile') # Add Gelf Outputter Log4r::GelfOutputter.new( 'gelfling', :formatter => SimpleFormatter, 'gelf_server' => ENV['GELF_SERVER'], 'gelf_port' => ENV['GELF_PORT']) log.add('gelfling') # Set Log Level log.level = level log end end end end