lib/logrb.rb in logrb-0.1.3 vs lib/logrb.rb in logrb-0.1.4
- old
+ new
@@ -23,12 +23,15 @@
#
# #fatal(msg, **fields): Outputs a fatal entry. Calling fatal causes the
# current process to exit with a status 1.
#
# #warn(msg, **fields): Outputs a warning entry.
+#
# #info(msg, **fields): Outputs a informational entry.
+#
# #debug(msg, **fields): Outputs a debug entry.
+#
# #dump(msg, data=nil): Outputs a given String or Array of bytes using the
# same format as `hexdump -C`.
class Logrb
attr_accessor :fields, :level, :format
@@ -167,13 +170,13 @@
[normalize_location(c), c.base_label]
end
# Internal: Performs a cleanup for a given backtrace frame.
#
- # trace - Trace to be clean.
+ # trace - Trace to be clean.
# include_function_name - Optional. When true, includes the function name
- # on the normalized string. Defaults to false.
+ # on the normalized string. Defaults to false.
def normalize_location(trace, include_function_name: false)
path = trace.absolute_path
return trace.to_s if path.nil?
if (root = Gem.path.find { |p| path.start_with?(p) })
@@ -187,10 +190,11 @@
def stack_trace(trace = clean_caller_locations)
trace.map { |s| normalize_location(s, include_function_name: true) }.join("\n")
end
# Internal: Composes a log line with given information.
+ #
# level - The severity of the log message
# caller_meta - An Array containing the caller's location and name
# msg - The message to be logged
# fields - A Hash of fields to be included in the entry
def compose_line(level, caller_meta, msg, fields)
@@ -204,10 +208,11 @@
level_str = color(level, level.to_s.upcase)
"#{ts} #{level_str}: #{caller_meta.last}:#{msg}#{fields_str}"
end
# Internal: Logs a text entry to the current output.
+ #
# level - The severity of the message to be logged.
# msg - The message to be logged
# error - Either an Exception object or nil. This parameter is used
# to provide extra information on the logged entry.
# fields - A Hash containing metadata to be included in the logged
@@ -248,10 +253,11 @@
@output.write(text)
end
end
# Internal: Logs a JSON entry to the current output.
+ #
# level - The severity of the message to be logged.
# msg - The message to be logged
# error - Either an Exception object or nil. This parameter is used
# to provide extra information on the logged entry.
# fields - A Hash containing metadata to be included in the logged
@@ -265,10 +271,13 @@
caller: caller_meta.first,
msg: msg,
ts: Time.now.utc.to_i
}
- data[:stacktrace] = backtrace(error) if level == :error
+ if level == :error
+ data[:exception] = error.message if error.respond_to?(:message)
+ data[:stacktrace] = backtrace(error)
+ end
data.merge!(fields)
write_output("#{data.to_json}\n")
end