lib/semantic_logger/log.rb in semantic_logger-4.0.0 vs lib/semantic_logger/log.rb in semantic_logger-4.1.0
- old
+ new
@@ -41,12 +41,15 @@
# The backtrace captured at source when the log level >= SemanticLogger.backtrace_level
#
# metric_amount [Numeric]
# Used for numeric or counter metrics.
# For example, the number of inquiries or, the amount purchased etc.
+ #
+ # context [Hash]
+ # Named contexts that were captured when the log entry was created.
class Log
- attr_accessor :level, :thread_name, :name, :message, :payload, :time, :duration, :tags, :level_index, :exception, :metric, :backtrace, :metric_amount, :named_tags
+ attr_accessor :level, :thread_name, :name, :message, :payload, :time, :duration, :tags, :level_index, :exception, :metric, :backtrace, :metric_amount, :named_tags, :context
def initialize(name, level, index = nil)
@level = level
@thread_name = Thread.current.name
@name = name
@@ -178,11 +181,11 @@
end
# Returns [String] duration of the log entry as a string
# Returns nil if their is no duration
# Java time precision does not include microseconds
- if defined? JRuby
+ if Formatters::Base::PRECISION == 3
def duration_to_s
"#{duration.to_i}ms" if duration
end
else
def duration_to_s
@@ -254,78 +257,25 @@
# Returns [true|false] whether the log entry has a payload
def has_payload?
!(payload.nil? || (payload.respond_to?(:empty?) && payload.empty?))
end
- if defined? JRuby
- # Return the Time as a formatted string
- # JRuby only supports time in ms
- # DEPRECATED
- def 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
- # DEPRECATED
- def formatted_time
- "#{time.strftime('%Y-%m-%d %H:%M:%S')}.#{'%06d' % (time.usec)}"
- end
+ # DEPRECATED
+ def formatted_time
+ time.strftime(Formatters::Base::TIME_FORMAT)
end
- # Returns [Hash] representation of this log entry
+ DeprecatedLogger = Struct.new(:host, :application)
+
+ # DEPRECATED: Use SemanticLogger::Formatters::Raw
def to_h(host = SemanticLogger.host, application = SemanticLogger.application)
- # Header
- h = {
- name: name,
- pid: $$,
- thread: thread_name,
- time: time,
- level: level,
- level_index: level_index,
- }
- h[:host] = host if host
- h[:application] = application if application
- file, line = file_name_and_line
- if file
- h[:file] = file
- h[:line] = line.to_i
- end
+ logger = DeprecatedLogger.new(host, application)
+ SemanticLogger::Formatters::Raw.new.call(self, logger)
+ end
- # Tags
- h[:tags] = tags if tags && !tags.empty?
- h[:named_tags] = named_tags if named_tags && !named_tags.empty?
-
- # Duration
- if duration
- h[:duration_ms] = duration
- h[:duration] = duration_human
- end
-
- # Log message
- h[:message] = cleansed_message if message
-
- # Payload
- h[:payload] = payload if payload && payload.respond_to?(:empty?) && !payload.empty?
-
- # Exceptions
- if exception
- root = h
- each_exception do |exception, i|
- name = i == 0 ? :exception : :cause
- root[name] = {
- name: exception.class.name,
- message: exception.message,
- stack_trace: exception.backtrace
- }
- root = root[name]
- end
- end
-
- # Metric
- h[:metric] = metric if metric
- h[:metric_amount] = metric_amount if metric_amount
- h
+ # Lazy initializes the context hash and assigns a key value pair.
+ def set_context(key, value)
+ (self.context ||= {})[key] = value
end
private
SELF_PATTERN = File.join('lib', 'semantic_logger')