Class: Mammoth::Logging::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/mammoth/logging.rb

Overview

Logger that emits one JSON object per line.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(level:, output: $stdout, clock: -> { Time.now.utc }) ⇒ Logger

Returns a new instance of Logger.

Raises:



16
17
18
19
20
21
# File 'lib/mammoth/logging.rb', line 16

def initialize(level:, output: $stdout, clock: -> { Time.now.utc })
  @level = level.to_s
  @output = output
  @clock = clock
  raise ConfigurationError, "unsupported logging level: #{@level}" unless LEVELS.key?(@level)
end

Instance Attribute Details

#clockObject (readonly)

Returns the value of attribute clock.



14
15
16
# File 'lib/mammoth/logging.rb', line 14

def clock
  @clock
end

#levelObject (readonly)

Returns the value of attribute level.



14
15
16
# File 'lib/mammoth/logging.rb', line 14

def level
  @level
end

#outputObject (readonly)

Returns the value of attribute output.



14
15
16
# File 'lib/mammoth/logging.rb', line 14

def output
  @output
end

Instance Method Details

#debug(event, **context) ⇒ Boolean

Emit a debug event when debug logging is enabled.

Parameters:

  • event (String, Symbol)

    stable event name

  • context (Hash)

    structured event fields

Returns:

  • (Boolean)

    whether the event was emitted



28
# File 'lib/mammoth/logging.rb', line 28

def debug(event, **context) = write("debug", event, context)

#enabled?(severity) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/mammoth/logging.rb', line 51

def enabled?(severity)
  LEVELS.fetch(severity.to_s) >= LEVELS.fetch(level)
end

#error(event, **context) ⇒ Boolean

Emit an error event.

Parameters:

  • event (String, Symbol)

    stable event name

  • context (Hash)

    structured event fields

Returns:

  • (Boolean)

    whether the event was emitted



49
# File 'lib/mammoth/logging.rb', line 49

def error(event, **context) = write("error", event, context)

#info(event, **context) ⇒ Boolean

Emit an informational event when info logging is enabled.

Parameters:

  • event (String, Symbol)

    stable event name

  • context (Hash)

    structured event fields

Returns:

  • (Boolean)

    whether the event was emitted



35
# File 'lib/mammoth/logging.rb', line 35

def info(event, **context) = write("info", event, context)

#warn(event, **context) ⇒ Boolean

Emit a warning event when warn logging is enabled.

Parameters:

  • event (String, Symbol)

    stable event name

  • context (Hash)

    structured event fields

Returns:

  • (Boolean)

    whether the event was emitted



42
# File 'lib/mammoth/logging.rb', line 42

def warn(event, **context) = write("warn", event, context)