Skip to content

Commit

Permalink
Merge pull request #44 from tilfin/feature/keep-backtrace-array
Browse files Browse the repository at this point in the history
Add serialize_backtrace attr to base formatter
  • Loading branch information
tilfin authored Dec 17, 2017
2 parents d2777d5 + 2174942 commit 5ac21c1
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
11 changes: 9 additions & 2 deletions lib/ougai/formatters/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@

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)
@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

Expand All @@ -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
Expand Down
7 changes: 6 additions & 1 deletion lib/ougai/formatters/readable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/ougai/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Ougai
VERSION = "1.5.6"
VERSION = "1.5.7"
end
31 changes: 31 additions & 0 deletions spec/formatters/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5ac21c1

Please sign in to comment.