Skip to content

Instantly share code, notes, and snippets.

@bibikovilya
Created October 18, 2017 14:27
Show Gist options
  • Save bibikovilya/48ab80eb26669aa48ce8d92d2aebfc07 to your computer and use it in GitHub Desktop.
Save bibikovilya/48ab80eb26669aa48ce8d92d2aebfc07 to your computer and use it in GitHub Desktop.
Logger
module SoapService
class SoapLogger
attr_accessor :logger
class << self
attr_accessor :class_logger
delegate :info, :warn, :debug, :error, to: :class_logger
end
def initialize
log = Logger.new('log/soap_service.log')
log.formatter = formatter
@logger = ActiveSupport::TaggedLogging.new(log)
end
def formatter
Proc.new do |severity, time, progname, msg|
formatted_severity = sprintf('%-5s', severity.to_s)
formatted_time = time.strftime('%Y-%m-%d %H:%M:%S')
"[#{formatted_severity} #{formatted_time} #{$$}] #{msg}\n"
end
end
def info(msg, vacation = nil)
@logger.tagged(message_header(vacation)) { @logger.info(msg) }
end
def warn(msg, vacation = nil)
@logger.tagged(message_header(vacation)) { @logger.warn(msg) }
end
def debug(msg, vacation = nil)
@logger.tagged(message_header(vacation)) { @logger.debug(msg) }
end
def error(msg, vacation = nil)
@logger.tagged(message_header(vacation)) { @logger.error(msg) }
end
def message_header(vacation)
vacation ? "< vacation_id: #{vacation.id} >" : ''
end
end
end
SoapService::SoapLogger.class_logger = SoapService::SoapLogger.new
SoapLogger.info("Message for log")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment