Sha256: f168525b300b3f503d11b33e66ec122de49f572ee6266d61ad0a4e8cd3aa3b43

Contents?: true

Size: 1.52 KB

Versions: 10

Compression:

Stored size: 1.52 KB

Contents

# Receives a variety of objects for logging.
# LogSanity itself sends Hashes which are formatted with #to_json.
# Other than Strings, will embed any other object into a jsonified hash.
# Strings are a bit of a special case and by default continue to be formatted
# with whatever Rails' formatter originally was. As such, it can be configured
# using `config.log_formatter`. This keeps exception backtraces and other
# various logs still as Strings. If you prefer Strings to also be turned into
# jsonified messages, set `config.logsanity.json_strings = true`.

module LogSanity
  class Formatter < Logger::Formatter
    include ActiveSupport::TaggedLogging::Formatter
      # tags are ignored when rendering as json
      # however, tags are prepended when rendering with string_formatter

    def call(severity, timestamp, progname, msg)
      if msg.is_a? Hash
        msg.reverse_merge!('at' => timestamp) unless msg.key?('at')
      elsif msg.is_a? String
        if string_formatter
          msg = "#{tags_text}#{msg}" if current_tags.any?
          return string_formatter.call(severity, timestamp, progname, msg)
        else
          msg = {'at' => timestamp, 'message' => msg}
        end
      else
        msg = {'at' => timestamp, 'object' => msg.inspect}
      end
      msg['at'] = msg['at'].utc
      "#{msg.to_json}\n"
    end

    attr_writer :string_formatter

    def string_formatter
      return @string_formatter if defined?(@string_formatter)
      @string_formatter ||= ActiveSupport::Logger::SimpleFormatter.new
    end

  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
log_sanity-2.3.0 lib/log_sanity/formatter.rb
log_sanity-2.2.0 lib/log_sanity/formatter.rb
log_sanity-2.1.2 lib/log_sanity/formatter.rb
log_sanity-2.1.1 lib/log_sanity/formatter.rb
log_sanity-2.1.0 lib/log_sanity/formatter.rb
log_sanity-2.0.0 lib/log_sanity/formatter.rb
log_sanity-1.3.2 lib/log_sanity/formatter.rb
log_sanity-1.3.1 lib/log_sanity/formatter.rb
log_sanity-1.3.0 lib/log_sanity/formatter.rb
log_sanity-1.2.0 lib/log_sanity/formatter.rb