Class: Ductr::Log::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/ductr/log/logger.rb

Overview

A ractor compatible logger to be used inside jobs or anywhere else in your ductr project.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prog_name = nil) ⇒ Logger

Create configured outputs instances, meaning that you can't add outputs in an already instantiated logger.



68
69
70
71
72
73
74
75
76
77
# File 'lib/ductr/log/logger.rb', line 68

def initialize(prog_name = nil)
  @prog_name = prog_name

  @outputs = self.class.outputs.map do |output_with_params|
    out, params = *output_with_params
    formatter, options = *params

    out.new(formatter, **options || {})
  end
end

Class Method Details

.add_output(output, formatter = ::Logger::Formatter, **options) ⇒ void

This method returns an undefined value.

Allows to add another log output. Making possible to write logs in multiple places at the same time, e.g. in STDOUT and in logs files

Parameters:

  • output (StandardOutput)

    The new output to write logs to

  • formatter (::Logger::Formatter) (defaults to: ::Logger::Formatter)

    The formatter to use when writing logs

  • **options (Hash)

    The formatter options



22
23
24
25
# File 'lib/ductr/log/logger.rb', line 22

def add_output(output, formatter = ::Logger::Formatter, **options)
  @outputs ||= []
  @outputs.push([output, [formatter, options]])
end

.levelInteger

Returns The current logging level, default ::Logger::DEBUG.

Returns:

  • (Integer)

    The current logging level, default ::Logger::DEBUG



60
61
62
# File 'lib/ductr/log/logger.rb', line 60

def level
  @level || ::Logger::DEBUG
end

.level=(lvl) ⇒ void

This method returns an undefined value.

Configure the logging level.

Parameters:

  • lvl (Symbol, String)

    The desired logging level

Raises:

  • (ArgumentError)


44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ductr/log/logger.rb', line 44

def level=(lvl)
  level_sym = lvl.to_s.downcase.to_sym
  @level = {
    debug: ::Logger::DEBUG,
    info: ::Logger::INFO,
    warn: ::Logger::WARN,
    error: ::Logger::ERROR,
    fatal: ::Logger::FATAL
  }[level_sym]

  raise ArgumentError, "invalid log level: #{lvl}" unless @level
end

.outputsArray<Array<StandardOutput, Array<::Logger::Formatter, Hash>>>

The configured outputs list

Returns:

  • (Array<Array<StandardOutput, Array<::Logger::Formatter, Hash>>>)

    The list of outputs with their formatters and configurations



33
34
35
# File 'lib/ductr/log/logger.rb', line 33

def outputs
  @outputs || [[StandardOutput, [::Logger::Formatter]]]
end

Instance Method Details

#debug { ... } ⇒ void

This method returns an undefined value.

Logs a message with the debug level.

Parameters:

  • message (String)

    The message to log

  • prog_name (String, Symbol)

    The program name of the message

Yields:

  • The message



88
89
90
# File 'lib/ductr/log/logger.rb', line 88

def debug(...)
  write(::Logger::DEBUG, ...)
end

#debug?Boolean

Returns true if the log level allows entries with severity Logger::DEBUG to be written, false otherwise.

Returns:

  • (Boolean)


97
98
99
# File 'lib/ductr/log/logger.rb', line 97

def debug?
  self.class.level <= ::Logger::DEBUG
end

#error { ... } ⇒ void

This method returns an undefined value.

Logs a message with the error level.

Parameters:

  • message (String)

    The message to log

  • prog_name (String, Symbol)

    The program name of the message

Yields:

  • The message



154
155
156
# File 'lib/ductr/log/logger.rb', line 154

def error(...)
  write(::Logger::ERROR, ...)
end

#error?Boolean

Returns true if the log level allows entries with severity Logger::ERROR to be written, false otherwise.

Returns:

  • (Boolean)


163
164
165
# File 'lib/ductr/log/logger.rb', line 163

def error?
  self.class.level <= ::Logger::ERROR
end

#fatal { ... } ⇒ void

This method returns an undefined value.

Logs a message with the fatal level.

Parameters:

  • message (String)

    The message to log

  • prog_name (String, Symbol)

    The program name of the message

Yields:

  • The message



176
177
178
# File 'lib/ductr/log/logger.rb', line 176

def fatal(...)
  write(::Logger::FATAL, ...)
end

#fatal?Boolean

Returns true if the log level allows entries with severity Logger::FATAL to be written, false otherwise.

Returns:

  • (Boolean)


185
186
187
# File 'lib/ductr/log/logger.rb', line 185

def fatal?
  self.class.level <= ::Logger::FATAL
end

#info { ... } ⇒ void

This method returns an undefined value.

Logs a message with the info level.

Parameters:

  • message (String)

    The message to log

  • prog_name (String, Symbol)

    The program name of the message

Yields:

  • The message



110
111
112
# File 'lib/ductr/log/logger.rb', line 110

def info(...)
  write(::Logger::INFO, ...)
end

#info?Boolean

Returns true if the log level allows entries with severity Logger::INFO to be written, false otherwise.

Returns:

  • (Boolean)


119
120
121
# File 'lib/ductr/log/logger.rb', line 119

def info?
  self.class.level <= ::Logger::INFO
end

#warn { ... } ⇒ void

This method returns an undefined value.

Logs a message with the warn level.

Parameters:

  • message (String)

    The message to log

  • prog_name (String, Symbol)

    The program name of the message

Yields:

  • The message



132
133
134
# File 'lib/ductr/log/logger.rb', line 132

def warn(...)
  write(::Logger::WARN, ...)
end

#warn?Boolean

Returns true if the log level allows entries with severity Logger::WARN to be written, false otherwise.

Returns:

  • (Boolean)


141
142
143
# File 'lib/ductr/log/logger.rb', line 141

def warn?
  self.class.level <= ::Logger::WARN
end