ltsv_log_formatter 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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 +85 -0
- data/Rakefile +17 -0
- data/lib/ltsv_log_formatter.rb +37 -0
- data/ltsv_log_formatter.gemspec +18 -0
- data/spec/ltsv_log_formatter_spec.rb +138 -0
- data/spec/spec_helper.rb +6 -0
- metadata +56 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3d4ed56403d40372bd8425a8bdd7a2eae1ef845d
|
4
|
+
data.tar.gz: 0f27394f33bf4263789a4b2fc8bb0a7d53d881a6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8dc326437d262858bc90cc562696d42993fbbee4c4f779e986c006c4fd1a3ca2b12b239e9924c0fed8bdaaf0c5f3968ffda709e8b1c4a00998fa743013bc4f88
|
7
|
+
data.tar.gz: 53367800112c88b2a5aa46cbc27c03f89a554b68b7916cd1140b0d3e410a06b1a9bbb3fb469f560d332a87153d23b0afa9067126c9ae32fd10c0cdf4cabf1fe5
|
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,85 @@
|
|
1
|
+
# LtsvLogFormatter
|
2
|
+
|
3
|
+
A ruby logger formatter to output log in LTSV format
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'ltsv_log_formatter'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
## How to use
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
require 'logger'
|
19
|
+
require 'ltsv_log_formatter'
|
20
|
+
|
21
|
+
logger = Logger.new(STDOUT)
|
22
|
+
logger.formatter = LtsvLogFormatter.new
|
23
|
+
```
|
24
|
+
|
25
|
+
## Rails
|
26
|
+
|
27
|
+
Configure at `config/application.rb`
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
config.logger.formatter = LtsvLogFormatter.new
|
31
|
+
```
|
32
|
+
|
33
|
+
## Example
|
34
|
+
|
35
|
+
Passing a hash parameter:
|
36
|
+
|
37
|
+
```
|
38
|
+
irb> logger.info({foo: "bar"})
|
39
|
+
time:20150423T00:00:00+09:00\tlevel:INFO\tfoo:bar
|
40
|
+
```
|
41
|
+
|
42
|
+
Passing a string parameter: `message` key is used as default
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
irb> logger.info("foo")
|
46
|
+
time:20150423T00:00:00+09:00\tlevel:INFO\tmessage:foo
|
47
|
+
```
|
48
|
+
|
49
|
+
Line feed: Notice that the line feed character `\n` is converted into `\\n`
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
irb> logger.info("foo\nbar")
|
53
|
+
time:20150423T00:00:00+09:00\tlevel:INFO\tmessage:foo\\nbar
|
54
|
+
```
|
55
|
+
|
56
|
+
## Options
|
57
|
+
|
58
|
+
* time_key
|
59
|
+
* Change the key name of the time field. Set `nil` to remove. Default: time
|
60
|
+
* level_key
|
61
|
+
* Change the kay name of the level field. Set `nil` to remove. Default: level
|
62
|
+
* message_key
|
63
|
+
* Change the kay name for the string (not hash) message. Default: message
|
64
|
+
|
65
|
+
Example)
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
logger.formatter = LtsvLogFormatter.new(time_key: "log_time", level_key: nil)
|
69
|
+
```
|
70
|
+
|
71
|
+
## ChangeLog
|
72
|
+
|
73
|
+
See [CHANGELOG.md](CHANGELOG.md) for details.
|
74
|
+
|
75
|
+
## Contributing
|
76
|
+
|
77
|
+
1. Fork it
|
78
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
79
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
80
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
81
|
+
5. Create new [Pull Request](../../pull/new/master)
|
82
|
+
|
83
|
+
## Copyright
|
84
|
+
|
85
|
+
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,37 @@
|
|
1
|
+
require 'time'
|
2
|
+
|
3
|
+
class LtsvLogFormatter
|
4
|
+
# for tests
|
5
|
+
attr_reader :opts
|
6
|
+
|
7
|
+
# @param [Hash] opts
|
8
|
+
# @option opts [String] time_key (default: time)
|
9
|
+
# @option opts [String] level_key (default: level)
|
10
|
+
# @option opts [String] message_key (default: message)
|
11
|
+
def initialize(opts={})
|
12
|
+
@opts = opts.map {|k, v| [k.to_sym, v] }.to_h # symbolize_keys
|
13
|
+
@opts[:time_key] = :time unless @opts.has_key?(:time_key)
|
14
|
+
@opts[:level_key] = :level unless @opts.has_key?(:level_key)
|
15
|
+
@opts[:message_key] ||= :message
|
16
|
+
end
|
17
|
+
|
18
|
+
def call(severity, time, progname, msg)
|
19
|
+
"#{format_time(time)}#{format_severity(severity)}#{format_message(msg)}\n"
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
def format_time(time)
|
24
|
+
"#{@opts[:time_key]}:#{time.iso8601}\t" if @opts[:time_key]
|
25
|
+
end
|
26
|
+
|
27
|
+
def format_severity(severity)
|
28
|
+
"#{@opts[:level_key]}:#{severity}\t" if @opts[:level_key]
|
29
|
+
end
|
30
|
+
|
31
|
+
def format_message(msg)
|
32
|
+
unless msg.is_a?(Hash)
|
33
|
+
msg = { @opts[:message_key] => msg }
|
34
|
+
end
|
35
|
+
msg.map {|k, v| "#{k}:#{v.to_s.gsub(/\n/, "\\n")}" }.join("\t")
|
36
|
+
end
|
37
|
+
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 = "ltsv_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 log in LTSV format}
|
11
|
+
gem.summary = %q{A logger formatter to output log in LTSV format}
|
12
|
+
gem.homepage = "https://github.com/sonots/ltsv_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,138 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
require 'ltsv_log_formatter'
|
3
|
+
require 'fileutils'
|
4
|
+
require 'logger'
|
5
|
+
|
6
|
+
describe LtsvLogFormatter do
|
7
|
+
let(:logger) do
|
8
|
+
Logger.new("#{log_dir}/test.log").tap {|logger|
|
9
|
+
logger.formatter = LtsvLogFormatter.new(opts)
|
10
|
+
}
|
11
|
+
end
|
12
|
+
let(:opts) { {} }
|
13
|
+
let(:log_dir) { "#{File.dirname(__FILE__)}/log" }
|
14
|
+
let(:now) { Time.now.iso8601 }
|
15
|
+
|
16
|
+
before do
|
17
|
+
FileUtils.mkdir_p log_dir
|
18
|
+
Timecop.freeze(Time.now)
|
19
|
+
end
|
20
|
+
|
21
|
+
after do
|
22
|
+
FileUtils.rm_rf log_dir
|
23
|
+
Timecop.return
|
24
|
+
end
|
25
|
+
|
26
|
+
def gets
|
27
|
+
File.open("#{log_dir}/test.log") do |f|
|
28
|
+
f.gets # drop the `# Logfile created on ...` line
|
29
|
+
return f.gets
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe 'options' do
|
34
|
+
context '#initialize' do
|
35
|
+
it 'with default' do
|
36
|
+
formatter = LtsvLogFormatter.new
|
37
|
+
expect(formatter.opts[:time_key]).to eql(:time)
|
38
|
+
expect(formatter.opts[:level_key]).to eql(:level)
|
39
|
+
expect(formatter.opts[:message_key]).to eql(:message)
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'with time_key' do
|
43
|
+
formatter = LtsvLogFormatter.new("time_key" => "foo")
|
44
|
+
expect(formatter.opts[:time_key]).to eql("foo")
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'with level_key' do
|
48
|
+
formatter = LtsvLogFormatter.new("level_key" => "foo")
|
49
|
+
expect(formatter.opts[:level_key]).to eql("foo")
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'with message_key' do
|
53
|
+
formatter = LtsvLogFormatter.new("message_key" => "foo")
|
54
|
+
expect(formatter.opts[:message_key]).to eql("foo")
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
context 'with time_key nil' do
|
59
|
+
let(:opts) { {time_key: nil} }
|
60
|
+
|
61
|
+
it 'should not output a time field' do
|
62
|
+
logger.info("test")
|
63
|
+
expect(gets).to eq "level:INFO\tmessage:test\n"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
context 'with level_key nil' do
|
68
|
+
let(:opts) { {level_key: nil} }
|
69
|
+
|
70
|
+
it 'should not output a level field' do
|
71
|
+
logger.info("test")
|
72
|
+
expect(gets).to eq "time:#{now}\tmessage:test\n"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context 'with message_key nil' do
|
77
|
+
let(:opts) { {message_key: nil} }
|
78
|
+
|
79
|
+
it 'should have no effect' do
|
80
|
+
logger.info("test")
|
81
|
+
expect(gets).to eq "time:#{now}\tlevel:INFO\tmessage:test\n"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
describe 'string param' do
|
87
|
+
context 'with default' do
|
88
|
+
it do
|
89
|
+
logger.info("test")
|
90
|
+
expect(gets).to eq "time:#{now}\tlevel:INFO\tmessage:test\n"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
context 'with message_key' do
|
95
|
+
let(:opts) { {message_key: "foo"} }
|
96
|
+
|
97
|
+
it do
|
98
|
+
logger.info("test")
|
99
|
+
expect(gets).to eq "time:#{now}\tlevel:INFO\tfoo:test\n"
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'with line feed' do
|
104
|
+
logger.info("foo\nbar")
|
105
|
+
expect(gets).to eq "time:#{now}\tlevel:INFO\tmessage:foo\\nbar\n"
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
describe 'hash param' do
|
110
|
+
context 'with default' do
|
111
|
+
it do
|
112
|
+
logger.info(foo: "bar")
|
113
|
+
expect(gets).to eq "time:#{now}\tlevel:INFO\tfoo:bar\n"
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
it 'with line feed' do
|
118
|
+
logger.info(foo: "foo\nbar")
|
119
|
+
expect(gets).to eq "time:#{now}\tlevel:INFO\tfoo:foo\\nbar\n"
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
describe 'block param' do
|
124
|
+
context 'with string' do
|
125
|
+
it do
|
126
|
+
logger.info { "test" }
|
127
|
+
expect(gets).to eq "time:#{now}\tlevel:INFO\tmessage:test\n"
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
context 'with hash' do
|
132
|
+
it do
|
133
|
+
logger.info { {foo: "bar"} }
|
134
|
+
expect(gets).to eq "time:#{now}\tlevel:INFO\tfoo:bar\n"
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ltsv_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-24 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A logger formatter to output log in LTSV format
|
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/ltsv_log_formatter.rb
|
28
|
+
- ltsv_log_formatter.gemspec
|
29
|
+
- spec/ltsv_log_formatter_spec.rb
|
30
|
+
- spec/spec_helper.rb
|
31
|
+
homepage: https://github.com/sonots/ltsv_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 log in LTSV format
|
54
|
+
test_files:
|
55
|
+
- spec/ltsv_log_formatter_spec.rb
|
56
|
+
- spec/spec_helper.rb
|