Class: Logmaster

Inherits:
Object
  • Object
show all
Defined in:
lib/logmaster.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_configObject

Returns the value of attribute email_config.



5
6
7
# File 'lib/logmaster.rb', line 5

def email_config
  @email_config
end

#email_log_levelObject

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_levelObject

Returns the value of attribute log_level.



5
6
7
# File 'lib/logmaster.rb', line 5

def log_level
  @log_level
end

#loggersObject

Returns the value of attribute loggers.



5
6
7
# File 'lib/logmaster.rb', line 5

def loggers
  @loggers
end

#logstash_configObject

Returns the value of attribute logstash_config.



5
6
7
# File 'lib/logmaster.rb', line 5

def logstash_config
  @logstash_config
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/logmaster.rb', line 5

def name
  @name
end

#raise_exceptionObject

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_exceptionsObject



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
    message =  e.class.to_s
    message += ": "
    message += e.message
    message += "\n"
    message += "Backtrace:\n"
    e.backtrace.each { |l| message += "    #{l}\n" }
    self.fatal(message)
    raise e if @raise_exception
  end
end