lib/timber/logger.rb in timber-1.0.3 vs lib/timber/logger.rb in timber-1.0.4
- old
+ new
@@ -79,10 +79,31 @@
LogEntry.new(level, time, progname, msg, context, nil)
end
end
end
+ # Structures your log messages into Timber's hybrid format, which makes
+ # it easy to read while also appending the appropriate metadata.
+ #
+ # logger = Timber::Logger.new(STDOUT)
+ # logger.formatter = Timber::JSONFormatter.new
+ #
+ # Example message:
+ #
+ # My log message @timber.io {"level":"info","dt":"2016-09-01T07:00:00.000000-05:00"}
+ #
+ class HybridFormatter < Formatter
+ METADATA_CALLOUT = "@timber.io".freeze
+
+ def call(severity, time, progname, msg)
+ log_entry = build_log_entry(severity, time, progname, msg)
+ metadata = log_entry.to_json(:except => [:message])
+ # use << for concatenation for performance reasons
+ log_entry.message.gsub("\n", "\\n") << " " << METADATA_CALLOUT << " " << metadata << "\n"
+ end
+ end
+
# Structures your log messages into JSON.
#
# logger = Timber::Logger.new(STDOUT)
# logger.formatter = Timber::JSONFormatter.new
#
@@ -95,28 +116,23 @@
# use << for concatenation for performance reasons
build_log_entry(severity, time, progname, msg).to_json() << "\n"
end
end
- # Structures your log messages into Timber's hybrid format, which makes
- # it easy to read while also appending the appropriate metadata.
+ # Structures your log messages into JSON.
#
# logger = Timber::Logger.new(STDOUT)
# logger.formatter = Timber::JSONFormatter.new
#
# Example message:
#
- # My log message @timber.io {"level":"info","dt":"2016-09-01T07:00:00.000000-05:00"}
+ # {"level":"info","dt":"2016-09-01T07:00:00.000000-05:00","message":"My log message"}
#
- class HybridFormatter < Formatter
- METADATA_CALLOUT = "@timber.io".freeze
-
+ class MsgPackFormatter < Formatter
def call(severity, time, progname, msg)
- log_entry = build_log_entry(severity, time, progname, msg)
- metadata = log_entry.to_json(:except => [:message])
# use << for concatenation for performance reasons
- log_entry.message.gsub("\n", "\\n") << " " << METADATA_CALLOUT << " " << metadata << "\n"
+ build_log_entry(severity, time, progname, msg).as_json.to_msgpack << "\n"
end
end
# Creates a new Timber::Logger instances. Accepts the same arguments as `::Logger.new`.
# The only difference is that it default the formatter to {HybridFormatter}. Using
@@ -125,10 +141,23 @@
# @example Changing your formatter
# logger = Timber::Logger.new(STDOUT)
# logger.formatter = Timber::Logger::JSONFormatter.new
def initialize(*args)
super(*args)
- self.formatter = HybridFormatter.new
+ if args.size == 1 and args.first.is_a?(LogDevices::HTTP)
+ self.formatter = MsgPackFormatter.new
+ else
+ self.formatter = HybridFormatter.new
+ end
+ end
+
+ def formatter=(value)
+ if @dev.is_a?(Timber::LogDevices::HTTP)
+ raise ArgumentError.new("The formatter cannot be changed when using the " +
+ "Timber::LogDevices::HTTP log device. The MsgPackFormatter must be used for proper " +
+ "delivery.")
+ end
+ super
end
# Backwards compatibility with older ActiveSupport::Logger versions
Logger::Severity.constants.each do |severity|
class_eval(<<-EOT, __FILE__, __LINE__ + 1)
\ No newline at end of file