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, 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
# 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
  name:            "Logmaster"
)

  @name            = name
  @raise_exception = raise_exception
  @log_level       = log_level
  @loggers         = []

  self.email_config = email_config if email_config

  @loggers << ::Logger.new(STDOUT)            if stdout
  @loggers << ::Logger.new(file, 10, 1024000) if file
  @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)



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/logmaster.rb', line 69

def method_missing(name, *args)

  if [:unknown, :fatal, :error, :warn, :info, :debug].include?(name)
    
    if @email_config && @log_level <= Logger.const_get(@email_log_level.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

#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

#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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/logmaster.rb', line 51

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