Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tilfin committed Oct 16, 2016
0 parents commit 4fa7f41
Show file tree
Hide file tree
Showing 12 changed files with 419 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/.bundle/
/.yardoc
/Gemfile.lock
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--format documentation
--color
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: ruby
rvm:
- 2.2.3
before_install: gem install bundler -v 1.11.2
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in ougai.gemspec
gemspec
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Toshimitsu Takahashi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
117 changes: 117 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
Ougai
=====

A JSON logger is compatible with [bunyan](https://github.com/trentm/node-bunyan) for Node.js

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'ougai'
```

And then execute:

```
$ bundle
```

Or install it yourself as:

```
$ gem install ougai
```

## Usage

Ougai::Logger is sub-class of original Logger in Ruby.

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

logger = Ougai::Logger.new(STDOUT)
```

### log only message

```ruby
logger.info('Information!')
```

```json
{"name":"main","hostname":"mint","pid":14607,"level":30,"time":"2016-10-16T22:26:48.835+09:00","v":0,"msg":"Information!"}
```

### log with a message and custom data

```ruby
logger.debug('Debugging', data_id: 1, data_flag: true)
logger.debug('Debug!', custom_data: { id: 1, name: 'something' })
```

```json
{"name":"main","hostname":"mint","pid":14607,"level":20,"time":"2016-10-16T22:26:48.836+09:00","v":0,"msg":"Debugging","custom_data":{"id":1,"name":"something"}}
{"name":"main","hostname":"mint","pid":14607,"level":20,"time":"2016-10-16T22:26:48.836+09:00","v":0,"msg":"Debug!","data_id":1,"data_flag":true}
```

### log with a message and an exception

```ruby
begin
raise StandardError, 'fatal error'
rescue => ex
logger.fatal('Unexpected!', ex)
end
```

```json
{"name":"main","hostname":"mint","pid":14607,"level":60,"time":"2016-10-16T22:26:48.836+09:00","v":0,"msg":"Unexpected!","err":{"name":"StandardError","message":"fatal error","stack":"main.rb:12:in `<main>'"}}
```

### log with a message, an exception and custom data

```ruby
begin
1 / 0
rescue => ex
logger.error('Caught error', ex, reason: 'zero spec')
end
```

```json
{"name":"main","hostname":"mint","pid":14607,"level":50,"time":"2016-10-16T22:26:48.836+09:00","v":0,"msg":"Caught error","err":{"name":"ZeroDivisionError","message":"divided by 0","stack":"main.rb:18:in `/'\n ...'"},"reason":"zero spec"}
```


## View log by node-bunyan

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

```
$ npm install -g bunyan
```

Pass a log file to command `bunyan`

```
$ bunyan output.log
[2016-10-16T22:26:48.835+09:00] INFO: main/14607 on mint: Info message!
[2016-10-16T22:26:48.836+09:00] DEBUG: main/14607 on mint: Debugging (data_id=1, data_flag=true)
[2016-10-16T22:26:48.836+09:00] DEBUG: main/14607 on mint: Debug!
custom_data: {
"id": 1,
"name": "something"
}
[2016-10-16T22:26:48.836+09:00] FATAL: main/14607 on mint: Unexpected!
main.rb:12:in `<main>'
[2016-10-16T22:26:48.836+09:00] ERROR: main/14607 on mint: Caught error (reason="z
main.rb:18:in `/'
main.rb:18:in `<main>'
```


## License

[MIT](LICENSE.txt)
6 changes: 6 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require "bundler/gem_tasks"
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec)

task :default => :spec
93 changes: 93 additions & 0 deletions lib/ougai.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
require "ougai/version"
require 'logger'
require 'socket'
require 'time'
require 'json'

module Ougai
class Logger < Logger
def initialize(*args)
super(*args)
run_filename = File.basename($0, ".rb")
hostname = Socket.gethostname
@formatter = proc do |severity, time, progname, data|
JSON.generate({
name: progname || run_filename,
hostname: hostname,
pid: $$,
level: to_level(severity),
time: time.iso8601(3),
v: 0
}.merge(data)) + "\n"
end
end

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

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

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

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

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

private

def to_item(msg, ex, data)
item = {}
if ex.nil? && msg.is_a?(Exception)
item[:msg] = ex.to_s
item[:err] = serialize_ex(ex)
elsif ex
item[:msg] = msg
if ex.is_a?(Hash)
item.merge!(ex)
elsif ex.is_a?(Exception)
item[:err] = serialize_ex(ex)
item.merge!(data)
end
else
item[:msg] = msg
item.merge!(data)
end
item
end

def serialize_ex(ex)
err = {
name: ex.class.name,
message: ex.to_s
}
if ex.backtrace
err[:stack] = ex.backtrace.join("\n ")
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
3 changes: 3 additions & 0 deletions lib/ougai/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Ougai
VERSION = "0.1.0"
end
24 changes: 24 additions & 0 deletions ougai.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'ougai/version'

Gem::Specification.new do |spec|
spec.name = "ougai"
spec.version = Ougai::VERSION
spec.authors = ["Toshimitsu Takahashi"]
spec.email = ["[email protected]"]

spec.summary = %q{JSON logger compatible node-bunyan.}
spec.description = %q{JSON logger compatible bunyan for Node.js}
spec.homepage = "https://github.com/tilfin/ougai"
spec.license = "MIT"

spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
spec.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
spec.require_paths = ['lib']

spec.add_development_dependency "bundler", "~> 1.11"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.0"
end
Loading

0 comments on commit 4fa7f41

Please sign in to comment.