lib/semantic_logger/appender/base.rb in semantic_logger-2.21.0 vs lib/semantic_logger/appender/base.rb in semantic_logger-3.0.0
- old
+ new
@@ -46,11 +46,11 @@
# Default log formatter
# Replace this formatter by supplying a Block to the initializer
# Generates logs of the form:
# 2011-07-19 14:36:15.660 D [1149:ScriptThreadProcess] Rails -- Hello World
def default_formatter
- Proc.new do |log|
+ Proc.new do |log, logger|
# Header with date, time, log level and process info
entry = "#{log.formatted_time} #{log.level_to_s} [#{log.process_info}]"
# Tags
entry << ' ' << log.tags.collect { |tag| "[#{tag}]" }.join(' ') if log.tags && (log.tags.size > 0)
@@ -68,13 +68,13 @@
if payload = log.payload_to_s(false)
entry << ' -- ' << payload
end
# Exceptions
- log.each_exception do |exception, i|
- entry << (i == 0 ? ' -- Exception: ' : "\nCause: ")
- entry << "#{exception.class}: #{exception.message}\n#{(exception.backtrace || []).join("\n")}"
+ if log.exception
+ entry << " -- Exception: #{log.exception.class}: #{log.exception.message}\n"
+ entry << log.backtrace_to_s
end
entry
end
end
@@ -82,11 +82,11 @@
# To use this formatter
# SemanticLogger.add_appender($stdout, &SemanticLogger::Appender::Base.colorized_formatter)
#
# 2011-07-19 14:36:15.660 D [1149:ScriptThreadProcess] Rails -- Hello World
def self.colorized_formatter
- Proc.new do |log|
+ Proc.new do |log, logger|
colors = SemanticLogger::Appender::AnsiColors
level_color = colors::LEVEL_MAP[log.level]
# Header with date, time, log level and process info
entry = "#{log.formatted_time} #{level_color}#{log.level_to_s}#{colors::CLEAR} [#{log.process_info}]"
@@ -107,18 +107,30 @@
if payload = log.payload_to_s(true)
entry << ' -- ' << payload
end
# Exceptions
- log.each_exception do |exception, i|
- entry << (i == 0 ? ' -- Exception: ' : "\nCause: ")
- entry << "#{colors::BOLD}#{exception.class}: #{exception.message}#{colors::CLEAR}\n#{(exception.backtrace || []).join("\n")}"
+ if log.exception
+ entry << " -- Exception: #{colors::BOLD}#{log.exception.class}: #{log.exception.message}#{colors::CLEAR}\n"
+ entry << log.backtrace_to_s
end
entry
end
end
+ # Optional log formatter to output data in a hash format
+ # To use this formatter
+ # SemanticLogger.add_appender($stdout, &SemanticLogger::Appender::Base.json_formatter)
+ def self.json_formatter
+ Proc.new do |log, logger|
+ h = log.to_h
+ h.delete(:time)
+ h[:timestamp] = log.time.utc.iso8601(defined?(JRuby) ? 3 : 6)
+ h.to_json
+ end
+ end
+
def flush
# An appender can implement a flush method if it supports it.
end
# Returns the current log level if set, otherwise it returns the global
@@ -153,25 +165,9 @@
# Return the level index for fast comparisons
# Returns the lowest level index if the level has not been explicitly
# set for this instance
def level_index
@level_index || 0
- end
-
- if defined? Java
- # Return the Time as a formatted string
- # JRuby only supports time in ms
- def self.formatted_time(time)
- warn '[deprecated] SemanticLogger::Base.formatted_time is deprecated please use log.formatted_time'
- "#{time.strftime('%Y-%m-%d %H:%M:%S')}.#{'%03d' % (time.usec/1000)}"
- end
- else
- # Return the Time as a formatted string
- # Ruby MRI supports micro seconds
- def self.formatted_time(time)
- warn '[deprecated] SemanticLogger::Base.formatted_time is deprecated please use log.formatted_time'
- "#{time.strftime('%Y-%m-%d %H:%M:%S')}.#{'%06d' % (time.usec)}"
- end
end
end
end
end