Sha256: fe0ada5230f5a7375674e6f915ef9586ce95668768e9fad8d3a93f8105c363d8

Contents?: true

Size: 1.17 KB

Versions: 5

Compression:

Stored size: 1.17 KB

Contents

require 'time'
require 'socket'

module Ougai
  module Formatters
    class Base < Logger::Formatter
      attr_accessor :trace_indent, :trace_max_lines
      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
        self.datetime_format = nil
      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
          err[:stack] = serialize_trace(ex.backtrace)
        end
        err
      end

      def serialize_trace(trace)
        sp = "\n" + ' ' * @trace_indent
        trace.slice(0, @trace_max_lines).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

5 entries across 5 versions & 1 rubygems

Version Path
ougai-1.5.6 lib/ougai/formatters/base.rb
ougai-1.5.5 lib/ougai/formatters/base.rb
ougai-1.5.4 lib/ougai/formatters/base.rb
ougai-1.5.3 lib/ougai/formatters/base.rb
ougai-1.5.2 lib/ougai/formatters/base.rb