├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── Gemfile ├── LICENSE.txt ├── README.md ├── Rakefile ├── lib └── oneline_log_formatter.rb ├── oneline_log_formatter.gemspec └── spec ├── oneline_log_formatter_spec.rb └── spec_helper.rb /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | .bundle 4 | .config 5 | .yardoc 6 | Gemfile.lock 7 | InstalledFiles 8 | _yardoc 9 | coverage 10 | doc/ 11 | lib/bundler/man 12 | pkg 13 | rdoc 14 | spec/reports 15 | test/tmp 16 | test/version_tmp 17 | tmp 18 | example/log 19 | 20 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | rvm: 2 | - 1.9.3 3 | - 2.0.0 4 | - 2.1 5 | - 2.2 6 | gemfile: 7 | - Gemfile 8 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # 0.0.2 (2016/08/17) 2 | 3 | * Call `format_severity` (thanks to @ma2ge) 4 | 5 | # 0.0.1 (2015/04/23) 6 | 7 | First version 8 | 9 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gemspec 4 | 5 | gem 'rspec' 6 | gem 'timecop' 7 | gem 'rake' 8 | gem 'pry' 9 | gem 'pry-nav' 10 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 Naotoshi Seo 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OnelineLogFormatter 2 | 3 | A logger formatter to output each log with in line forcely 4 | 5 | ## What is this for? 6 | 7 | Rails default log formatter outputs backtrace in multiple lines, and it makes difficult to parse the log. 8 | 9 | This log formatter replaces sthe line feed characters `\n` with `\\n` so that log messages will be in one line. 10 | 11 | ## Installation 12 | 13 | Add this line to your application's Gemfile: 14 | 15 | gem 'oneline_log_formatter' 16 | 17 | And then execute: 18 | 19 | $ bundle 20 | 21 | ## How to use 22 | 23 | ```ruby 24 | require 'logger' 25 | require 'oneline_log_formatter' 26 | 27 | logger = Logger.new(STDOUT) 28 | logger.formatter = OnelineLogFormatter.new 29 | logger.info("foo\nbar") 30 | ``` 31 | 32 | which outputs logs like 33 | 34 | ``` 35 | 20150423T00:00:00+09:00 [INFO] foo\nbar 36 | ``` 37 | 38 | Note that the line feed character is converted into `\n`. 39 | 40 | ## Rails 41 | 42 | Configure at `config/application.rb` 43 | 44 | ```ruby 45 | config.logger.formatter = OnelineLogFormatter.new 46 | ``` 47 | 48 | ## ChangeLog 49 | 50 | See [CHANGELOG.md](CHANGELOG.md) for details. 51 | 52 | ## Contributing 53 | 54 | 1. Fork it 55 | 2. Create your feature branch (`git checkout -b my-new-feature`) 56 | 3. Commit your changes (`git commit -am 'Add some feature'`) 57 | 4. Push to the branch (`git push origin my-new-feature`) 58 | 5. Create new [Pull Request](../../pull/new/master) 59 | 60 | ## Copyright 61 | 62 | See [LICENSE.txt](LICENSE.txt) for details. 63 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | 3 | task :default => :test 4 | 5 | task :test do 6 | require 'rspec/core' 7 | require 'rspec/core/rake_task' 8 | RSpec::Core::RakeTask.new(:test) do |spec| 9 | spec.pattern = FileList['spec/**/*_spec.rb'] 10 | end 11 | end 12 | 13 | desc 'Open an irb session preloaded with the gem library' 14 | task :console do 15 | sh 'irb -rubygems -I lib -r strftime_logger' 16 | end 17 | task :c => :console 18 | -------------------------------------------------------------------------------- /lib/oneline_log_formatter.rb: -------------------------------------------------------------------------------- 1 | require 'time' 2 | 3 | class OnelineLogFormatter 4 | FORMAT = "%s [%s] %s\n" 5 | 6 | def initialize(opts={}) 7 | end 8 | 9 | def call(severity, time, progname, msg) 10 | FORMAT % [format_datetime(time), format_severity(severity), format_message(msg)] 11 | end 12 | 13 | private 14 | def format_datetime(time) 15 | time.iso8601 16 | end 17 | 18 | def format_severity(severity) 19 | severity 20 | end 21 | 22 | def format_message(message) 23 | case message 24 | when ::Exception 25 | e = message 26 | "#{e.class} (#{e.message})\\n #{e.backtrace.join("\\n ")}" 27 | else 28 | message.to_s.gsub(/\n/, "\\n") 29 | end 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /oneline_log_formatter.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | 5 | Gem::Specification.new do |gem| 6 | gem.name = "oneline_log_formatter" 7 | gem.version = "0.0.2" 8 | gem.authors = ["Naotoshi Seo"] 9 | gem.email = ["sonots@gmail.com"] 10 | gem.description = %q{A logger formatter to output each log in one line forcely} 11 | gem.summary = %q{A logger formatter to output each log in one line forcely} 12 | gem.homepage = "https://github.com/sonots/oneline_log_formatter" 13 | 14 | gem.files = `git ls-files`.split($/) 15 | gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } 16 | gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) 17 | gem.require_paths = ["lib"] 18 | end 19 | -------------------------------------------------------------------------------- /spec/oneline_log_formatter_spec.rb: -------------------------------------------------------------------------------- 1 | require_relative 'spec_helper' 2 | require 'oneline_log_formatter' 3 | require 'fileutils' 4 | require 'logger' 5 | 6 | describe OnelineLogFormatter do 7 | let(:logger) do 8 | Logger.new("#{log_dir}/test.log").tap {|logger| 9 | logger.formatter = OnelineLogFormatter.new 10 | } 11 | end 12 | let(:log_dir) { "#{File.dirname(__FILE__)}/log" } 13 | let(:now) { Time.now.iso8601 } 14 | 15 | before do 16 | FileUtils.mkdir_p log_dir 17 | Timecop.freeze(Time.now) 18 | end 19 | 20 | after do 21 | FileUtils.rm_rf log_dir 22 | Timecop.return 23 | end 24 | 25 | it :info do 26 | logger.info("test") 27 | begin 28 | raise ArgumentError.new('test') 29 | rescue => e 30 | logger.info(e) 31 | end 32 | File.open("#{log_dir}/test.log") do |f| 33 | f.gets # drop the `# Logfile created on ...` line 34 | expect(f.gets).to eq "#{now} [INFO] test\n" 35 | expect(f.gets).to match(/#{Regexp.escape(now)} \[INFO\] ArgumentError \(test\)\\n.*formatter_spec\.rb/) 36 | end 37 | end 38 | 39 | it :block do 40 | logger.fatal { "test" } 41 | begin 42 | raise ArgumentError.new('test') 43 | rescue => e 44 | logger.fatal { e } 45 | end 46 | File.open("#{log_dir}/test.log") do |f| 47 | f.gets # drop the `# Logfile created on ...` line 48 | expect(f.gets).to eq "#{now} [FATAL] test\n" 49 | expect(f.gets).to match(/#{Regexp.escape(now)} \[FATAL\] ArgumentError \(test\)\\n.*formatter_spec\.rb/) 50 | end 51 | end 52 | end 53 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require 'bundler' 2 | Bundler.setup(:default, :test) 3 | Bundler.require(:default, :test) 4 | 5 | $TESTING=true 6 | $:.unshift File.join(File.dirname(__FILE__), '..', 'lib') 7 | --------------------------------------------------------------------------------