oneline_log_formatter 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/.travis.yml +7 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +9 -0
- data/LICENSE.txt +22 -0
- data/README.md +60 -0
- data/Rakefile +17 -0
- data/lib/oneline_log_formatter.rb +31 -0
- data/oneline_log_formatter.gemspec +18 -0
- data/spec/oneline_log_formatter_spec.rb +52 -0
- data/spec/spec_helper.rb +6 -0
- metadata +56 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e513826de84a29156f48a88f369763b2779cbb6d
|
4
|
+
data.tar.gz: 6708475f83307566520018434e1bbd95744da533
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 98df061eb86a692cd9dec0958ffcc943404ff3b37a644d3f6740995d2f9eae2a25706ef96632c6bfc81f37193037aa0ead030e0b335696d21e361b55d328cc28
|
7
|
+
data.tar.gz: 50b3ea8899164be8c62df93f6282d7206d97324b5c13a6ab1ca973f336aafefd28d70a54d7b98420071ecb0645b318c1aee3c9b70948527cb7b7ea6a9e28f3e2
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,60 @@
|
|
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
|
+
## Rails
|
39
|
+
|
40
|
+
Configure at `config/application.rb`
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
config.logger.formatter = OnelineLogFormatter.new
|
44
|
+
```
|
45
|
+
|
46
|
+
## ChangeLog
|
47
|
+
|
48
|
+
See [CHANGELOG.md](CHANGELOG.md) for details.
|
49
|
+
|
50
|
+
## Contributing
|
51
|
+
|
52
|
+
1. Fork it
|
53
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
54
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
55
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
56
|
+
5. Create new [Pull Request](../../pull/new/master)
|
57
|
+
|
58
|
+
## Copyright
|
59
|
+
|
60
|
+
See [LICENSE.txt](LICENSE.txt) for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
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
|
@@ -0,0 +1,31 @@
|
|
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), 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
|
@@ -0,0 +1,18 @@
|
|
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.1"
|
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
|
@@ -0,0 +1,52 @@
|
|
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
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: oneline_log_formatter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Naotoshi Seo
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-04-23 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A logger formatter to output each log in one line forcely
|
14
|
+
email:
|
15
|
+
- sonots@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".gitignore"
|
21
|
+
- ".travis.yml"
|
22
|
+
- CHANGELOG.md
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- lib/oneline_log_formatter.rb
|
28
|
+
- oneline_log_formatter.gemspec
|
29
|
+
- spec/oneline_log_formatter_spec.rb
|
30
|
+
- spec/spec_helper.rb
|
31
|
+
homepage: https://github.com/sonots/oneline_log_formatter
|
32
|
+
licenses: []
|
33
|
+
metadata: {}
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
requirements: []
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 2.2.2
|
51
|
+
signing_key:
|
52
|
+
specification_version: 4
|
53
|
+
summary: A logger formatter to output each log in one line forcely
|
54
|
+
test_files:
|
55
|
+
- spec/oneline_log_formatter_spec.rb
|
56
|
+
- spec/spec_helper.rb
|