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

#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



127
128
129
# File 'lib/ductr/log/logger.rb', line 127

def error(...)
  write(::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



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

def fatal(...)
  write(::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



101
102
103
# File 'lib/ductr/log/logger.rb', line 101

def info(...)
  write(::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



114
115
116
# File 'lib/ductr/log/logger.rb', line 114

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