grape-logging-1.1.2/0000755000175000017500000000000012551534212014447 5ustar terceiroterceirogrape-logging-1.1.2/lib/0000755000175000017500000000000012551534212015215 5ustar terceiroterceirogrape-logging-1.1.2/lib/grape_logging.rb0000644000175000017500000000023512551534212020346 0ustar terceiroterceirorequire 'grape_logging/multi_io' require 'grape_logging/version' require 'grape_logging/formatters/default' require 'grape_logging/middleware/request_logger'grape-logging-1.1.2/lib/grape_logging/0000755000175000017500000000000012551534212020021 5ustar terceiroterceirogrape-logging-1.1.2/lib/grape_logging/formatters/0000755000175000017500000000000012551534212022207 5ustar terceiroterceirogrape-logging-1.1.2/lib/grape_logging/formatters/default.rb0000644000175000017500000000157012551534212024163 0ustar terceiroterceiromodule GrapeLogging module Formatters class Default def call(severity, datetime, _, data) "[#{datetime}] #{severity} -- #{format(data)}\n" end def format(data) if data.is_a?(String) data elsif data.is_a?(Exception) format_exception(data) elsif data.is_a?(Hash) "#{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 endgrape-logging-1.1.2/lib/grape_logging/multi_io.rb0000644000175000017500000000034612551534212022172 0ustar terceiroterceiromodule 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 endgrape-logging-1.1.2/lib/grape_logging/version.rb0000644000175000017500000000005412551534212022032 0ustar terceiroterceiromodule GrapeLogging VERSION = '1.1.2' end grape-logging-1.1.2/lib/grape_logging/middleware/0000755000175000017500000000000012551534212022136 5ustar terceiroterceirogrape-logging-1.1.2/lib/grape_logging/middleware/request_logger.rb0000644000175000017500000000252712551534212025520 0ustar terceiroterceirorequire 'grape/middleware/base' module GrapeLogging module Middleware class RequestLogger < Grape::Middleware::Base def before start_time @db_duration = 0 @subscription = ActiveSupport::Notifications.subscribe('sql.active_record') do |*args| event = ActiveSupport::Notifications::Event.new(*args) @db_duration += event.duration end if defined?(ActiveRecord) end def after stop_time logger.info parameters nil end def call!(env) super ensure ActiveSupport::Notifications.unsubscribe(@subscription) if @subscription end protected def parameters { path: request.path, params: request.params.to_hash, method: request.request_method, total: total_runtime, db: @db_duration.round(2), status: response.status } end private def logger @logger ||= @options[:logger] || Logger.new(STDOUT) end def request @request ||= ::Rack::Request.new(env) end def total_runtime ((stop_time - start_time) * 1000).round(2) end def start_time @start_time ||= Time.now end def stop_time @stop_time ||= Time.now end end end endgrape-logging-1.1.2/bin/0000755000175000017500000000000012551534212015217 5ustar terceiroterceirogrape-logging-1.1.2/bin/setup0000644000175000017500000000016312551534212016302 0ustar terceiroterceiro#!/bin/bash set -euo pipefail IFS=$'\n\t' bundle install # Do any other automated setup that you need to do here grape-logging-1.1.2/bin/console0000644000175000017500000000052212551534212016603 0ustar terceiroterceiro#!/usr/bin/env ruby require 'bundler/setup' require 'grape_logging' # You can add fixtures and/or initialization code here to make experimenting # with your gem easier. You can also use a different console, if you like. # (If you use this, don't forget to add pry to your Gemfile!) # require "pry" # Pry.start require 'irb' IRB.start grape-logging-1.1.2/LICENSE.txt0000644000175000017500000000206312551534212016273 0ustar terceiroterceiroThe MIT License (MIT) Copyright (c) 2015 aserafin 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. grape-logging-1.1.2/.gitignore0000644000175000017500000000012712551534212016437 0ustar terceiroterceiro/.bundle/ /.yardoc /Gemfile.lock /_yardoc/ /coverage/ /doc/ /pkg/ /spec/reports/ /tmp/ grape-logging-1.1.2/README.md0000644000175000017500000000411012551534212015722 0ustar terceiroterceiro# grape_logging [![Code Climate](https://codeclimate.com/github/aserafin/grape_logging/badges/gpa.svg)](https://codeclimate.com/github/aserafin/grape_logging) ## Installation Add this line to your application's Gemfile: gem 'grape_logging' And then execute: $ bundle Or install it yourself as: $ gem install grape_logging ## Basic Usage In your api file (somewhere on the top) logger.formatter = GrapeLogging::Formatters::Default.new use GrapeLogging::Middleware::RequestLogger, { logger: logger } ## Features ### Log Format With the default configuration you will get nice log message [2015-04-16 12:52:12 +0200] INFO -- 200 -- total=2.06 db=0.36 -- PATCH /your_app/endpoint params={"some_param"=>{"value_1"=>"123", "value_2"=>"456"}} 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 the same time, 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 After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment. To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). ## Contributing 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 grape-logging-1.1.2/Gemfile0000644000175000017500000000014212551534212015737 0ustar terceiroterceirosource 'https://rubygems.org' # Specify your gem's dependencies in grape_logging.gemspec gemspec grape-logging-1.1.2/Rakefile0000644000175000017500000000003512551534212016112 0ustar terceiroterceirorequire 'bundler/gem_tasks' grape-logging-1.1.2/grape_logging.gemspec0000644000175000017500000000211312551534212020615 0ustar terceiroterceiro# coding: utf-8 lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'grape_logging/version' Gem::Specification.new do |spec| spec.name = 'grape_logging' spec.version = GrapeLogging::VERSION spec.authors = ['aserafin'] spec.email = ['adrian@softmad.pl'] spec.summary = %q{Out of the box request logging for Grape!} spec.description = %q{This gem provides simple request logging for Grape with just few lines of code you have to put in your project! In return you will get response codes, paths, parameters and more!} spec.homepage = 'http://github.com/aserafin/grape_logging' spec.license = 'MIT' spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } spec.bindir = "exe" spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ['lib'] spec.add_dependency 'grape' spec.add_development_dependency 'bundler', '~> 1.8' spec.add_development_dependency 'rake', '~> 10.0' endgrape-logging-1.1.2/metadata.yml0000644000175000017500000000464512551534212016763 0ustar terceiroterceiro--- !ruby/object:Gem::Specification name: grape_logging version: !ruby/object:Gem::Version version: 1.1.2 platform: ruby authors: - aserafin autorequire: bindir: exe cert_chain: [] date: 2015-07-15 00:00:00.000000000 Z dependencies: - !ruby/object:Gem::Dependency name: grape requirement: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' type: :runtime prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' - !ruby/object:Gem::Dependency name: bundler requirement: !ruby/object:Gem::Requirement requirements: - - "~>" - !ruby/object:Gem::Version version: '1.8' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - "~>" - !ruby/object:Gem::Version version: '1.8' - !ruby/object:Gem::Dependency name: rake requirement: !ruby/object:Gem::Requirement requirements: - - "~>" - !ruby/object:Gem::Version version: '10.0' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - "~>" - !ruby/object:Gem::Version version: '10.0' description: This gem provides simple request logging for Grape with just few lines of code you have to put in your project! In return you will get response codes, paths, parameters and more! email: - adrian@softmad.pl executables: [] extensions: [] extra_rdoc_files: [] files: - ".gitignore" - Gemfile - LICENSE.txt - README.md - Rakefile - bin/console - bin/setup - grape_logging.gemspec - lib/grape_logging.rb - lib/grape_logging/formatters/default.rb - lib/grape_logging/middleware/request_logger.rb - lib/grape_logging/multi_io.rb - lib/grape_logging/version.rb homepage: http://github.com/aserafin/grape_logging licenses: - MIT metadata: {} post_install_message: rdoc_options: [] require_paths: - lib required_ruby_version: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' required_rubygems_version: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' requirements: [] rubyforge_project: rubygems_version: 2.4.5 signing_key: specification_version: 4 summary: Out of the box request logging for Grape! test_files: [] has_rdoc: