Class: Logmaster
- Inherits:
-
Object
- Object
- Logmaster
- Defined in:
- lib/logmaster.rb
Instance Attribute Summary collapse
-
#email_config ⇒ Object
Returns the value of attribute email_config.
-
#email_log_level ⇒ Object
Returns the value of attribute email_log_level.
-
#log_level ⇒ Object
Returns the value of attribute log_level.
-
#loggers ⇒ Object
Returns the value of attribute loggers.
-
#logstash_config ⇒ Object
Returns the value of attribute logstash_config.
-
#name ⇒ Object
Returns the value of attribute name.
-
#raise_exception ⇒ Object
Returns the value of attribute raise_exception.
Instance Method Summary collapse
-
#initialize(log_level: Logger::INFO, file: nil, stdout: true, raise_exception: false, email_config: nil, logstash_config: nil, name: "Logmaster") ⇒ Logmaster
constructor
A new instance of Logmaster.
- #watch_exceptions ⇒ Object
Constructor Details
#initialize(log_level: Logger::INFO, file: nil, stdout: true, raise_exception: false, email_config: nil, logstash_config: nil, name: "Logmaster") ⇒ Logmaster
Returns a new instance of Logmaster.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/logmaster.rb', line 7 def initialize( log_level: Logger::INFO, file: nil, # if nil, will not log into any file stdout: true, # if false, will not log into STDOUR raise_exception: false, # if true, will a raise an Exception after logging it email_config: nil, # see email config options for Pony gem logstash_config: nil, # see https://github.com/dwbutler/logstash-logger#basic-usage name: "Logmaster" ) @name = name @raise_exception = raise_exception @log_level = log_level @loggers = [] self.email_config = email_config if email_config @logstash_config = logstash_config @loggers << ::Logger.new(STDOUT) if stdout @loggers << ::Logger.new(file, 10, 1024000) if file if logstash_config require 'logstash-logger' @loggers << LogStashLogger.new(**logstash_config) end @loggers.each { |l| l.level = @log_level } end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object (private)
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/logmaster.rb', line 75 def method_missing(name, *args) if [:unknown, :fatal, :error, :warn, :info, :debug].include?(name) if @email_config && @email_log_level <= Logger.const_get(name.to_s.upcase) send_email(type: name, message: args[0]) end @loggers.each do |logger| logger.send(name, *args) end end end |
Instance Attribute Details
#email_config ⇒ Object
Returns the value of attribute email_config.
5 6 7 |
# File 'lib/logmaster.rb', line 5 def email_config @email_config end |
#email_log_level ⇒ Object
Returns the value of attribute email_log_level.
5 6 7 |
# File 'lib/logmaster.rb', line 5 def email_log_level @email_log_level end |
#log_level ⇒ Object
Returns the value of attribute log_level.
5 6 7 |
# File 'lib/logmaster.rb', line 5 def log_level @log_level end |
#loggers ⇒ Object
Returns the value of attribute loggers.
5 6 7 |
# File 'lib/logmaster.rb', line 5 def loggers @loggers end |
#logstash_config ⇒ Object
Returns the value of attribute logstash_config.
5 6 7 |
# File 'lib/logmaster.rb', line 5 def logstash_config @logstash_config end |
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/logmaster.rb', line 5 def name @name end |
#raise_exception ⇒ Object
Returns the value of attribute raise_exception.
5 6 7 |
# File 'lib/logmaster.rb', line 5 def raise_exception @raise_exception end |
Instance Method Details
#watch_exceptions ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/logmaster.rb', line 57 def watch_exceptions raise "Please provide a block to this method" unless block_given? begin yield rescue Exception => e = e.class.to_s += ": " += e. += "\n" += "Backtrace:\n" e.backtrace.each { |l| += " #{l}\n" } self.fatal() raise e if @raise_exception end end |