Class: Logmsg::LogMsg

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

Overview

Manage the various log destinations

See Also:

Author:

  • Egon Braun

Constant Summary collapse

DEFAULT_SETTINGS_FILE =

The default settings file name

'logmsg.yml'
DEFAULT_PATH =

The default locations in order to find the configuration file

[
  File.join('.', ".#{DEFAULT_SETTINGS_FILE}"),
  File.join(File.expand_path('~'), ".#{DEFAULT_SETTINGS_FILE}"),
  File.join(File.dirname(__FILE__), "logmsg/yaml/#{DEFAULT_SETTINGS_FILE}")
]
DEFAULT_DATETIME_FORMAT =

Log messages usually contain a timestamp associated with them, this is the default date and time formats that logmsg will use when printing out logs

'%Y-%m-%d %H:%M:%S'
DEFAULT_LOG_FORMAT =

The log message default format

'%{progname} [%{datetime}] %{severity}: %{msg}'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace, settings_file = nil) ⇒ LogMsg

Returns a new instance of LogMsg



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/logmsg.rb', line 37

def initialize(namespace, settings_file = nil)
  # If we need to load settings from a custom configuration file then we
  # need to first add it to our load path at the first position
  @path = DEFAULT_PATH.clone
  @path.insert(0, settings_file) unless settings_file.nil?

  # The namespace will work like a section to be parsed in the settings
  # file, so this way we do not need to load huge files in memory and waste
  # resources with stuff we do not need
  @namespace = namespace

  # List of log file destinations
  @logfiles = {}

  # Set the initial state of the class and unloaded
  @loaded = false

  load
end

Instance Attribute Details

#loadedObject (readonly)

Returns the value of attribute loaded



34
35
36
# File 'lib/logmsg.rb', line 34

def loaded
  @loaded
end

#logfilesObject (readonly)

Returns the value of attribute logfiles



33
34
35
# File 'lib/logmsg.rb', line 33

def logfiles
  @logfiles
end

#namespaceObject (readonly)

Returns the value of attribute namespace



31
32
33
# File 'lib/logmsg.rb', line 31

def namespace
  @namespace
end

#pathObject

Returns the value of attribute path



35
36
37
# File 'lib/logmsg.rb', line 35

def path
  @path
end

#settingsObject (readonly)

Returns the value of attribute settings



32
33
34
# File 'lib/logmsg.rb', line 32

def settings
  @settings
end

Instance Method Details

#closeObject

Close all open logfiles and freeing up resources



75
76
77
78
# File 'lib/logmsg.rb', line 75

def close
  @logfiles.each_value(&:unregister)
  @loaded = false
end

#close_logfile(name) ⇒ Object

Close specified open logifle and freeing up resources

Parameters:

  • name (Symbol)

    The name of the logfile to be closed



82
83
84
85
86
# File 'lib/logmsg.rb', line 82

def close_logfile(name)
  return unless @logfiles.key?(name)
  @logfiles[name].unregister
  @logfiles.delete(name)
end

#debug(message) ⇒ Object

Log a debug message to all configured log files

> debug('This is a log message')

Parameters:

  • message (String)

    the log message



92
93
94
95
96
# File 'lib/logmsg.rb', line 92

def debug(message)
  @logfiles.each_value do |logfile|
    logfile.debug(message)
  end
end

#error(message) ⇒ Object

Log an error message to all configured log files

> error('This is a log message')

Parameters:

  • message (String)

    the log message



122
123
124
125
126
# File 'lib/logmsg.rb', line 122

def error(message)
  @logfiles.each_value do |logfile|
    logfile.error(message)
  end
end

#fatal(message) ⇒ Object

Log a fatal error message to all configured log files

> fatal('This is a log message')

Parameters:

  • message (String)

    the log message



132
133
134
135
136
# File 'lib/logmsg.rb', line 132

def fatal(message)
  @logfiles.each_value do |logfile|
    logfile.fatal(message)
  end
end

#info(message) ⇒ Object

Log an informational message to all configured log files

> info('This is a log message')

Parameters:

  • message (String)

    the log message



102
103
104
105
106
# File 'lib/logmsg.rb', line 102

def info(message)
  @logfiles.each_value do |logfile|
    logfile.info(message)
  end
end

#loadObject

Load the configuration file



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/logmsg.rb', line 59

def load
  close if @loaded

  @path.each do |file|
    next unless File.exist?(file)
    @settings = Psych.load_file(file)
    next unless @settings.include?(@namespace)
    parse(@settings[@namespace])
    @loaded = true
    break
  end

  fail unless @loaded
end

#unknown(message) ⇒ Object

Log an unknown message to all configured log files

> debug('This is a log message')

Parameters:

  • message (String)

    the log message



142
143
144
145
146
# File 'lib/logmsg.rb', line 142

def unknown(message)
  @logfiles.each_value do |logfile|
    logfile.unknown(message)
  end
end

#warn(message) ⇒ Object

Log a warning message to all configured log files

> warn('This is a log message')

Parameters:

  • message (String)

    the log message



112
113
114
115
116
# File 'lib/logmsg.rb', line 112

def warn(message)
  @logfiles.each_value do |logfile|
    logfile.warn(message)
  end
end