Class: Giblish::AsciidoctorLogger::UserInfoFormatter

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

Overview

log formatter specialized for formatting messages from asciidoctor's stdout

Constant Summary collapse

SEVERITY_LABELS =
{ "WARN" => "WARNING", "FATAL" => "FAILED" }.freeze

Instance Method Summary collapse

Instance Method Details

#call(severity, datetime, progname, msg) ⇒ Object

The hash that can be emitted as the msg from asciidoctor have the following format:

:source_location=>#<Asciidoctor::Reader::Cursor:0x000055e65a8729e0
        @file="<full adoc filename>",
        @dir="<full src dir>",
        @path="<only file name>",
        @lineno=<src line no>


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/giblish/utils.rb', line 52

def call(severity, datetime, progname, msg)
  message = case msg
            when ::String
              msg
            when ::Hash
              # asciidoctor seem to emit a hash with error text and source location info
              # for warnings and errors
              str = String.new("")
              src_loc = msg.fetch(:source_location, nil)
              err_txt = msg.fetch(:text, nil)
              str << "Line #{src_loc.lineno} - " if src_loc
              str << err_txt.to_s if err_txt
              str
            else
              msg.inspect
            end
  %(#{datetime.strftime('%H:%M:%S')} #{progname}: #{SEVERITY_LABELS[severity] || severity}: #{message}\n)
end