Skip to content

Commit

Permalink
Merge branch 'master' of github.com:tilfin/ougai
Browse files Browse the repository at this point in the history
  • Loading branch information
tilfin committed Nov 2, 2016
2 parents 0b0e26e + eee9f50 commit ef28058
Show file tree
Hide file tree
Showing 10 changed files with 466 additions and 312 deletions.
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ source 'https://rubygems.org'

# Specify your gem's dependencies in ougai.gemspec
gemspec

group :test do
gem 'awesome_print'
end
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ end
```


## View log by node-bunyan
### View log by node-bunyan

Install [bunyan](https://github.com/trentm/node-bunyan) via NPM

Expand All @@ -171,6 +171,27 @@ $ bunyan output.log
main.rb:18:in `<main>'
```

## Use human Readable formatter for console

Add awesome_print to Gemfile and `bundle`

```ruby
gem 'awesome_print'
```

Set *Ougai::Formatters::Readable* instance to `formatter` accessor

```ruby
require 'rubygems'
require 'ougai'

logger = Ougai::Logger.new(STDOUT)
logger.formatter = Ougai::Formatters::Readable.new
```

### Screen result example

![Screen Shot](https://github.com/tilfin/ougai/blob/images/ougai_readable_format.png?raw=true))

## License

Expand Down
125 changes: 5 additions & 120 deletions lib/ougai.rb
Original file line number Diff line number Diff line change
@@ -1,120 +1,5 @@
require "ougai/version"
require 'logger'
require 'socket'
require 'time'
require 'json'

module Ougai
class Logger < Logger
attr_accessor :default_message, :app_name
attr_accessor :ex_key, :ex_trace_indent

def initialize(*args)
super(*args)
@default_message = 'No message'
@app_name = File.basename($0, ".rb")
@hostname = Socket.gethostname
@ex_key = :err
@ex_trace_indent = 2
@formatter = create_formatter
end

def debug(message, ex = nil, data = nil)
super(to_item(message, ex, data))
end

def info(message, ex = nil, data = nil)
super(to_item(message, ex, data))
end

def warn(message, ex = nil, data = nil)
super(to_item(message, ex, data))
end

def error(message, ex = nil, data = nil)
super(to_item(message, ex, data))
end

def fatal(message, ex = nil, data = nil)
super(to_item(message, ex, data))
end

protected

def create_formatter
proc do |severity, time, progname, data|
JSON.generate({
name: progname || @app_name,
hostname: @hostname,
pid: $$,
level: to_level(severity),
time: time.iso8601(3),
v: 0
}.merge(data)) + "\n"
end
end

private

def to_item(msg, ex, data)
item = {}
if ex.nil? # 1 arg
if msg.is_a?(Exception)
item[:msg] = msg.to_s
item[@ex_key] = serialize_ex(msg)
elsif msg.is_a?(Hash)
item[:msg] = @default_message unless msg.key?(:msg)
item.merge!(msg)
else
item[:msg] = msg.to_s
end
elsif data.nil? # 2 args
if ex.is_a?(Exception)
item[:msg] = msg.to_s
item[@ex_key] = serialize_ex(ex)
elsif ex.is_a?(Hash)
item.merge!(ex)
if msg.is_a?(Exception)
item[@ex_key] = serialize_ex(msg)
else
item[:msg] = msg.to_s
end
end
elsif msg # 3 args
item[@ex_key] = serialize_ex(ex) if ex.is_a?(Exception)
item.merge!(data) if data.is_a?(Hash)
item[:msg] = msg.to_s
else # No args
item[:msg] = @default_message
end
item
end

def serialize_ex(ex)
err = {
name: ex.class.name,
message: ex.to_s
}
if ex.backtrace
sp = "\n" + ' ' * @ex_trace_indent
err[:stack] = ex.backtrace.join(sp)
end
err
end

def to_level(severity)
case severity
when 'INFO'
30
when 'WARN'
40
when 'ERROR'
50
when 'FATAL'
60
else # DEBUG
20
end
end
end
end
require 'ougai/version'
require 'ougai/formatters/base'
require 'ougai/formatters/bunyan'
require 'ougai/formatters/readable'
require 'ougai/logger'
34 changes: 34 additions & 0 deletions lib/ougai/formatters/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'logger'
require 'time'
require 'socket'

module Ougai
module Formatters
class Base < Logger::Formatter
attr_accessor :trace_indent, :trace_max_lines

def initialize(app_name = nil, hostname = nil)
@app_name = app_name || File.basename($0, ".rb")
@hostname = hostname || Socket.gethostname
@trace_indent = 2
@trace_max_lines = 100
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
end
end
end
34 changes: 34 additions & 0 deletions lib/ougai/formatters/bunyan.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'ougai/formatters/base'
require 'json'

module Ougai
module Formatters
class Bunyan < Base
def call(severity, time, progname, data)
JSON.generate({
name: progname || @app_name,
hostname: @hostname,
pid: $$,
level: to_level(severity),
time: time.iso8601(3),
v: 0
}.merge(data)) + "\n"
end

def to_level(severity)
case severity
when 'INFO'
30
when 'WARN'
40
when 'ERROR'
50
when 'FATAL'
60
else # DEBUG
20
end
end
end
end
end
53 changes: 53 additions & 0 deletions lib/ougai/formatters/readable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require 'ougai/formatters/base'

module Ougai
module Formatters
class Readable < Base
def initialize(app_name = nil, hostname = nil)
super(app_name, hostname)
@trace_indent = 4
load_dependency
end

def call(severity, time, progname, data)
msg = data.delete(:msg)
strs = ["[#{time.iso8601(3)}] #{colored_level(severity)}: #{msg}"]
if data.key?(:err)
err = data.delete(:err)
err_str = " #{err[:name]} (#{err[:message]}):"
err_str += "\n " + err[:stack] if err.key?(:stack)
strs.push(err_str)
end
unless data.empty?
strs.push(data.ai)
end
strs.join("\n") + "\n"
end

def colored_level(severity)
case severity
when 'INFO'
color = '0;36'
when 'WARN'
color = '0;33'
when 'ERROR'
color = '0;31'
when 'FATAL'
color = '0;35'
else # DEBUG
color = '0;37'
end
"\e[#{color}m#{severity}\e[0m"
end

private

def load_dependency
require 'awesome_print'
rescue LoadError
puts 'You must install the awesome_print gem to use this output.'
raise
end
end
end
end
81 changes: 81 additions & 0 deletions lib/ougai/logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
require 'ougai/formatters/bunyan'
require 'logger'

module Ougai
class Logger < ::Logger
attr_accessor :default_message, :exc_key

def initialize(*args)
super(*args)
@default_message = 'No message'
@exc_key = :err
@formatter = create_formatter
end

def debug(message, ex = nil, data = nil)
super(to_item(message, ex, data))
end

def info(message, ex = nil, data = nil)
super(to_item(message, ex, data))
end

def warn(message, ex = nil, data = nil)
super(to_item(message, ex, data))
end

def error(message, ex = nil, data = nil)
super(to_item(message, ex, data))
end

def fatal(message, ex = nil, data = nil)
super(to_item(message, ex, data))
end

protected

def create_formatter
Formatters::Bunyan.new
end

private

def to_item(msg, ex, data)
item = {}
if ex.nil? # 1 arg
if msg.is_a?(Exception)
item[:msg] = msg.to_s
set_exc(item, msg)
elsif msg.is_a?(Hash)
item[:msg] = @default_message unless msg.key?(:msg)
item.merge!(msg)
else
item[:msg] = msg.to_s
end
elsif data.nil? # 2 args
if ex.is_a?(Exception)
item[:msg] = msg.to_s
set_exc(item, ex)
elsif ex.is_a?(Hash)
item.merge!(ex)
if msg.is_a?(Exception)
set_exc(item, msg)
else
item[:msg] = msg.to_s
end
end
elsif msg # 3 args
set_exc(item, ex) if ex.is_a?(Exception)
item.merge!(data) if data.is_a?(Hash)
item[:msg] = msg.to_s
else # No args
item[:msg] = @default_message
end
item
end

def set_exc(item, exc)
item[@exc_key] = @formatter.serialize_exc(exc)
end
end
end
Loading

0 comments on commit ef28058

Please sign in to comment.