Sha256: cda2781a46ed4c8145665f680e4da247c98ab7b8f45ce3910a79e1d76ac07368

Contents?: true

Size: 1.91 KB

Versions: 2

Compression:

Stored size: 1.91 KB

Contents

require 'time'
require 'socket'

module Ougai
  module Formatters
    # Base formatter
    # Custom formatter must override `_call`.
    # @attr [Fixnum] trace_indent Specify exception backtrace indent (by default this is 2).
    # @attr [Fixnum] trace_max_lines Keep exception backtrace lines (by default this is 100).
    # @attr [Boolean] serialize_backtrace Whether exception should converts String (by default this is on).
    class Base < Logger::Formatter
      attr_accessor :trace_indent, :trace_max_lines
      attr_accessor :serialize_backtrace
      attr_reader :app_name, :hostname

      def initialize(app_name = nil, hostname = nil)
        @app_name = app_name || File.basename($0, ".rb")
        @hostname = hostname || Socket.gethostname.force_encoding('UTF-8')
        @trace_indent = 2
        @trace_max_lines = 100
        @serialize_backtrace = true
        self.datetime_format = nil
      end

      def call(severity, time, progname, data)
        _call(severity, time, progname, data.is_a?(Hash) ? data : { msg: data.to_s })
      end

      def _call(severity, time, progname, data)
        raise NotImplementedError, "_call must be implemented"
      end

      def datetime_format=(value)
        @datetime_format = value || default_datetime_format
      end

      def serialize_exc(ex)
        err = {
          name: ex.class.name,
          message: ex.to_s
        }
        if ex.backtrace
          bt = ex.backtrace.slice(0, @trace_max_lines)
          err[:stack] = @serialize_backtrace ? serialize_trace(bt) : bt
        end
        err
      end

      def serialize_trace(trace)
        sp = "\n" + ' ' * @trace_indent
        trace.join(sp)
      end

      private

      def format_datetime(time)
        time.strftime(@datetime_format)
      end

      def default_datetime_format
        t = Time.new
        f = '%FT%T.%3N'
        f << (t.utc? ? 'Z' : '%:z')
        f.freeze
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ougai-1.6.1 lib/ougai/formatters/base.rb
ougai-1.5.8 lib/ougai/formatters/base.rb