Class: Giblish::AsciidoctorLogger

Inherits:
Logger
  • Object
show all
Defined in:
lib/giblish/utils.rb

Overview

This logger is customized to receive log messages via the Asciidoctor API. It parses the messages and ‘source_location’ objects from the Asciidoctor API into messages using an opinionated format.

The output is written to both $stdout and an in-memory StringIO instance. The log level can be set separately for each of these output channels.

Defined Under Namespace

Classes: UserInfoFormatter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_logger, string_level = nil) ⇒ AsciidoctorLogger

The level is one of the standard ::Logger levels

stdout_level

the log level to use for gating the messages to stdout

string_level

the log level to use for gating the messages to the in-memory string.

defaults to ‘stdout_level’ if not set.



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/giblish/utils.rb', line 93

def initialize(user_logger, string_level = nil)
  # def initialize(stdout_level, string_level = nil)
  string_level = stdout_level if string_level.nil?
  @user_logger = user_logger

  @max_severity = UNKNOWN

  # create a new, internal logger that echos messages to an in-memory string
  @in_mem_storage = StringIO.new
  # @in_mem_logger = ::Logger.new(@in_mem_storage, formatter: UserInfoFormatter.new, level: string_level)
  super(@in_mem_storage, progname: "(asciidoctor)", formatter: UserInfoFormatter.new, level: string_level)
end

Instance Attribute Details

#in_mem_storageObject (readonly)

Returns the value of attribute in_mem_storage.



44
45
46
# File 'lib/giblish/utils.rb', line 44

def in_mem_storage
  @in_mem_storage
end

#max_severityObject (readonly)

Returns the value of attribute max_severity.



44
45
46
# File 'lib/giblish/utils.rb', line 44

def max_severity
  @max_severity
end

Instance Method Details

#add(severity, message = nil, progname = nil, &block) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/giblish/utils.rb', line 106

def add(severity, message = nil, progname = nil, &block)
  # add message to adoc log
  super(severity, message.dup, progname)

  # add message to user log (wierd interface... see Logger::add(...))
  message = yield if block
  message ||= progname
  @user_logger&.add(severity, "(asciidoctor) #{UserInfoFormatter.adoc_to_message(message)}")

  # update the maximum severity received by this logger
  @max_severity = severity if severity != UNKNOWN && severity > @max_severity
end