hedgelog 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.rubocop.yml +53 -0
- data/.simplecov +3 -0
- data/.travis.yml +7 -0
- data/CONTRIBUTING.md +29 -0
- data/Gemfile +18 -0
- data/Guardfile +70 -0
- data/LICENSE.md +20 -0
- data/README.md +84 -0
- data/Rakefile +6 -0
- data/bin/console +10 -0
- data/bin/profile +21 -0
- data/bin/setup +7 -0
- data/config/pre_commit.yml +9 -0
- data/hedgelog.gemspec +25 -0
- data/lib/hedgelog/context.rb +64 -0
- data/lib/hedgelog/scrub_replacement.rb +39 -0
- data/lib/hedgelog/scrubber.rb +20 -0
- data/lib/hedgelog/version.rb +3 -0
- data/lib/hedgelog.rb +167 -0
- metadata +93 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ODBjYWJhZDM5N2Q0Mzg0Y2QwZWVlZGMwMDg1ZTc1YWY0ZjgxZGJlNg==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NTZmZTYyNzRmYTNkNTExZDEyMDBlYzcxMzljZmEyNjY4MWRmMjdiYQ==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
M2Y0NzU1YjVmMzU4NmEzMGRmMmY0MzgzOGVmOTEyYTcwM2MxNGEzNDdlMThi
|
10
|
+
M2IwOTI2MjdmYmFlMTlhODgxNjNiYjE2MzQzMzRkMzNmZjRkYjM3NmUzNDBi
|
11
|
+
OGJkZDgxNWRiODY4NmMwOTUyYjk5YmJmZmIwMjczMjRhOTRmYmI=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
NjM0MTUxZmQxYzMxY2Y2ZjNmM2I5NGY2NjI0ZTQ1ZDM3NjQ4MTQ3ODQ1OGRl
|
14
|
+
ZTFhZDlkOGQyNDQ1YzkwZWIzNTc1ZWQwYjlhNDAxNjliOTQ1NDYxYWRiNGQ0
|
15
|
+
NDgwZDJjNTU1NGVlYzUyN2YwYTU5MmJmODdiNTA0NTMxZTkzMTE=
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# This is the configuration used to check the rubocop source code.
|
2
|
+
inherit_from: #{RUBOCOP_HOME}/config/default.yml
|
3
|
+
|
4
|
+
AllCops:
|
5
|
+
Include:
|
6
|
+
- '**/lib/**/*'
|
7
|
+
Exclude:
|
8
|
+
- 'bin/**/*'
|
9
|
+
|
10
|
+
CyclomaticComplexity:
|
11
|
+
Max: 15
|
12
|
+
|
13
|
+
PerceivedComplexity:
|
14
|
+
Max: 20
|
15
|
+
|
16
|
+
MethodLength:
|
17
|
+
Max: 30
|
18
|
+
|
19
|
+
ClassLength:
|
20
|
+
Enabled: false
|
21
|
+
|
22
|
+
LineLength:
|
23
|
+
Max: 150
|
24
|
+
|
25
|
+
WordArray:
|
26
|
+
Enabled: false
|
27
|
+
|
28
|
+
LeadingCommentSpace:
|
29
|
+
Enabled: false
|
30
|
+
|
31
|
+
CommentAnnotation:
|
32
|
+
Enabled: false
|
33
|
+
|
34
|
+
Documentation:
|
35
|
+
Enabled: false
|
36
|
+
|
37
|
+
Next:
|
38
|
+
Enabled: false
|
39
|
+
|
40
|
+
OptionalArguments:
|
41
|
+
Enabled: false
|
42
|
+
|
43
|
+
SpaceInsideHashLiteralBraces:
|
44
|
+
EnforcedStyle: no_space
|
45
|
+
|
46
|
+
SignalException:
|
47
|
+
EnforcedStyle: only_raise
|
48
|
+
|
49
|
+
TrivialAccessors:
|
50
|
+
AllowPredicates: true
|
51
|
+
|
52
|
+
BlockNesting:
|
53
|
+
Max: 4
|
data/.simplecov
ADDED
data/.travis.yml
ADDED
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Contributing
|
2
|
+
|
3
|
+
Pull requests are always appreciated.
|
4
|
+
|
5
|
+
To get started contributing, fork and clone the repo:
|
6
|
+
|
7
|
+
```
|
8
|
+
git clone git@github.com:your-username/hedgelog.git
|
9
|
+
```
|
10
|
+
|
11
|
+
Install development Gems:
|
12
|
+
|
13
|
+
```
|
14
|
+
bundle install
|
15
|
+
```
|
16
|
+
|
17
|
+
Make your code changes.
|
18
|
+
|
19
|
+
Make sure that your changes do not break any existing tests:
|
20
|
+
|
21
|
+
```
|
22
|
+
rspec
|
23
|
+
```
|
24
|
+
|
25
|
+
Pull requests **will not be accepted** for any additional functionality without adding accompanying tests. Moreover, the repository contains a configuration for [Rubocop](https://github.com/bbatsov/rubocop). Pull requests will also not be accepted that do not pass the rubocop style-check. To check this simply run:
|
26
|
+
|
27
|
+
```
|
28
|
+
rubocop
|
29
|
+
```
|
data/Gemfile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in hedgelog.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
group :test, :development do
|
7
|
+
gem "rake", "~> 10.0"
|
8
|
+
gem "rspec", "~> 3.3"
|
9
|
+
gem "pry"
|
10
|
+
gem "simplecov", "~> 0.10"
|
11
|
+
gem "rubocop", "~> 0.33"
|
12
|
+
gem "timecop"
|
13
|
+
end
|
14
|
+
|
15
|
+
group :development do
|
16
|
+
gem 'ruby-prof'
|
17
|
+
gem 'guard-rspec', require: false
|
18
|
+
end
|
data/Guardfile
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
## Uncomment and set this to only include directories you want to watch
|
5
|
+
# directories %w(app lib config test spec features) \
|
6
|
+
# .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
|
7
|
+
|
8
|
+
## Note: if you are using the `directories` clause above and you are not
|
9
|
+
## watching the project directory ('.'), then you will want to move
|
10
|
+
## the Guardfile to a watched dir and symlink it back, e.g.
|
11
|
+
#
|
12
|
+
# $ mkdir config
|
13
|
+
# $ mv Guardfile config/
|
14
|
+
# $ ln -s config/Guardfile .
|
15
|
+
#
|
16
|
+
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"
|
17
|
+
|
18
|
+
# Note: The cmd option is now required due to the increasing number of ways
|
19
|
+
# rspec may be run, below are examples of the most common uses.
|
20
|
+
# * bundler: 'bundle exec rspec'
|
21
|
+
# * bundler binstubs: 'bin/rspec'
|
22
|
+
# * spring: 'bin/rspec' (This will use spring if running and you have
|
23
|
+
# installed the spring binstubs per the docs)
|
24
|
+
# * zeus: 'zeus rspec' (requires the server to be started separately)
|
25
|
+
# * 'just' rspec: 'rspec'
|
26
|
+
|
27
|
+
guard :rspec, cmd: "bundle exec rspec --format progress" do
|
28
|
+
require "guard/rspec/dsl"
|
29
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
30
|
+
|
31
|
+
# Feel free to open issues for suggestions and improvements
|
32
|
+
|
33
|
+
# RSpec files
|
34
|
+
rspec = dsl.rspec
|
35
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
36
|
+
watch(rspec.spec_support) { rspec.spec_dir }
|
37
|
+
watch(rspec.spec_files)
|
38
|
+
|
39
|
+
# Ruby files
|
40
|
+
ruby = dsl.ruby
|
41
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
42
|
+
|
43
|
+
# Rails files
|
44
|
+
rails = dsl.rails(view_extensions: %w(erb haml slim))
|
45
|
+
dsl.watch_spec_files_for(rails.app_files)
|
46
|
+
dsl.watch_spec_files_for(rails.views)
|
47
|
+
|
48
|
+
watch(rails.controllers) do |m|
|
49
|
+
[
|
50
|
+
rspec.spec.("routing/#{m[1]}_routing"),
|
51
|
+
rspec.spec.("controllers/#{m[1]}_controller"),
|
52
|
+
rspec.spec.("acceptance/#{m[1]}")
|
53
|
+
]
|
54
|
+
end
|
55
|
+
|
56
|
+
# Rails config changes
|
57
|
+
watch(rails.spec_helper) { rspec.spec_dir }
|
58
|
+
watch(rails.routes) { "#{rspec.spec_dir}/routing" }
|
59
|
+
watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }
|
60
|
+
|
61
|
+
# Capybara features specs
|
62
|
+
watch(rails.view_dirs) { |m| rspec.spec.("features/#{m[1]}") }
|
63
|
+
watch(rails.layouts) { |m| rspec.spec.("features/#{m[1]}") }
|
64
|
+
|
65
|
+
# Turnip features and steps
|
66
|
+
watch(%r{^spec/acceptance/(.+)\.feature$})
|
67
|
+
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
|
68
|
+
Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
|
69
|
+
end
|
70
|
+
end
|
data/LICENSE.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2015 Firespring
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Hedgelog
|
2
|
+
|
3
|
+
This gem provides an opinionated Ruby logger for writing structured JSON logs. It attempts to maintain compatibility with the standard Ruby logger api while extending it with functionality relevant to JSON logging.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'hedgelog'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install hedgelog
|
20
|
+
|
21
|
+
## Basic Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require 'hedgelog'
|
25
|
+
|
26
|
+
# Logging defaults to STDOUT or you can pass a file path to .new
|
27
|
+
logger = Hedgelog::Channel.new
|
28
|
+
|
29
|
+
logger.level = :info
|
30
|
+
|
31
|
+
logger.debug "Foo"
|
32
|
+
# No Output
|
33
|
+
|
34
|
+
logger.info "Foo"
|
35
|
+
=> {"message":"FOO","timestamp":"2015-07-15T12:03:08.257356","level_name":"info","level":1}
|
36
|
+
|
37
|
+
logger.info "FOO", sample: 'data'
|
38
|
+
=> {"context": {"sample":"data"},"message":"FOO","timestamp":"2015-07-15T12:05:02.302202","level_name":"info","level":1}
|
39
|
+
|
40
|
+
# It also supports logging as a block with extra data
|
41
|
+
logger.info { ["FOO", {sample: 'data'}] }
|
42
|
+
=> {"context", {"sample":"data"},"message":"FOO","timestamp":"2015-07-15T12:06:20.026807","level_name":"info","level":1}
|
43
|
+
```
|
44
|
+
|
45
|
+
## Context
|
46
|
+
|
47
|
+
Hedgelog allows adding additional context to an instance of a logger that will get output with each log message
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
logger[:request_id] = 1234
|
51
|
+
=> 1234
|
52
|
+
logger.info "FOO"
|
53
|
+
=> {"request_id":1234,"message":"FOO","timestamp":"2015-07-15T12:09:33.129984","level_name":"info","level":1}
|
54
|
+
```
|
55
|
+
|
56
|
+
## Sub-channels
|
57
|
+
|
58
|
+
One of the primary features of Hedgelog is the usage of sub-channels.
|
59
|
+
|
60
|
+
Sub-channels can have their own context separate from the main loggers context. This allows including additional information for all log messages from a portion of your application.
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
subchannel = logger.subchannel(:database)
|
64
|
+
subchannel.info "FOO"
|
65
|
+
=> {"subchannel":"database","message":"FOO","timestamp":"2015-07-15T12:12:39.147210","level_name":"info","level":1}
|
66
|
+
|
67
|
+
# The subchannel does not effect the primary instance of the logger
|
68
|
+
logger.info "FOO"
|
69
|
+
=>{"message":"FOO","timestamp":"2015-07-15T12:13:31.132059","level_name":"info","level":1}
|
70
|
+
```
|
71
|
+
|
72
|
+
The sub-channel instances conform to the same interface as Hedgelog. Therefore they can be passed in as standard Ruby loggers to gems that take an instance of Ruby logger for input.
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
DataMapper.logger = logger.subchannel(:database)
|
76
|
+
```
|
77
|
+
|
78
|
+
## Scrubbing
|
79
|
+
|
80
|
+
## Contributing
|
81
|
+
|
82
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/firespring/hedgelog.
|
83
|
+
|
84
|
+
For details on the pull request process please see our [contributing documentation](CONTRIBUTING.md)
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/profile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "hedgelog"
|
5
|
+
require 'ruby-prof'
|
6
|
+
require 'logger'
|
7
|
+
|
8
|
+
logger = Hedgelog.new('/dev/null')
|
9
|
+
logger.level = Logger::INFO
|
10
|
+
|
11
|
+
RubyProf.start
|
12
|
+
10000.times do
|
13
|
+
logger.info 'Foo'
|
14
|
+
end
|
15
|
+
result = RubyProf.stop
|
16
|
+
result.eliminate_methods!([/Integer#times/])
|
17
|
+
|
18
|
+
|
19
|
+
# printer = RubyProf::GraphPrinter.new(result)
|
20
|
+
printer = RubyProf::FlatPrinterWithLineNumbers.new(result)
|
21
|
+
printer.print(STDOUT)
|
data/bin/setup
ADDED
data/hedgelog.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'hedgelog/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "hedgelog"
|
8
|
+
spec.version = Hedgelog::VERSION
|
9
|
+
spec.licenses = ['MIT']
|
10
|
+
spec.authors = ["Jeff Utter"]
|
11
|
+
spec.email = ["jeff.utter@firespring.com"]
|
12
|
+
spec.homepage = 'https://github.com/firespring/hedgelog'
|
13
|
+
|
14
|
+
spec.summary = "An opinionated/strucutred JSON logger for Ruby"
|
15
|
+
spec.description = "An opinionated/structured JSON logger for Ruby"
|
16
|
+
spec.homepage = "http://firespring.com"
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_dependency "yajl-ruby", "~> 1.2"
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
25
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'hedgelog/scrubber'
|
2
|
+
|
3
|
+
class Hedgelog
|
4
|
+
class Context
|
5
|
+
def initialize(scrubber, data = {})
|
6
|
+
raise ::ArgumentError, "#{self.class}: argument must be Hash got #{data.class}." unless data.is_a? Hash
|
7
|
+
check_reserved_keys(data)
|
8
|
+
@data = data
|
9
|
+
@scrubber = scrubber
|
10
|
+
end
|
11
|
+
|
12
|
+
def []=(key, val)
|
13
|
+
raise ::ArgumentError, "#{self.class}: The #{key} is a reserved key and cannot be used." if Hedgelog::RESERVED_KEYS.include? key.to_sym
|
14
|
+
|
15
|
+
@data[key] = val
|
16
|
+
end
|
17
|
+
|
18
|
+
def [](key)
|
19
|
+
@data[key]
|
20
|
+
end
|
21
|
+
|
22
|
+
def delete(key)
|
23
|
+
@data.delete(key)
|
24
|
+
end
|
25
|
+
|
26
|
+
def clear
|
27
|
+
@data = {}
|
28
|
+
end
|
29
|
+
|
30
|
+
def merge(hash)
|
31
|
+
@data.merge(hash)
|
32
|
+
end
|
33
|
+
|
34
|
+
def merge!(hash_or_context)
|
35
|
+
check_reserved_keys(hash_or_context) unless hash_or_context.is_a? Hedgelog::Context
|
36
|
+
|
37
|
+
hash_or_context = hash_or_context.to_h if hash_or_context.respond_to?(:to_h)
|
38
|
+
@data = hash_or_context.merge(@data)
|
39
|
+
end
|
40
|
+
|
41
|
+
def overwrite!(hash_or_context)
|
42
|
+
check_reserved_keys(hash_or_context) unless hash_or_context.is_a? Hedgelog::Context
|
43
|
+
|
44
|
+
hash_or_context = hash_or_context.to_h if hash_or_context.respond_to?(:to_h)
|
45
|
+
@data.merge!(hash_or_context)
|
46
|
+
end
|
47
|
+
|
48
|
+
def scrub!
|
49
|
+
@data = @scrubber.scrub(@data)
|
50
|
+
self
|
51
|
+
end
|
52
|
+
|
53
|
+
def to_h
|
54
|
+
@data
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def check_reserved_keys(hash)
|
60
|
+
invalid_keys = Hedgelog::RESERVED_KEYS & hash.keys
|
61
|
+
raise ::ArgumentError, "#{self.class}: The following keys are reserved and cannot be used #{invalid_keys.to_a}." if invalid_keys.length > 0
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
class Hedgelog
|
2
|
+
class ScrubReplacement
|
3
|
+
def initialize(key, replacement)
|
4
|
+
@key = key
|
5
|
+
@replacement = replacement
|
6
|
+
@match_regex = /("?)#{@key}("?[=:]\s*"?)(.+?)(["&,;\s]|$)/
|
7
|
+
end
|
8
|
+
|
9
|
+
def scrub_string(string)
|
10
|
+
string.gsub!(@match_regex) do
|
11
|
+
start = Regexp.last_match[1]
|
12
|
+
eql = Regexp.last_match[2]
|
13
|
+
delim = Regexp.last_match[4]
|
14
|
+
"#{start}#{@key}#{eql}#{@replacement}#{delim}"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def scrub_hash(hash)
|
19
|
+
hash.each do |key, val|
|
20
|
+
next hash[key] = @replacement if key.to_s.downcase == @key.to_s.downcase
|
21
|
+
scrub_thing(val)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def scrub_array(array)
|
26
|
+
array.each do |val|
|
27
|
+
scrub_thing(val)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def scrub_thing(thing)
|
34
|
+
scrub_string(thing) if thing.is_a?(String)
|
35
|
+
scrub_array(thing) if thing.is_a?(Array)
|
36
|
+
scrub_hash(thing) if thing.is_a?(Hash)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'hedgelog/scrub_replacement'
|
2
|
+
|
3
|
+
class Hedgelog
|
4
|
+
class Scrubber
|
5
|
+
def initialize(replacements = nil)
|
6
|
+
@replacements = replacements || [
|
7
|
+
ScrubReplacement.new('pasword', '**********')
|
8
|
+
]
|
9
|
+
end
|
10
|
+
|
11
|
+
def scrub(data)
|
12
|
+
# Need to Marshal.dump/Marshal.load to deep copy the input so that scrubbing doesn't change global state
|
13
|
+
d = Marshal.load(Marshal.dump(data))
|
14
|
+
@replacements.each do |r|
|
15
|
+
r.scrub_hash(d)
|
16
|
+
end
|
17
|
+
d
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/hedgelog.rb
ADDED
@@ -0,0 +1,167 @@
|
|
1
|
+
require 'hedgelog/version'
|
2
|
+
require 'hedgelog/context'
|
3
|
+
require 'hedgelog/scrubber'
|
4
|
+
require 'logger'
|
5
|
+
require 'yajl'
|
6
|
+
|
7
|
+
class Hedgelog
|
8
|
+
LEVELS = %w(DEBUG INFO WARN ERROR FATAL UNKNOWN).each_with_object({}).with_index do |(v, h), i|
|
9
|
+
h[v] = i
|
10
|
+
h[v.downcase] = i
|
11
|
+
h[v.to_sym] = i
|
12
|
+
h[v.downcase.to_sym] = i
|
13
|
+
h[i] = v.downcase.to_sym
|
14
|
+
end.freeze
|
15
|
+
|
16
|
+
TOP_LEVEL_KEYS = [:app, :channel, :level, :level_name, :message, :request_id, :timestamp]
|
17
|
+
RESERVED_KEYS = [:app, :level, :level_name, :timestamp, :context, :caller]
|
18
|
+
|
19
|
+
TIMESTAMP_FORMAT = '%Y-%m-%dT%H:%M:%S.%6N'.freeze
|
20
|
+
BACKTRACE_RE = /([^:]+):([0-9]+)(?::in `(.*)')?/
|
21
|
+
|
22
|
+
def initialize(logdev = STDOUT, shift_age = nil, shift_size = nil)
|
23
|
+
@channel_context = Hedgelog::Context.new(@scrubber)
|
24
|
+
@level = LEVELS[:debug]
|
25
|
+
@channel = nil
|
26
|
+
@logdev = nil
|
27
|
+
@app = nil
|
28
|
+
@scrubber = Hedgelog::Scrubber.new
|
29
|
+
|
30
|
+
if logdev.is_a?(self.class)
|
31
|
+
@channel = logdev
|
32
|
+
else
|
33
|
+
@logdev = Logger::LogDevice.new(logdev, shift_age: shift_age, shift_size: shift_size)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
attr_reader :level
|
38
|
+
|
39
|
+
def level=(level)
|
40
|
+
int_level = level_to_int(level)
|
41
|
+
raise ::ArgumentError, "#{self.class}#level= , #{level} is not a valid level." if int_level.nil?
|
42
|
+
@level = int_level
|
43
|
+
end
|
44
|
+
|
45
|
+
attr_writer :app
|
46
|
+
|
47
|
+
def add(severity = LEVELS[:unknown], message = nil, progname = nil, context, &block)
|
48
|
+
return true if (@logdev.nil? && @channel.nil?) || severity < @level
|
49
|
+
|
50
|
+
message, context = *block.call if block
|
51
|
+
context ||= {}
|
52
|
+
|
53
|
+
context = Hedgelog::Context.new(@scrubber, context) unless context.is_a? Hedgelog::Context
|
54
|
+
context.merge!(@channel_context)
|
55
|
+
context[:message] ||= message
|
56
|
+
|
57
|
+
return write(severity, context) if @logdev
|
58
|
+
|
59
|
+
@channel.add(severity, nil, progname, context) if @channel
|
60
|
+
end
|
61
|
+
|
62
|
+
def []=(key, val)
|
63
|
+
@channel_context[key] = val
|
64
|
+
end
|
65
|
+
|
66
|
+
def [](key)
|
67
|
+
@channel_context[key]
|
68
|
+
end
|
69
|
+
|
70
|
+
def delete(key)
|
71
|
+
@channel_context.delete(key)
|
72
|
+
end
|
73
|
+
|
74
|
+
def clear_channel_context
|
75
|
+
@channel_context = {}
|
76
|
+
end
|
77
|
+
|
78
|
+
def channel(name)
|
79
|
+
sc = self.class.new(self)
|
80
|
+
sc.level = @level
|
81
|
+
channel_name = name
|
82
|
+
channel_name = "#{self[:channel]} => #{name}" if self[:channel]
|
83
|
+
sc[:channel] = channel_name
|
84
|
+
sc
|
85
|
+
end
|
86
|
+
|
87
|
+
%w(fatal error warn info debug unknown).each do |level|
|
88
|
+
predicate = "#{level}?".to_sym
|
89
|
+
level = level.to_sym
|
90
|
+
|
91
|
+
define_method(level) do |message = nil, context = {}, &block|
|
92
|
+
raise ::ArgumentError, "#{self.class}##{level} requires at least 1 argument or a block" if !message && !block
|
93
|
+
raise ::ArgumentError, "#{self.class}##{level} requires either a message OR a block" if message && block
|
94
|
+
raise ::ArgumentError, "#{self.class}##{level} context is a #{context.class}, it must be a Hash" unless context.is_a?(Hash)
|
95
|
+
|
96
|
+
return true unless send(predicate)
|
97
|
+
|
98
|
+
add(level_to_int(level), message, nil, context, &block)
|
99
|
+
end
|
100
|
+
|
101
|
+
define_method(predicate) do
|
102
|
+
level_to_int(level) >= @level
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
private
|
107
|
+
|
108
|
+
def level_to_int(level)
|
109
|
+
return level if level.is_a?(Fixnum)
|
110
|
+
LEVELS[level]
|
111
|
+
end
|
112
|
+
|
113
|
+
def level_from_int(level)
|
114
|
+
return LEVELS[level] if level.is_a?(Fixnum)
|
115
|
+
level.to_sym
|
116
|
+
end
|
117
|
+
|
118
|
+
def write(severity, context)
|
119
|
+
return true if @logdev.nil?
|
120
|
+
|
121
|
+
context.scrub!
|
122
|
+
|
123
|
+
data = context.merge(default_data(severity))
|
124
|
+
data[:app] = @app if @app
|
125
|
+
data[:caller] = debugharder(caller[3]) if debug?
|
126
|
+
data = extract_top_level_keys(data)
|
127
|
+
|
128
|
+
@logdev.write(Yajl::Encoder.encode(data) + "\n")
|
129
|
+
end
|
130
|
+
|
131
|
+
def default_data(severity)
|
132
|
+
{
|
133
|
+
timestamp: Time.now.strftime(TIMESTAMP_FORMAT),
|
134
|
+
level_name: level_from_int(severity),
|
135
|
+
level: severity
|
136
|
+
}
|
137
|
+
end
|
138
|
+
|
139
|
+
def extract_top_level_keys(context)
|
140
|
+
data = {}
|
141
|
+
TOP_LEVEL_KEYS.each do |key|
|
142
|
+
data[key] = context.delete(key) if context.key? key
|
143
|
+
end
|
144
|
+
data[:context] = context
|
145
|
+
data
|
146
|
+
end
|
147
|
+
|
148
|
+
def debugharder(callinfo)
|
149
|
+
m = BACKTRACE_RE.match(callinfo)
|
150
|
+
return unless m
|
151
|
+
path, line, method = m[1..3]
|
152
|
+
whence = $LOAD_PATH.find { |p| path.start_with?(p) }
|
153
|
+
if whence
|
154
|
+
# Remove the RUBYLIB path portion of the full file name
|
155
|
+
file = path[whence.length + 1..-1]
|
156
|
+
else
|
157
|
+
# We get here if the path is not in $:
|
158
|
+
file = path
|
159
|
+
end
|
160
|
+
|
161
|
+
{
|
162
|
+
file: file,
|
163
|
+
line: line,
|
164
|
+
method: method
|
165
|
+
}
|
166
|
+
end
|
167
|
+
end
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hedgelog
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jeff Utter
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: yajl-ruby
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.2'
|
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
|
+
description: An opinionated/structured JSON logger for Ruby
|
42
|
+
email:
|
43
|
+
- jeff.utter@firespring.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- .rspec
|
50
|
+
- .rubocop.yml
|
51
|
+
- .simplecov
|
52
|
+
- .travis.yml
|
53
|
+
- CONTRIBUTING.md
|
54
|
+
- Gemfile
|
55
|
+
- Guardfile
|
56
|
+
- LICENSE.md
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- bin/console
|
60
|
+
- bin/profile
|
61
|
+
- bin/setup
|
62
|
+
- config/pre_commit.yml
|
63
|
+
- hedgelog.gemspec
|
64
|
+
- lib/hedgelog.rb
|
65
|
+
- lib/hedgelog/context.rb
|
66
|
+
- lib/hedgelog/scrub_replacement.rb
|
67
|
+
- lib/hedgelog/scrubber.rb
|
68
|
+
- lib/hedgelog/version.rb
|
69
|
+
homepage: http://firespring.com
|
70
|
+
licenses:
|
71
|
+
- MIT
|
72
|
+
metadata: {}
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options: []
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ! '>='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ! '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
requirements: []
|
88
|
+
rubyforge_project:
|
89
|
+
rubygems_version: 2.4.3
|
90
|
+
signing_key:
|
91
|
+
specification_version: 4
|
92
|
+
summary: An opinionated/strucutred JSON logger for Ruby
|
93
|
+
test_files: []
|