Class: Cosmos::Logger

Inherits:
Object show all
Defined in:
lib/cosmos/utilities/logger.rb

Overview

Supports different levels of logging and only writes to stdout if the level is exceeded.

Constant Summary collapse

DEBUG =

DEBUG only prints DEBUG messages

::Logger::DEBUG
INFO =

INFO prints INFO, DEBUG messages

::Logger::INFO
WARN =

WARN prints WARN, INFO, DEBUG messages

::Logger::WARN
ERROR =

ERROR prints ERROR, WARN, INFO, DEBUG messages

::Logger::ERROR
FATAL =

FATAL prints FATAL, ERROR, WARN, INFO, DEBUG messages

::Logger::FATAL
UNKNOWN =

UNKNOWN always prints

::Logger::UNKNOWN
DEBUG_SEVERITY_STRING =
' DEBUG:'
INFO_SEVERITY_STRING =
' INFO:'
WARN_SEVERITY_STRING =
' WARN:'
ERROR_SEVERITY_STRING =
' ERROR:'
FATAL_SEVERITY_STRING =
' FATAL:'
UNKNOWN_SEVERITY_STRING =
''
@@instance =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(level = Logger::UNKNOWN) ⇒ Logger

Returns a new instance of Logger.

Parameters:

  • level (Integer) (defaults to: Logger::UNKNOWN)

    The initial logging level



49
50
51
52
53
# File 'lib/cosmos/utilities/logger.rb', line 49

def initialize(level = Logger::UNKNOWN)
  @level = level
  @detail_string = nil
  @mutex = Mutex.new
end

Class Method Details

.debug(message = nil, &block) ⇒ Object

Parameters:

  • message (String) (defaults to: nil)

    The message to print if the log level is at or below the method name log level.

  • block (Proc)

    Block to call which should return a string to append to the log message



89
90
91
# File 'lib/cosmos/utilities/logger.rb', line 89

def self.debug(message = nil, &block)
  self.instance.debug(message, &block)
end

.error(message = nil, &block) ⇒ Object

Parameters:

  • message (String) (defaults to: nil)

    The message to print if the log level is at or below the method name log level.

  • block (Proc)

    Block to call which should return a string to append to the log message



104
105
106
# File 'lib/cosmos/utilities/logger.rb', line 104

def self.error(message = nil, &block)
  self.instance.error(message, &block)
end

.fatal(message = nil, &block) ⇒ Object

Parameters:

  • message (String) (defaults to: nil)

    The message to print if the log level is at or below the method name log level.

  • block (Proc)

    Block to call which should return a string to append to the log message



109
110
111
# File 'lib/cosmos/utilities/logger.rb', line 109

def self.fatal(message = nil, &block)
  self.instance.fatal(message, &block)
end

.info(message = nil, &block) ⇒ Object

Parameters:

  • message (String) (defaults to: nil)

    The message to print if the log level is at or below the method name log level.

  • block (Proc)

    Block to call which should return a string to append to the log message



94
95
96
# File 'lib/cosmos/utilities/logger.rb', line 94

def self.info(message = nil, &block)
  self.instance.info(message, &block)
end

.instanceLogger

Returns The logger instance.

Returns:

  • (Logger)

    The logger instance



119
120
121
# File 'lib/cosmos/utilities/logger.rb', line 119

def self.instance
  @@instance ||= self.new
end

.unknown(message = nil, &block) ⇒ Object

Parameters:

  • message (String) (defaults to: nil)

    The message to print if the log level is at or below the method name log level.

  • block (Proc)

    Block to call which should return a string to append to the log message



114
115
116
# File 'lib/cosmos/utilities/logger.rb', line 114

def self.unknown(message = nil, &block)
  self.instance.unknown(message, &block)
end

.warn(message = nil, &block) ⇒ Object

Parameters:

  • message (String) (defaults to: nil)

    The message to print if the log level is at or below the method name log level.

  • block (Proc)

    Block to call which should return a string to append to the log message



99
100
101
# File 'lib/cosmos/utilities/logger.rb', line 99

def self.warn(message = nil, &block)
  self.instance.warn(message, &block)
end

Instance Method Details

#debug(message = nil, &block) ⇒ Object

Parameters:

  • message (String) (defaults to: nil)

    The message to print if the log level is at or below the method name log level.

  • block (Proc)

    Block to call which should return a string to append to the log message



59
60
61
# File 'lib/cosmos/utilities/logger.rb', line 59

def debug(message = nil, &block)
  log_message(DEBUG_SEVERITY_STRING, message, &block) if @level <= DEBUG
end

#error(message = nil, &block) ⇒ Object

Parameters:

  • message (String) (defaults to: nil)

    The message to print if the log level is at or below the method name log level.

  • block (Proc)

    Block to call which should return a string to append to the log message



74
75
76
# File 'lib/cosmos/utilities/logger.rb', line 74

def error(message = nil, &block)
  log_message(ERROR_SEVERITY_STRING, message, &block) if @level <= ERROR
end

#fatal(message = nil, &block) ⇒ Object

Parameters:

  • message (String) (defaults to: nil)

    The message to print if the log level is at or below the method name log level.

  • block (Proc)

    Block to call which should return a string to append to the log message



79
80
81
# File 'lib/cosmos/utilities/logger.rb', line 79

def fatal(message = nil, &block)
  log_message(FATAL_SEVERITY_STRING, message, &block) if @level <= FATAL
end

#info(message = nil, &block) ⇒ Object

Parameters:

  • message (String) (defaults to: nil)

    The message to print if the log level is at or below the method name log level.

  • block (Proc)

    Block to call which should return a string to append to the log message



64
65
66
# File 'lib/cosmos/utilities/logger.rb', line 64

def info(message = nil, &block)
  log_message(INFO_SEVERITY_STRING, message, &block) if @level <= INFO
end

#levelInteger

Returns The logging level.

Returns:

  • (Integer)

    The logging level



23
# File 'lib/cosmos/utilities/logger.rb', line 23

instance_attr_accessor :level

#unknown(message = nil, &block) ⇒ Object

Parameters:

  • message (String) (defaults to: nil)

    The message to print if the log level is at or below the method name log level.

  • block (Proc)

    Block to call which should return a string to append to the log message



84
85
86
# File 'lib/cosmos/utilities/logger.rb', line 84

def unknown(message = nil, &block)
  log_message(UNKNOWN_SEVERITY_STRING, message, &block) if @level <= UNKNOWN
end

#warn(message = nil, &block) ⇒ Object

Parameters:

  • message (String) (defaults to: nil)

    The message to print if the log level is at or below the method name log level.

  • block (Proc)

    Block to call which should return a string to append to the log message



69
70
71
# File 'lib/cosmos/utilities/logger.rb', line 69

def warn(message = nil, &block)
  log_message(WARN_SEVERITY_STRING, message, &block) if @level <= WARN
end