Class: Logmsg::LogFile

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

Overview

Manage a destination stream or file that will be used to output log messages

Author:

  • Egon Braun

Constant Summary collapse

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_SEVERITY =

The default severity used to print log messages

Logger::INFO
DEFAULT_SEVERITY_THRESHOLD =

The default severity threshold used to print log messages

Logger::WARN
DEFAULT_FORMAT =

The default format of the log message

'%{severity}: %{msg}'
STDOUT_STREAM =

Constants to define the standard output and error streams and set them out from normal files

1
STDERR_STREAM =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, path = STDOUT_STREAM) ⇒ LogFile

Returns a new instance of LogFile


40
41
42
43
44
45
46
47
48
# File 'lib/logmsg/logfile.rb', line 40

def initialize(name, path = STDOUT_STREAM)
  @name = name
  @registered = false
  @path = path
  @threshold = DEFAULT_SEVERITY_THRESHOLD
  @severities = []
  @datetime_format = DEFAULT_DATETIME_FORMAT
  @format = DEFAULT_FORMAT
end

Instance Attribute Details

#datetime_formatObject

Returns the value of attribute datetime_format


37
38
39
# File 'lib/logmsg/logfile.rb', line 37

def datetime_format
  @datetime_format
end

#formatObject

Returns the value of attribute format


38
39
40
# File 'lib/logmsg/logfile.rb', line 38

def format
  @format
end

#nameObject (readonly)

Returns the value of attribute name


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

def name
  @name
end

#pathObject

Returns the value of attribute path


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

def path
  @path
end

#registeredObject (readonly)

Returns the value of attribute registered


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

def registered
  @registered
end

#severitiesObject

Returns the value of attribute severities


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

def severities
  @severities
end

#thresholdObject

Returns the value of attribute threshold


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

def threshold
  @threshold
end

Instance Method Details

#debug(message) ⇒ Object

Log a debug message

> debug('This is a log message')

Parameters:

  • message (String)

    the log message


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

def debug(message)
  log(message, Logger::DEBUG)
end

#error(message) ⇒ Object

Log an error message

> error('This is a log message')

Parameters:

  • message (String)

    the log message


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

def error(message)
  log(message, Logger::ERROR)
end

#fatal(message) ⇒ Object

Log a fatal error message

> fatal('This is a log message')

Parameters:

  • message (String)

    the log message


111
112
113
# File 'lib/logmsg/logfile.rb', line 111

def fatal(message)
  log(message, Logger::FATAL)
end

#info(message) ⇒ Object

Log an informational message

> info('This is a log message')

Parameters:

  • message (String)

    the log message


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

def info(message)
  log(message, Logger::INFO)
end

#registerObject

Adds a logger definition linked to a file descriptor


52
53
54
55
56
57
# File 'lib/logmsg/logfile.rb', line 52

def register
  unregister if registered
  init_formatter
  init_logger
  @registered = true
end

#unknown(message) ⇒ Object

Log an unknown message

> debug('This is a log message')

Parameters:

  • message (String)

    the log message


120
121
122
# File 'lib/logmsg/logfile.rb', line 120

def unknown(message)
  log(message, Logger::UNKNOWN)
end

#unregisterObject

Remove the logger and output stream definitions closing all open files


61
62
63
64
65
66
67
68
# File 'lib/logmsg/logfile.rb', line 61

def unregister
  unless @logger.nil?
    @logger.close unless @path == STDOUT_STREAM || @path == STDERR_STREAM
    @logger = nil
  end

  @registered = false
end

#warn(message) ⇒ Object

Log a warning message

> warn('This is a log message')

Parameters:

  • message (String)

    the log message


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

def warn(message)
  log(message, Logger::WARN)
end