-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:tilfin/ougai
- Loading branch information
Showing
10 changed files
with
466 additions
and
312 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.