Class: Kitchen::Logger::StructuredLogdevLogger

Inherits:
Object
  • Object
show all
Includes:
Logger::Severity
Defined in:
lib/kitchen/logger.rb

Overview

Internal class that emits one JSON object per log event.

Constant Summary collapse

SEVERITY_NAMES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Maps stdlib Logger severity constants to their lowercase string names as they appear in emitted JSON events.

Returns:

  • (Hash{Integer => String}) —

    severity constant to name mapping

{
  DEBUG => "debug",
  INFO => "info",
  WARN => "warn",
  ERROR => "error",
  FATAL => "fatal",
  UNKNOWN => "unknown",
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logdev, metadata_provider) ⇒ StructuredLogdevLogger

Returns a new instance of StructuredLogdevLogger.



654
655
656
657
658
659
660
661
# File 'lib/kitchen/logger.rb', line 654

def initialize(logdev, )
  @logdev = logdev
   = 
  @level = INFO
  @progname = "Kitchen"
  @sequence = 0
  @mutex = Mutex.new
end

Instance Attribute Details

#datetime_format ⇒ Object

Returns the value of attribute datetime_format.



652
653
654
# File 'lib/kitchen/logger.rb', line 652

def datetime_format
  @datetime_format
end

#level ⇒ Object

Returns the value of attribute level.



650
651
652
# File 'lib/kitchen/logger.rb', line 650

def level
  @level
end

#progname ⇒ Object

Returns the value of attribute progname.



651
652
653
# File 'lib/kitchen/logger.rb', line 651

def progname
  @progname
end

Instance Method Details

#<<(msg) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Appends raw stream output, emitting a structured event per complete line.

Parameters:

  • msg (String) —

    a chunk of stream output



692
693
694
# File 'lib/kitchen/logger.rb', line 692

def <<(msg)
  line_buffer << msg
end

#add(severity, message = nil, progname = nil) { ... } ⇒ true

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Writes a log event if the given severity meets the current level.

Parameters:

  • severity (Integer) —

    a stdlib Logger severity constant

  • message (#to_s, nil) (defaults to: nil) —

    the message to log; when nil the block's value is used, falling back to progname

  • progname (#to_s, nil) (defaults to: nil) —

    used as the message when both message and a block are absent

Yields:

  • evaluates to the message to log, only when the severity is high enough to be recorded

Returns:

  • (true) —

    always, once the event has been considered



674
675
676
677
678
679
680
681
682
683
684
# File 'lib/kitchen/logger.rb', line 674

def add(severity, message = nil, progname = nil)
  severity ||= UNKNOWN
  return true if severity < level

  message = if message.nil?
              block_given? ? yield : progname
            else
              message
            end
  write_event(severity, message, "log")
end

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Writes a banner event at info severity.

Parameters:

  • msg (#to_s, nil) (defaults to: nil) —

    the message to log; when nil the block's value is used

Yields:

  • evaluates to the message to log



703
704
705
706
# File 'lib/kitchen/logger.rb', line 703

def banner(msg = nil)
  message = block_given? ? yield : msg
  write_event(INFO, message, "banner") unless INFO < level
end

#close ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Closes the underlying log device if it is open and supports closing.



810
811
812
813
814
815
816
817
818
# File 'lib/kitchen/logger.rb', line 810

def close
  return unless @logdev.respond_to?(:close)

  if @logdev.respond_to?(:closed?)
    @logdev.close unless @logdev.closed?
  else
    @logdev.close
  end
end

#debug(msg = nil) { ... } ⇒ true

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Writes a log event at debug severity.

Parameters:

  • msg (#to_s, nil) (defaults to: nil) —

    the message to log; when nil the block's value is used

Yields:

  • evaluates to the message to log

Returns:

  • (true)


727
728
729
# File 'lib/kitchen/logger.rb', line 727

def debug(msg = nil, &block)
  add(DEBUG, msg, nil, &block)
end

#debug? ⇒ Boolean

Returns:

  • (Boolean)


786
787
788
# File 'lib/kitchen/logger.rb', line 786

def debug?
  level <= DEBUG
end

#error(msg = nil) { ... } ⇒ true

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Writes a log event at error severity.

Parameters:

  • msg (#to_s, nil) (defaults to: nil) —

    the message to log; when nil the block's value is used

Yields:

  • evaluates to the message to log

Returns:

  • (true)


760
761
762
# File 'lib/kitchen/logger.rb', line 760

def error(msg = nil, &block)
  add(ERROR, msg, nil, &block)
end

#error? ⇒ Boolean

Returns:

  • (Boolean)


798
799
800
# File 'lib/kitchen/logger.rb', line 798

def error?
  level <= ERROR
end

#fatal(msg = nil) { ... } ⇒ true

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Writes a log event at fatal severity.

Parameters:

  • msg (#to_s, nil) (defaults to: nil) —

    the message to log; when nil the block's value is used

Yields:

  • evaluates to the message to log

Returns:

  • (true)


771
772
773
# File 'lib/kitchen/logger.rb', line 771

def fatal(msg = nil, &block)
  add(FATAL, msg, nil, &block)
end

#fatal? ⇒ Boolean

Returns:

  • (Boolean)


802
803
804
# File 'lib/kitchen/logger.rb', line 802

def fatal?
  level <= FATAL
end

#info(msg = nil) { ... } ⇒ true

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Writes a log event at info severity.

Parameters:

  • msg (#to_s, nil) (defaults to: nil) —

    the message to log; when nil the block's value is used

Yields:

  • evaluates to the message to log

Returns:

  • (true)


738
739
740
# File 'lib/kitchen/logger.rb', line 738

def info(msg = nil, &block)
  add(INFO, msg, nil, &block)
end

#info? ⇒ Boolean

Returns:

  • (Boolean)


790
791
792
# File 'lib/kitchen/logger.rb', line 790

def info?
  level <= INFO
end

#stream(severity, msg) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Writes a stream event at the given severity.

Parameters:

  • severity (Integer, Symbol) —

    a stdlib Logger severity constant or its symbol name

  • msg (#to_s) —

    the message to log



715
716
717
718
# File 'lib/kitchen/logger.rb', line 715

def stream(severity, msg)
  severity = severity_const(severity)
  write_event(severity, msg, "stream") unless severity < level
end

#unknown(msg = nil) { ... } ⇒ true

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Writes a log event at unknown severity.

Parameters:

  • msg (#to_s, nil) (defaults to: nil) —

    the message to log; when nil the block's value is used

Yields:

  • evaluates to the message to log

Returns:

  • (true)


782
783
784
# File 'lib/kitchen/logger.rb', line 782

def unknown(msg = nil, &block)
  add(UNKNOWN, msg, nil, &block)
end

#warn(msg = nil) { ... } ⇒ true

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Writes a log event at warn severity.

Parameters:

  • msg (#to_s, nil) (defaults to: nil) —

    the message to log; when nil the block's value is used

Yields:

  • evaluates to the message to log

Returns:

  • (true)


749
750
751
# File 'lib/kitchen/logger.rb', line 749

def warn(msg = nil, &block)
  add(WARN, msg, nil, &block)
end

#warn? ⇒ Boolean

Returns:

  • (Boolean)


794
795
796
# File 'lib/kitchen/logger.rb', line 794

def warn?
  level <= WARN
end