log15 1.0.0
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 +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +39 -0
- data/Rakefile +2 -0
- data/lib/log15.rb +6 -0
- data/lib/log15/logger.rb +77 -0
- data/lib/log15/version.rb +3 -0
- data/log15.gemspec +26 -0
- data/spec/log15/logger_spec.rb +82 -0
- metadata +112 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 15778719187c022926549eebe3a808ee0374f8d7
|
4
|
+
data.tar.gz: e90453d30af6fe2a61a58ba9d11e9b70ddf075c1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ba82e4bc74b888d64db5aed928be551621c6d656857a89db9a43586375bb0afc09f129514c5567e30b6195fc6a055e69c0df7a575ec6ad012076cede00207a26
|
7
|
+
data.tar.gz: e25c7d4e3fc23d8bc812cb7046c0514f2e34d1e26a0724e1615d6af07457b2c3bd969a4ec218acf8196ed2d0b58c52247b341a438bb4302a371cca3eeea4fa29
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Ryan Bigg
|
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,39 @@
|
|
1
|
+
# Log15
|
2
|
+
|
3
|
+

|
4
|
+
|
5
|
+
This library was inspired by [inconshreveable/log15](https://github.com/inconshreveable/log15).
|
6
|
+
|
7
|
+
`log15` is a structured logger, outputting a message followed by key/value pairs containing more information about that particular line. This is useful if you're piping your logs to something like Kibana and want to make processing them simpler.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'log15'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install log15
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
# Create a new log15 logger
|
29
|
+
logger = Log15::Logger.default
|
30
|
+
logger.info("Test message", foo: "bar")
|
31
|
+
```
|
32
|
+
|
33
|
+
This will output something like:
|
34
|
+
|
35
|
+
```
|
36
|
+
INFO[11-26|16:27:09] Test message foo="bar"\n"
|
37
|
+
```
|
38
|
+
|
39
|
+
Other log levels are also supported: `debug`, `warn`, and `error`.
|
data/Rakefile
ADDED
data/lib/log15.rb
ADDED
data/lib/log15/logger.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'logger'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module Log15
|
5
|
+
class Logger
|
6
|
+
attr_accessor :logger
|
7
|
+
def initialize(logger=Rails.logger)
|
8
|
+
@logger = logger.dup
|
9
|
+
@logger.formatter = proc do |severity, datetime, progname, msg|
|
10
|
+
date = "#{datetime.month}-#{datetime.day}"
|
11
|
+
time = "#{pad(datetime.hour)}:#{pad(datetime.min)}:#{pad(datetime.sec)}"
|
12
|
+
severities = { "ERROR" => "EROR", "DEBUG" => "DBUG" }
|
13
|
+
severity = severities.fetch(severity, severity)
|
14
|
+
"#{severity}[#{date}|#{time}] #{msg}\n"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.default
|
19
|
+
@logger ||= Log15.new
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.sanitize(params, key, opts={})
|
23
|
+
if params[key]
|
24
|
+
raise SanitizationError, "expected #{key} to not be blank" if params[key] == ""
|
25
|
+
if opts[:expected_length]
|
26
|
+
minimum_size = opts[:expected_length].first
|
27
|
+
maximum_size = opts[:expected_length].last
|
28
|
+
length = params[key].length
|
29
|
+
if length < minimum_size || length > maximum_size
|
30
|
+
raise SanitizationError, "expected access_token to be between #{minimum_size} and #{maximum_size} characters long (is #{length})"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
params[key] = params[key][0..5] + "****" + params[key][-6..-1]
|
34
|
+
else
|
35
|
+
raise SanitizationError, "expected #{key} to be present"
|
36
|
+
end
|
37
|
+
params
|
38
|
+
end
|
39
|
+
|
40
|
+
def debug(msg, data={})
|
41
|
+
logger.debug(process(msg, data))
|
42
|
+
end
|
43
|
+
|
44
|
+
def info(msg, data={})
|
45
|
+
logger.info(process(msg, data))
|
46
|
+
end
|
47
|
+
|
48
|
+
def warn(msg, data={})
|
49
|
+
logger.warn(process(msg, data))
|
50
|
+
end
|
51
|
+
|
52
|
+
def error(msg, data={})
|
53
|
+
logger.error(process(msg, data))
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def process(msg, data)
|
59
|
+
output = ["#{msg}"]
|
60
|
+
if data.keys.count > 0
|
61
|
+
data.each do |k, v|
|
62
|
+
if v.is_a?(Array) || v.is_a?(Hash)
|
63
|
+
output << ["#{k}=#{JSON.dump(v).inspect}"]
|
64
|
+
else
|
65
|
+
output << ["#{k}=#{v.inspect}"]
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
output.join(" ")
|
71
|
+
end
|
72
|
+
|
73
|
+
def pad(number)
|
74
|
+
number.to_s.rjust(2, '0')
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
data/log15.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'log15/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "log15"
|
8
|
+
spec.version = Log15::VERSION
|
9
|
+
spec.authors = ["Ryan Bigg"]
|
10
|
+
spec.email = ["radar@lifx.co"]
|
11
|
+
spec.summary = %q{Structured logging.}
|
12
|
+
spec.description = %q{Structured logging.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "json"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.1"
|
26
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'log15'
|
2
|
+
|
3
|
+
describe Log15 do
|
4
|
+
let(:log) { StringIO.new }
|
5
|
+
let(:logger) { Logger.new(log) }
|
6
|
+
subject { Log15::Logger.new(logger) }
|
7
|
+
|
8
|
+
context "info" do
|
9
|
+
it "simple message" do
|
10
|
+
subject.info("msg", foo: "bar", baz: "foo")
|
11
|
+
output = log.string
|
12
|
+
expect(output).to match(/^INFO/)
|
13
|
+
expect(output).to match(/\[\d{2}-\d{2}|\d{2}:\d{2}:\d{2}\]/)
|
14
|
+
expect(output).to match(/msg foo="bar" baz="foo"\n$/)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "message with quotes" do
|
18
|
+
subject.info("msg", foo: "\"bar\"")
|
19
|
+
output = log.string
|
20
|
+
expect(output).to match(/^INFO/)
|
21
|
+
expect(output).to match(/\[\d{2}-\d{2}|\d{2}:\d{2}:\d{2}\]/)
|
22
|
+
expect(output).to match(/msg foo="\\\"bar\\\""\n$/)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "message with nested hash" do
|
26
|
+
subject.info("msg", foo: { bar: "baz"})
|
27
|
+
output = log.string
|
28
|
+
expect(output).to match(/^INFO/)
|
29
|
+
expect(output).to match(/\[\d{2}-\d{2}|\d{2}:\d{2}:\d{2}\]/)
|
30
|
+
expect(output).to match(/msg foo="{\\\"bar\\\":\\\"baz\\\"}"\n$/)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "message with nested array" do
|
34
|
+
subject.info("msg", foo: ["bar", "baz"])
|
35
|
+
output = log.string
|
36
|
+
expect(output).to match(/^INFO/)
|
37
|
+
expect(output).to match(/\[\d{2}-\d{2}|\d{2}:\d{2}:\d{2}\]/)
|
38
|
+
expect(output).to match(/msg foo="\[\\\"bar\\\",\\\"baz\\\"\]"\n$/)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context "debug" do
|
43
|
+
it "simple message" do
|
44
|
+
subject.debug("msg", foo: "bar")
|
45
|
+
output = log.string
|
46
|
+
expect(output).to match(/^DBUG/)
|
47
|
+
expect(output).to match(/\[\d{2}-\d{2}|\d{2}:\d{2}:\d{2}\]/)
|
48
|
+
expect(output).to match(/msg foo="bar"\n$/)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context "sanitisation" do
|
53
|
+
it "sanitises a key" do
|
54
|
+
params = { "access_token" => "123456789012345678901234567890" }
|
55
|
+
Log15::Logger.sanitize(params, "access_token")
|
56
|
+
expect(params["access_token"]).to eq("123456****567890")
|
57
|
+
end
|
58
|
+
|
59
|
+
it "raises an error if key is not within a range" do
|
60
|
+
params = { "access_token" => "onetwo" }
|
61
|
+
expect do
|
62
|
+
Log15::Logger.sanitize(params, "access_token", expected_length: 8..24)
|
63
|
+
end.to raise_error(Log15::SanitizationError, "expected access_token to be between 8 and 24 characters long (is 6)")
|
64
|
+
end
|
65
|
+
|
66
|
+
it "doesn't sanitize a key if it doesn't exist" do
|
67
|
+
params = { "not_access_token" => "incognito" }
|
68
|
+
expect do
|
69
|
+
Log15::Logger.sanitize(params, "access_token")
|
70
|
+
end.to raise_error(Log15::SanitizationError, "expected access_token to be present")
|
71
|
+
expect(params).to eq(params)
|
72
|
+
end
|
73
|
+
|
74
|
+
it "doesn't sanitize a key if it is blank" do
|
75
|
+
params = { "access_token" => "" }
|
76
|
+
expect do
|
77
|
+
Log15::Logger.sanitize(params, "access_token")
|
78
|
+
end.to raise_error(Log15::SanitizationError, "expected access_token to not be blank")
|
79
|
+
expect(params).to eq(params)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: log15
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ryan Bigg
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: json
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.7'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.1'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.1'
|
69
|
+
description: Structured logging.
|
70
|
+
email:
|
71
|
+
- radar@lifx.co
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- lib/log15.rb
|
82
|
+
- lib/log15/logger.rb
|
83
|
+
- lib/log15/version.rb
|
84
|
+
- log15.gemspec
|
85
|
+
- spec/log15/logger_spec.rb
|
86
|
+
homepage: ''
|
87
|
+
licenses:
|
88
|
+
- MIT
|
89
|
+
metadata: {}
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 2.2.2
|
107
|
+
signing_key:
|
108
|
+
specification_version: 4
|
109
|
+
summary: Structured logging.
|
110
|
+
test_files:
|
111
|
+
- spec/log15/logger_spec.rb
|
112
|
+
has_rdoc:
|