ltsv-logger 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
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
+ vendor
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format doc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ltsv-logger.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 togo
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.
@@ -0,0 +1,61 @@
1
+ # LTSV::Logger
2
+
3
+ Logger for LTSV(Labeled Tab-separated Values) format.
4
+ See http://ltsv.org/ about LTSV.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'ltsv-logger'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install ltsv-logger
19
+
20
+ ## Usage
21
+
22
+ Use `Hash` instead of `String` message:
23
+
24
+ ```ruby
25
+ # e.g.: rails
26
+ require 'ltsv-logger'
27
+
28
+ LTSV::Logger.open(File.join('log', "event_#{Rails.env}.log"))
29
+ ltsv = { method: "GET", uri: "/foo", status: 200 }
30
+ LTSV.logger.info ltsv # => time:2013-04-02 21:55:05 +0900 method:GET uri:/?max_id=9235&page=2 status:200
31
+
32
+ ```
33
+
34
+ If logging `Hash` like request parameters, serialize `Hash`. e.g.:`Hash.to_json`:
35
+
36
+ ```ruby
37
+ # e.g.: rails
38
+ # params = { max_id: 9235, page: 2, controller: 'foo', action: 'bar' }
39
+
40
+ ltsv = { method: "GET", uri: "/?max_id=9235&page=2", status: 200, params: params.to_json }
41
+ LTSV.logger.info ltsv # => time:2013-04-02 21:55:05 +0900 method:GET uri:/?max_id=9235&page=2 status:200 params:{"max_id":"9235","page":"2","controller":"foo","action":"bar"}
42
+ ```
43
+
44
+ Maybe, to use `Marshal` is not good, because it's difficult to deserialize with other programe launguage.
45
+
46
+ Other way, you can use `String`, as usual:
47
+
48
+ ```ruby
49
+ LTSV.logger.info "This is sample." # => time:2013-04-02 21:55:05 +0900 message:This is sample.
50
+ ```
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
59
+
60
+ ## Copyright
61
+ Copyright © 2013 terut. See LICENSE.txt(MIT-LICENCE) for further details.
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ require "ltsv-logger/version"
2
+
3
+ module LTSV
4
+ module Logger
5
+ require 'ltsv-logger/logger'
6
+ class LTSVLoggerError < StandardError; end
7
+
8
+ class << self
9
+ attr_accessor :logger
10
+
11
+ def open(name)
12
+ self.logger = Logger.new(name)
13
+ end
14
+ end
15
+ end
16
+
17
+ # shortcut method
18
+ class << self
19
+ def logger
20
+ LTSV::Logger.logger
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,24 @@
1
+ require 'logger'
2
+ module LTSV
3
+ module Logger
4
+ class Logger < ::Logger
5
+ def initialize(*args)
6
+ super
7
+ @formatter = LTSVFormatter.new
8
+ end
9
+
10
+ class LTSVFormatter < ::Logger::Formatter
11
+ def call(severity, timestamp, progname, msg)
12
+ raws = ["time:#{timestamp}"]
13
+ case msg
14
+ when Hash
15
+ raws = msg.inject(raws) { |h, (key, value)| h << "#{key}:#{value}"; h }
16
+ when String
17
+ raws << "message:#{msg}"
18
+ end
19
+ "#{raws.join("\t")}\n"
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ module LTSV
2
+ module Logger
3
+ VERSION = "0.0.2"
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ltsv-logger/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "ltsv-logger"
8
+ gem.version = LTSV::Logger::VERSION
9
+ gem.required_ruby_version = '>= 1.9.3'
10
+ gem.authors = ["terut"]
11
+ gem.email = ["terut.dev+github@gmail.com"]
12
+ gem.description = %q{LTSV(Labeled Tab-separated Values) logger}
13
+ gem.summary = %q{LTSV(Labeled Tab-separated Values) logger}
14
+ gem.homepage = "http://terut.github.com/ltsv-logger"
15
+
16
+ gem.add_development_dependency('rake', '~> 10.0.0')
17
+ gem.add_development_dependency('rspec', '~> 2.13.0')
18
+
19
+ gem.files = `git ls-files`.split($/)
20
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
21
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
22
+ gem.require_paths = ["lib"]
23
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe LTSV::Logger::Logger do
5
+ context '#initialize' do
6
+ let(:logger) { LTSV::Logger::Logger.new(RSpec.configuration.test_log_path) }
7
+
8
+ it 'formatter class is LTSVFormatter' do
9
+ logger.formatter.should be_an_instance_of(LTSV::Logger::Logger::LTSVFormatter)
10
+ end
11
+ end
12
+ end
13
+
14
+ describe LTSV::Logger::Logger::LTSVFormatter do
15
+ context '#call' do
16
+ let(:logger) { LTSV::Logger::Logger.new(RSpec.configuration.test_log_path) }
17
+ let(:formatter) { LTSV::Logger::Logger::LTSVFormatter.new }
18
+
19
+ it 'return ltsv when argument is Hash' do
20
+ time = Time.now
21
+ msg = { foo: 'foo1', bar: 'bar1' }
22
+ formatter.call(logger.level, time, 'hoge', msg).should eq("time:#{time}\tfoo:foo1\tbar:bar1\n")
23
+ end
24
+
25
+ it 'return ltsv when argument is String' do
26
+ time = Time.now
27
+ msg = "foo"
28
+ formatter.call(logger.level, time, 'hoge', msg).should eq("time:#{time}\tmessage:foo\n")
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe LTSV do
5
+ context 'shortcut method' do
6
+ context '.logger' do
7
+ before do
8
+ LTSV::Logger.open(RSpec.configuration.test_log_path)
9
+ end
10
+
11
+ it 'module has logger instance' do
12
+ LTSV.logger.should be_an_instance_of(LTSV::Logger::Logger)
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ describe LTSV::Logger do
19
+ context 'logger method' do
20
+ context '.open' do
21
+ before do
22
+ LTSV::Logger.open(RSpec.configuration.test_log_path)
23
+ end
24
+
25
+ it 'module has logger instance' do
26
+ LTSV::Logger.logger.should be_an_instance_of(LTSV::Logger::Logger)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,21 @@
1
+ require 'bundler/setup'
2
+ Bundler.require
3
+ require 'ltsv-logger'
4
+
5
+ RSpec.configure do |config|
6
+ config.treat_symbols_as_metadata_keys_with_true_values = true
7
+ config.run_all_when_everything_filtered = true
8
+ config.filter_run :focus
9
+
10
+ # Run specs in random order to surface order dependencies. If you find an
11
+ # order dependency and want to debug it, you can fix the order by providing
12
+ # the seed, which is printed after each run.
13
+ # --seed 1234
14
+ config.order = 'random'
15
+
16
+ config.add_setting :test_log_path, default: "#{File.expand_path('../', __FILE__)}/support/test.log"
17
+
18
+ config.after do
19
+ File.delete(config.test_log_path) if File.exist?(config.test_log_path)
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ltsv-logger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - terut
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 10.0.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 10.0.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 2.13.0
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 2.13.0
46
+ description: LTSV(Labeled Tab-separated Values) logger
47
+ email:
48
+ - terut.dev+github@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .rspec
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - lib/ltsv-logger.rb
60
+ - lib/ltsv-logger/logger.rb
61
+ - lib/ltsv-logger/version.rb
62
+ - ltsv-logger.gemspec
63
+ - spec/logger_spec.rb
64
+ - spec/ltsv-logger_spec.rb
65
+ - spec/spec_helper.rb
66
+ homepage: http://terut.github.com/ltsv-logger
67
+ licenses: []
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 1.9.3
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ segments:
85
+ - 0
86
+ hash: -3976200979359774974
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 1.8.24
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: LTSV(Labeled Tab-separated Values) logger
93
+ test_files:
94
+ - spec/logger_spec.rb
95
+ - spec/ltsv-logger_spec.rb
96
+ - spec/spec_helper.rb