diff --git a/lib/ougai/formatters/base.rb b/lib/ougai/formatters/base.rb index 1672b8f..40551f5 100644 --- a/lib/ougai/formatters/base.rb +++ b/lib/ougai/formatters/base.rb @@ -3,8 +3,13 @@ module Ougai module Formatters + # Base formatter + # @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) @@ -12,6 +17,7 @@ def initialize(app_name = nil, hostname = nil) @hostname = hostname || Socket.gethostname.force_encoding('UTF-8') @trace_indent = 2 @trace_max_lines = 100 + @serialize_backtrace = true self.datetime_format = nil end @@ -25,14 +31,15 @@ def serialize_exc(ex) message: ex.to_s } if ex.backtrace - err[:stack] = serialize_trace(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.slice(0, @trace_max_lines).join(sp) + trace.join(sp) end private diff --git a/lib/ougai/formatters/readable.rb b/lib/ougai/formatters/readable.rb index 887e542..db26b65 100644 --- a/lib/ougai/formatters/readable.rb +++ b/lib/ougai/formatters/readable.rb @@ -14,6 +14,7 @@ def initialize(opts = {}) @trace_indent = opts[:trace_indent] || 4 @plain = opts[:plain] || false @excluded_fields = opts[:excluded_fields] || [] + @serialize_backtrace = true load_dependent end @@ -28,6 +29,10 @@ def call(severity, time, progname, data) format_log_parts(dt, level, msg, err_str, data_str) end + def serialize_backtrace=(value) + raise RuntimeError, 'Not support serialize_backtrace' + end + protected def format_log_parts(datetime, level, msg, err, data) @@ -61,7 +66,7 @@ def create_err_str(data) return nil unless data.key?(:err) err = data.delete(:err) err_str = " #{err[:name]} (#{err[:message]}):" - err_str += "\n " + err[:stack] if err.key?(:stack) + err_str += "\n" + (" " * @trace_indent) + err[:stack] if err.key?(:stack) err_str end diff --git a/lib/ougai/version.rb b/lib/ougai/version.rb index d63d747..b55e694 100644 --- a/lib/ougai/version.rb +++ b/lib/ougai/version.rb @@ -1,3 +1,3 @@ module Ougai - VERSION = "1.5.6" + VERSION = "1.5.7" end diff --git a/spec/formatters/base_spec.rb b/spec/formatters/base_spec.rb index 3685ae4..4f8c9f1 100644 --- a/spec/formatters/base_spec.rb +++ b/spec/formatters/base_spec.rb @@ -64,4 +64,35 @@ expect(subject.hostname).to eq('myhost') end end + + describe '#serialize_exc' do + let (:app_name) { 'myapp' } + let (:hostname) { 'myhost' } + let (:errmsg) { 'dummy error' } + + it 'returning data with stack as String' do + begin + raise errmsg + rescue => e + result = subject.serialize_exc(e) + end + expect(result[:message]).to eq(errmsg) + expect(result[:stack]).to be_instance_of(String) + end + + context 'not serialize backtrace' do + it 'returning data with stack as Array' do + subject.serialize_backtrace = false + subject.trace_max_lines = 6 + begin + raise errmsg + rescue => e + result = subject.serialize_exc(e) + end + expect(result[:message]).to eq(errmsg) + expect(result[:stack]).to be_instance_of(Array) + expect(result[:stack].size).to eq(6) + end + end + end end