readme, exception logging

This commit is contained in:
aserafin 2015-04-16 09:20:23 +02:00
parent adde009a64
commit d7996bd745
4 changed files with 77 additions and 15 deletions

View file

@ -1,16 +1,10 @@
# GrapeLogging
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/grape_logging`. To experiment with that code, run `bin/console` for an interactive prompt.
TODO: Delete this and the text above, and describe your gem
# grape_logging
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'grape_logging'
```
gem 'grape_logging'
And then execute:
@ -20,9 +14,51 @@ Or install it yourself as:
$ gem install grape_logging
## Usage
## Basic Usage
TODO: Write usage instructions here
### With Rack
In your `config.ru`:
...
use GrapeLogging::Middleware::RequestLogger
...
run Your::Application
And then set logger formatter in your main api file
logger.formatter = GrapeLogging::Formatters::Default.new
### With Rails
### Other features
## Log Format
With the default configuration you will get nice log message
[2015-04-14 13:54:08 +0200] INFO -- 200 -- total=2.06 db=0.36 -- GET /your_app/endpoint params={some_param: 12}
If you prefer some other format I strongly encourage you to do pull request with new formatter class ;)
## Logging to file and STDOUT
You can to file and STDOUT at once, you just need to assign new logger
logger Logger.new GrapeLogging::MultiIO.new(STDOUT, File.open('path/to/your/logfile.log'), 'a'))
## Logging exceptions
If you want to log exceptions you can do it like this
class MyAPI < Grape::API
rescue_from :all do |e|
MyAPI.logger.error e
#do here whatever you originally planned to do :)
end
end
## Development
@ -32,8 +68,8 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
## Contributing
1. Fork it ( https://github.com/[my-github-username]/grape_logging/fork )
1. Fork it ( https://github.com/aserafin/grape_logging/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request
5. Create a new Pull Request

View file

@ -1,3 +1,4 @@
require 'grape_logging/multi_io'
require 'grape_logging/version'
require 'grape_logging/formatters/default'
require 'grape_logging/middleware/request_logger'

View file

@ -2,20 +2,30 @@ module GrapeLogging
module Formatters
class Default
def call(severity, datetime, _, data)
"[#{datetime}] #{severity} -- #{data.delete(:status)} -- total=#{data.delete(:total)} db=#{data.delete(:db)} -- #{data.delete(:method)} #{data.delete(:path)} #{format(data)}\n"
"[#{datetime}] #{severity} -- #{format(data)}\n"
end
def format(data)
if data.is_a?(String)
data
elsif data.is_a?(Exception)
[data.to_s, data.backtrace].flatten.join('\n')
format_exception(data)
elsif data.is_a?(Hash)
data.keys.sort.map { |key| "#{key}=#{data[key]}" }.join(' ')
"#{data.delete(:status)} -- total=#{data.delete(:total)} db=#{data.delete(:db)} -- #{data.delete(:method)} #{data.delete(:path)} #{format_hash(data)}"
else
data.inspect
end
end
private
def format_hash(hash)
hash.keys.sort.map { |key| "#{key}=#{hash[key]}" }.join(' ')
end
def format_exception(exception)
backtrace_array = (exception.backtrace || []).map { |line| "\t#{line}" }
"#{exception.message}\n#{backtrace_array.join("\n")}"
end
end
end
end

View file

@ -0,0 +1,15 @@
module GrapeLogging
class MultiIO
def initialize(*targets)
@targets = targets
end
def write(*args)
@targets.each {|t| t.write(*args)}
end
def close
@targets.each(&:close)
end
end
end