Class: Cosmos::Logger

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

Overview

Supports different levels of logging and only writes 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
DEBUG_SEVERITY_STRING =
'DEBUG'
INFO_SEVERITY_STRING =
'INFO'
WARN_SEVERITY_STRING =
'WARN'
ERROR_SEVERITY_STRING =
'ERROR'
FATAL_SEVERITY_STRING =
'FATAL'
@@mutex =
Mutex.new
@@instance =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Logger.

Parameters:

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

    The initial logging level



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/cosmos/utilities/logger.rb', line 71

def initialize(level = Logger::INFO)
  @stdout = true
  @level = level
  @scope = nil
  @detail_string = nil
  @container_name = Socket.gethostname
  @microservice_name = nil
  @tag = @container_name + ".log"
  @mutex = Mutex.new
  @no_store = ENV['COSMOS_NO_STORE']
end

Class Method Details

.debug(message = nil, scope: nil, user: 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



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

def self.debug(message = nil, scope: nil, user: nil, &block)
  args = {}
  args[:scope] = scope if scope
  args[:user] = user if user
  self.instance.debug(message, **args, &block)
end

.error(message = nil, scope: nil, user: 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



136
137
138
139
140
141
# File 'lib/cosmos/utilities/logger.rb', line 136

def self.error(message = nil, scope: nil, user: nil, &block)
  args = {}
  args[:scope] = scope if scope
  args[:user] = user if user
  self.instance.error(message, **args, &block)
end

.fatal(message = nil, scope: nil, user: 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



144
145
146
147
148
149
# File 'lib/cosmos/utilities/logger.rb', line 144

def self.fatal(message = nil, scope: nil, user: nil, &block)
  args = {}
  args[:scope] = scope if scope
  args[:user] = user if user
  self.instance.fatal(message, **args, &block)
end

.info(message = nil, scope: nil, user: 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



120
121
122
123
124
125
# File 'lib/cosmos/utilities/logger.rb', line 120

def self.info(message = nil, scope: nil, user: nil, &block)
  args = {}
  args[:scope] = scope if scope
  args[:user] = user if user
  self.instance.info(message, **args, &block)
end

.instanceLogger

Returns The logger instance.

Returns:

  • (Logger)

    The logger instance



152
153
154
155
156
157
158
159
# File 'lib/cosmos/utilities/logger.rb', line 152

def self.instance
  return @@instance if @@instance

  @@mutex.synchronize do
    @@instance ||= self.new
  end
  @@instance
end

.warn(message = nil, scope: nil, user: 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



128
129
130
131
132
133
# File 'lib/cosmos/utilities/logger.rb', line 128

def self.warn(message = nil, scope: nil, user: nil, &block)
  args = {}
  args[:scope] = scope if scope
  args[:user] = user if user
  self.instance.warn(message, **args, &block)
end

Instance Method Details

#debug(message = nil, scope: @scope, user: 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



87
88
89
# File 'lib/cosmos/utilities/logger.rb', line 87

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

#detail_stringString

Returns Additional detail to add to messages.

Returns:

  • (String)

    Additional detail to add to messages



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

instance_attr_accessor :detail_string

#error(message = nil, scope: @scope, user: 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



102
103
104
# File 'lib/cosmos/utilities/logger.rb', line 102

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

#fatal(message = nil, scope: @scope, user: 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



107
108
109
# File 'lib/cosmos/utilities/logger.rb', line 107

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

#info(message = nil, scope: @scope, user: 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



92
93
94
# File 'lib/cosmos/utilities/logger.rb', line 92

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

#levelInteger

Returns The logging level.

Returns:

  • (Integer)

    The logging level



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

instance_attr_accessor :level

#microservice_nameString

Returns Microservice name.

Returns:

  • (String)

    Microservice name



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

instance_attr_accessor :microservice_name

#scopeString

Returns Scope.

Returns:



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

instance_attr_accessor :scope

#stdoutBoolean

Returns Whether to output the message to stdout.

Returns:

  • (Boolean)

    Whether to output the message to stdout



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

instance_attr_accessor :stdout

#tagString

Returns Fluent tag.

Returns:



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

instance_attr_accessor :tag

#warn(message = nil, scope: @scope, user: 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



97
98
99
# File 'lib/cosmos/utilities/logger.rb', line 97

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