lib/daemon_logger.rb in game-server-0.4.4 vs lib/daemon_logger.rb in game-server-0.4.5
- old
+ new
@@ -1,21 +1,22 @@
+# -*- coding: utf-8 -*-
require 'net/smtp'
require 'singleton'
require 'logger'
class DaemonLogger
include Singleton
attr_writer :logger
attr_accessor :email_receivers, :smtp_server
-
+
class NullLogger
def method_missing(*args, &block)
p @args
end
end
-
-
+
+
# ==== Params
# params<Hash>::
# log_file_path<String>:: куда будет писаться лог
# email_receivers<Array[String]>:: список получателей сообщений о критических ошибках
def init(params)
@@ -27,62 +28,65 @@
logger.formatter = Logger::Formatter.new
logger.datetime_format = "%y-%m-%d %H:%M:%S.%L"
self.email_receivers = params[:email_receivers].to_a
self.smtp_server = params[:smtp_server] || 'localhost'
end
-
+
def self.init(*params)
instance.init(*params)
end
def logger
if @logger
@logger
else
- warn 'Logger not initialized yet, run init with :log_file_path'
+ warn 'Logger not initialized yet, run init with :log_file_path'
NullLogger.new
end
end
-
- def send_emails_to_receivers(message)
- return if !email_receivers or email_receivers.empty?
- Net::SMTP.start(smtp_server) do |smtp|
- smtp.send_message message, 'daemon.exceptions@skyburg.com', email_receivers
- end
- rescue => e
- log_exception(e, false, "SENDING EXCEPTION EMAIL ERROR")
- end
-
-
+
+ # def send_emails_to_receivers(message)
+ # return if !email_receivers or email_receivers.empty?
+
+ # Net::SMTP.start(smtp_server) do |smtp|
+ # smtp.send_message message, 'daemon.exceptions@skyburg.com', email_receivers
+ # end
+ # rescue => e
+ # log_exception(e, false, "SENDING EXCEPTION EMAIL ERROR")
+ # end
+
+
module Mixins
-
+
# FIXME:: DOCUMENTATION
def log(message, log_type = :info)
puts "#{Time.now} #{log_type} #{message}"
DaemonLogger.instance.logger.send(log_type, message)
end
-
+
def log_exception(exception, send_mail = true, additional_info = "")
- message = <<"EOF"
-Exception was raised #{exception}
-#{additional_info}
-BACKTRACE:
-#{exception.backtrace.join("\n")}
-EOF
- DaemonLogger.instance.send_emails_to_receivers(message) if send_mail
+# message = <<"EOF"
+# Exception was raised #{exception}
+# #{additional_info}
+
+# BACKTRACE:
+# #{exception.backtrace.join("\n")}
+# EOF
+# DaemonLogger.instance.send_emails_to_receivers(message) if send_mail
log(message, :error)
+ ExceptionNotifier::Notifier.background_exception_notification(e).deliver
end
-
-
+
+
def with_exception_logging
yield
rescue => e
log_exception e, send_mail = true, additional_info = 'WITH EXCEPTION LOGGING CRITICAL ERROR'
raise e
end
-
-
+
+
end
include Mixins
end