structured_logger 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8997f897e7f961858b3b93d3da6a305767fe834b
4
+ data.tar.gz: 1131e5ddeaafa27f19e201a90d0a785f80a27211
5
+ SHA512:
6
+ metadata.gz: 921f12ce660981d634c9105fb874e4c43023d649d5af5459d637962a0f8031def8ba60444a96b58f961ed60d16c5307cfdc6c9af75dfa1563416bf022eaac8f5
7
+ data.tar.gz: 16194cec17bec2106b3569088a3b5c25dfafa6f119503cb475f7f1b2c2a2984771e18b7a7a3cb1ab0ba620261002e4b900251ca03ddc68fc22a951d36b0a1288
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - ruby-head
4
+ - 2.2
5
+ - 2.1
6
+ - 2.0.0
7
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in structured_logger.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,21 @@
1
+ guard(:bundler) do
2
+ watch(/\.gemspec\z/)
3
+ watch("Gemfile")
4
+ end
5
+
6
+ guard(:test, all_after_pass: true, test_paths: %w(test)) do
7
+ %w(
8
+ Gemfile.lock
9
+ test/test_helper.rb
10
+ ).each do |path|
11
+ watch(path) {
12
+ "test"
13
+ }
14
+ end
15
+
16
+ watch(%r{\Atest/(.*)\.rb\z})
17
+
18
+ watch(%r{\Alib/(.*)\.rb\z}) { |m|
19
+ "test/#{m[1]}_test.rb"
20
+ }
21
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Yuya.Nishida.
2
+
3
+ X11 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,58 @@
1
+ # StructuredLogger
2
+
3
+ A structured logger with Ruby's Logger interface.
4
+
5
+ [![License X11](https://img.shields.io/badge/license-X11-brightgreen.svg)](https://raw.githubusercontent.com/nishidayuya/structured_logger/master/LICENSE.txt)
6
+ [![Dependency Status](https://gemnasium.com/nishidayuya/structured_logger.svg)](https://gemnasium.com/nishidayuya/structured_logger)
7
+ [![Build Status](https://travis-ci.org/nishidayuya/structured_logger.svg?branch=master)](https://travis-ci.org/nishidayuya/structured_logger)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'structured_logger'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install structured_logger
22
+
23
+ ## Usage
24
+
25
+ We can write log with parameters.
26
+
27
+ ```ruby
28
+ t = Time.now; sleep(0.1)
29
+ l = StructuredLogger.new(STDOUT)
30
+
31
+ l.debug("processed request", started_at: t, elapsed_sec: Time.now - t, status: "ok")
32
+ #=> D, [2015-08-21T05:14:37.022621 #8310] DEBUG -- : processed request: started_at=2015-08-21 05:14:36 +0900 elapsed_sec=0.100156444 status="ok"
33
+
34
+ l.debug { ["processed request", started_at: t, elapsed_sec: Time.now - t, status: "ok"] }
35
+ #=> D, [2015-08-21T05:15:00.214480 #8416] DEBUG -- : processed request: started_at=2015-08-21 05:15:00 +0900 elapsed_sec=0.100193648 status="ok"
36
+ ```
37
+
38
+ `StructuredLogger` instance methods have Ruby's `Logger` interface. So, we can replace Ruby's `Logger` to `StructuredLogger`.
39
+
40
+ ```ruby
41
+ l = Logger.new(STDOUT)
42
+ l.error("Something happend")
43
+ #=> E, [2015-08-25T06:43:18.244950 #23623] ERROR -- : Something happend
44
+
45
+ l = StructuredLogger.new(STDOUT)
46
+ l.error("Something happend")
47
+ #=> E, [2015-08-25T06:43:47.798889 #23623] ERROR -- : Something happend
48
+ ```
49
+
50
+ ## Development
51
+
52
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
53
+
54
+ 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`.
55
+
56
+ ## Contributing
57
+
58
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nishidayuya/structured_logger.
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << "test"
6
+ t.test_files = Dir["test/**/*_test.rb"]
7
+ end
8
+
9
+ task(default: %i(test build))
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "structured_logger"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,100 @@
1
+ require "logger"
2
+ require "forwardable"
3
+
4
+ class StructuredLogger
5
+ extend Forwardable
6
+ include Logger::Severity
7
+
8
+ VERSION = "0.0.0"
9
+
10
+ attr_accessor :formatter
11
+
12
+ def self.severity_name(severity)
13
+ return Logger::SEV_LABEL[severity] || "ANY"
14
+ end
15
+
16
+ def initialize(io)
17
+ @logger = Logger.new(io)
18
+ @formatter = Formatter.new
19
+ end
20
+
21
+ def_delegators :@logger,
22
+ :close,
23
+ :level, :level=,
24
+ :debug?, :info?, :warn?, :error?, :fatal?
25
+
26
+ def_delegators :@formatter, :datetime_format, :datetime_format=
27
+
28
+ def debug(*args, &block)
29
+ add(DEBUG, *args, &block)
30
+ end
31
+
32
+ def info(*args, &block)
33
+ add(INFO, *args, &block)
34
+ end
35
+
36
+ def warn(*args, &block)
37
+ add(WARN, *args, &block)
38
+ end
39
+
40
+ def error(*args, &block)
41
+ add(ERROR, *args, &block)
42
+ end
43
+
44
+ def fatal(*args, &block)
45
+ add(FATAL, *args, &block)
46
+ end
47
+
48
+ def add(severity, *args, &block)
49
+ if level > severity
50
+ return
51
+ end
52
+ if block_given?
53
+ *args = yield
54
+ end
55
+ s = @formatter.call(severity, Time.now, *args)
56
+ @logger << s
57
+ end
58
+
59
+ # Default formatter for StructuredLogger.
60
+ class Formatter
61
+ attr_accessor :datetime_format
62
+
63
+ def initialize
64
+ @datetime_format = nil
65
+ end
66
+
67
+ def call(severity, time, message = nil, **options)
68
+ severity_name = StructuredLogger.severity_name(severity)
69
+ return FORMAT % {
70
+ short_severity_name: severity_name[0, 1],
71
+ datetime: format_datetime(time),
72
+ pid: Process.pid,
73
+ severity_name: severity_name,
74
+ program_name: "",
75
+ message: format_message(message, **options),
76
+ }
77
+ end
78
+
79
+ private
80
+
81
+ FORMAT = "%{short_severity_name}, [%{datetime} #%{pid}] %<severity_name>5s -- %{program_name}: %{message}\n"
82
+
83
+ def format_datetime(time)
84
+ time.strftime(@datetime_format || "%Y-%m-%dT%H:%M:%S.%6N".freeze)
85
+ end
86
+
87
+ def format_message(message = nil, **options)
88
+ return [message, format_options(options)].compact.join(": ")
89
+ end
90
+
91
+ def format_options(options)
92
+ if options.empty?
93
+ return nil
94
+ end
95
+ return options.map { |key, value|
96
+ "#{key}=#{value.inspect}"
97
+ }.join(" ")
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "structured_logger"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "structured_logger"
8
+ spec.version = StructuredLogger::VERSION
9
+ spec.authors = ["Yuya.Nishida."]
10
+ spec.email = ["yuya@j96.org"]
11
+ spec.summary = "A structured logger with Ruby's Logger interface."
12
+ spec.homepage = "https://github.com/nishidayuya/structured_logger"
13
+ spec.license = "X11"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.10"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "guard-bundler"
24
+ spec.add_development_dependency "guard-test"
25
+ spec.add_development_dependency "mocha"
26
+ spec.add_development_dependency "pry-byebug"
27
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: structured_logger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Yuya.Nishida.
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-08-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: guard-bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard-test
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: mocha
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description:
98
+ email:
99
+ - yuya@j96.org
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".travis.yml"
106
+ - Gemfile
107
+ - Guardfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - bin/console
112
+ - bin/setup
113
+ - lib/structured_logger.rb
114
+ - structured_logger.gemspec
115
+ homepage: https://github.com/nishidayuya/structured_logger
116
+ licenses:
117
+ - X11
118
+ metadata: {}
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 2.4.5
136
+ signing_key:
137
+ specification_version: 4
138
+ summary: A structured logger with Ruby's Logger interface.
139
+ test_files: []