blinkbox-common_logging 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/CHANGELOG.md +94 -0
- data/README.md +35 -0
- data/VERSION +1 -0
- data/lib/blinkbox/common_logging.rb +85 -0
- data/lib/blinkbox/utilities/extra_hash_methods.rb +76 -0
- metadata +121 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
Njk4YzRhMzg4NjcyZjVjZWQ4NDRjMjg4ZjM0OTQwM2VjMWMzMzRiNQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
YjAxYTcxMjQ5ZGE1ODY2MDgzNzBhYmZmMDJmNzYwNTY1NmQzNmZjNw==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
NWM5MTFlNTkwOGMzZTQxZWY4MjZiODQ4ZDhmYWVmOWQzYzhlNWMxZWMyMWMy
|
10
|
+
ZTFkMDcyOTNjYWY4NGQzZjA0MjcwYjVmMWYxMTAzY2JkMWI5NDM2ZGRiYjQx
|
11
|
+
YjZjYTQ3YTI2MWI3ZWY2ZGU4YWVmZjdlMTAyZDdjZDNhY2I2YjQ=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
MzU1NzIwMDA2MDI1YmNhMGM3NmYyM2RiOTIwOGUwYmY4OTFlYmJmZjk3MDFj
|
14
|
+
Y2E4YjY5MDU3NTZkYTA0ZDZmODliYTc4ZjRjYjY1M2RmZjNlZjdlMzZkYjYw
|
15
|
+
OTNiZTU5NmFmNGQ3M2ZkNjBkZDlkNTRiNzQ0ZDViODJmOGFhYWE=
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
# Change log
|
2
|
+
|
3
|
+
## Open Source release (2015-01-28 14:11:21)
|
4
|
+
|
5
|
+
Today we have decided to publish parts of our codebase on Github under the [MIT licence](LICENCE). The licence is very permissive, but we will always appreciate hearing if and where our work is used!
|
6
|
+
|
7
|
+
## 0.5.1 ([#8](https://git.mobcastdev.com/Platform/common_logging.rb/pull/8) 2014-12-15 15:01:11)
|
8
|
+
|
9
|
+
Changed level mapping to :direct
|
10
|
+
|
11
|
+
### Bug fixes
|
12
|
+
|
13
|
+
- Changed the level mapping to be `:direct` which means that
|
14
|
+
`logger.error` causes an error level message in Graylog, `logger.warn`
|
15
|
+
causes a warning level message, and so on. The default mappings mean
|
16
|
+
that `logger.error` causes a warning level message, and `logger.warn`
|
17
|
+
causes a notice level message which is nuts.
|
18
|
+
|
19
|
+
## 0.5.0 ([#7](https://git.mobcastdev.com/Platform/common_logging.rb/pull/7) 2014-10-03 15:02:14)
|
20
|
+
|
21
|
+
Log to console as well as Graylog
|
22
|
+
|
23
|
+
### New features
|
24
|
+
|
25
|
+
- Setting `logging.console.enabled` to `true` will now output to stdout as well as graylog.
|
26
|
+
- Slight refactoring of code in files.
|
27
|
+
|
28
|
+
## 0.4.1 ([#6](https://git.mobcastdev.com/Platform/common_logging.rb/pull/6) 2014-10-02 08:31:38)
|
29
|
+
|
30
|
+
Validation of values
|
31
|
+
|
32
|
+
### Improvements
|
33
|
+
|
34
|
+
- Validates input from config and raises an `ArgumentError` if anything is unusable.
|
35
|
+
- Will list out *all* problematic variables so fixing the config is faster.
|
36
|
+
- Translates the named invalid keys between `from_config` and `new` so that the error message makes sense for the code being used.
|
37
|
+
|
38
|
+
## 0.4.0 ([#5](https://git.mobcastdev.com/Platform/common_logging.rb/pull/5) 2014-09-25 10:46:32)
|
39
|
+
|
40
|
+
Shallow hashes
|
41
|
+
|
42
|
+
### New feature
|
43
|
+
|
44
|
+
- Graylog can't deal with nested hashes, so I've added functionality to flatten deep hashes given to the logging methods into dot-separated shallow ones:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
{
|
48
|
+
a: {
|
49
|
+
b: "will become a.b"
|
50
|
+
}
|
51
|
+
}
|
52
|
+
```
|
53
|
+
|
54
|
+
becomes
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
{
|
58
|
+
"a.b" => "will become a.b"
|
59
|
+
}
|
60
|
+
```
|
61
|
+
|
62
|
+
## 0.3.1 ([#4](https://git.mobcastdev.com/Platform/common_logging.rb/pull/4) 2014-09-22 13:53:00)
|
63
|
+
|
64
|
+
Ensure from_config actually works from a config!
|
65
|
+
|
66
|
+
### Bugfix
|
67
|
+
|
68
|
+
- `.from_config` now expects a shallow hash (with dots in keys) just as common_config.rb provides.
|
69
|
+
|
70
|
+
## 0.3.0 ([#3](https://git.mobcastdev.com/Platform/common_logging.rb/pull/3) 2014-08-14 16:45:39)
|
71
|
+
|
72
|
+
Allow adding of service version to default params
|
73
|
+
|
74
|
+
### New feature
|
75
|
+
|
76
|
+
- Allows us to add the default `serviceVersion` parameter to the messages sent to GrayLog which, along with the facility (which should be the service name) will allow us to pinpoint what code needs to be looked at when issues occur.
|
77
|
+
|
78
|
+
## 0.2.0 ([#2](https://git.mobcastdev.com/Platform/common_logging.rb/pull/2) 2014-08-14 13:09:41)
|
79
|
+
|
80
|
+
Support loading directly from config
|
81
|
+
|
82
|
+
### New feature
|
83
|
+
|
84
|
+
- Allow the injection of a hash from common_config to initialise a logger.
|
85
|
+
|
86
|
+
## 0.1.0 ([#1](https://git.mobcastdev.com/Platform/common_logging.rb/pull/1) 2014-08-12 17:05:06)
|
87
|
+
|
88
|
+
First release
|
89
|
+
|
90
|
+
### New Features
|
91
|
+
|
92
|
+
- Logs messages and exceptions.
|
93
|
+
- Pretty much only wraps the `GELF::Notifier` class (may need to check log levels) at the moment, but we may extend this to include extra functionality as the ruby logging requirements become clear.
|
94
|
+
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Blinkbox::CommonLogging
|
2
|
+
|
3
|
+
Automatic configuration for logging in the blinkbox format.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
# $ gem install blinkbox-common_logging
|
9
|
+
require "blinkbox/common_logging"
|
10
|
+
|
11
|
+
log_server = {
|
12
|
+
host: "127.0.0.1",
|
13
|
+
port: 12201,
|
14
|
+
# This is the name of the invoked script
|
15
|
+
facility: $0,
|
16
|
+
|
17
|
+
}
|
18
|
+
# These are the defaults
|
19
|
+
|
20
|
+
logger = Blinkbox::CommonLogging.new(log_server)
|
21
|
+
|
22
|
+
# Usual levels are available
|
23
|
+
logger.debug "This is a basic debug message"
|
24
|
+
logger.info "This is a basic info message"
|
25
|
+
logger.warn "This is a basic warn message"
|
26
|
+
logger.error "This is a basic error message"
|
27
|
+
logger.fatal "This is a basic fatal message"
|
28
|
+
|
29
|
+
# You can also pass an exception to #notify
|
30
|
+
begin
|
31
|
+
"raise an exception".by_calling_a_missing_method
|
32
|
+
raise StandardError => e
|
33
|
+
logger.notify(e)
|
34
|
+
end
|
35
|
+
```
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.5.1
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require "blinkbox/utilities/extra_hash_methods"
|
2
|
+
require "gelf"
|
3
|
+
require "logger"
|
4
|
+
|
5
|
+
module Blinkbox
|
6
|
+
class CommonLogging < GELF::Logger
|
7
|
+
def initialize(host: "localhost", port: 12201, facility: $0, max_size: 8192, facility_version: nil, echo_to_console: false)
|
8
|
+
validity_issues = []
|
9
|
+
validity_issues.push("host") unless host.is_a?(String)
|
10
|
+
validity_issues.push("port") unless port.is_a?(Integer)
|
11
|
+
validity_issues.push("facility") unless facility.is_a?(String)
|
12
|
+
validity_issues.push("max_size") unless max_size.is_a?(Integer)
|
13
|
+
raise ArgumentError, "Cannot start the logger, the following settings weren't valid: #{validity_issues.join(", ")}" if !validity_issues.empty?
|
14
|
+
|
15
|
+
if echo_to_console
|
16
|
+
@stdout_logger = Logger.new(STDOUT)
|
17
|
+
@stdout_logger.formatter = proc do |severity, datetime, progname, msg|
|
18
|
+
# Using console output is only for live-debug, so we can strip the timestamp, progname
|
19
|
+
l = msg.dup
|
20
|
+
l.delete("facility")
|
21
|
+
l.delete(:facility)
|
22
|
+
log_msg = "#{severity[0..0]} #{l.delete("short_message")}#{l.delete(:short_message)}"
|
23
|
+
log_msg << " (#{l})" if l.any?
|
24
|
+
log_msg << "\n"
|
25
|
+
log_msg
|
26
|
+
end
|
27
|
+
end
|
28
|
+
options = { facility: facility }
|
29
|
+
options[:facilityVersion] = facility_version unless facility_version.nil?
|
30
|
+
super(host, port, max_size, options)
|
31
|
+
end
|
32
|
+
|
33
|
+
# Allows the setting of the facility version default parameter.
|
34
|
+
# @param [String] semver The semantic version of the service which is sending logs
|
35
|
+
def facility_version=(semver)
|
36
|
+
self.default_options['facilityVersion'] = semver
|
37
|
+
end
|
38
|
+
|
39
|
+
# Accepts a hash detailing log config settings in the format used in blinkbox Books
|
40
|
+
# config files and returns a CommonLogging object.
|
41
|
+
#
|
42
|
+
# @example Load from config
|
43
|
+
# config = Blinkbox::CommonConfig.new
|
44
|
+
# logger = Blinkbox::CommonLogging.from_config(config.tree(:logging))
|
45
|
+
#
|
46
|
+
# @param [Hash] hash The returned hash from CommonConfig#tree
|
47
|
+
# @option hash [String] :udp.host (localhost) The Graylog host
|
48
|
+
# @option hash [Integer] :udp.port (12201) The Graylog port number
|
49
|
+
# @option hash [String] :gelf.facility Organisation/app_name eg. Marvin/cover_processor
|
50
|
+
# @option hash [Integer] :gelf.maxChunkSize (8192) The default size for UDP chunks sent to the server.
|
51
|
+
# @option hash ["DEBUG","INFO","WARN","ERROR","FATAL"] :level ("INFO") The level below which messages will not be sent to Graylog.
|
52
|
+
# @return [CommonLogging]
|
53
|
+
def self.from_config(hash)
|
54
|
+
mapping = {
|
55
|
+
host: :'udp.host',
|
56
|
+
port: :'udp.port',
|
57
|
+
facility: :'gelf.facility',
|
58
|
+
max_size: :'gelf.maxChunkSize',
|
59
|
+
echo_to_console: :'console.enabled'
|
60
|
+
}
|
61
|
+
begin
|
62
|
+
logger = new(Hash[mapping.map { |k, v| [k, hash[v]] }])
|
63
|
+
rescue ArgumentError => e
|
64
|
+
msg = e.message
|
65
|
+
mapping.each do |k, v|
|
66
|
+
msg.sub!(k.to_s, v.to_s)
|
67
|
+
end
|
68
|
+
raise e.class, msg
|
69
|
+
end
|
70
|
+
logger.level = GELF.const_get(hash[:level].upcase) rescue GELF::INFO
|
71
|
+
logger.level_mapping = :direct
|
72
|
+
logger
|
73
|
+
end
|
74
|
+
|
75
|
+
private
|
76
|
+
def notify_with_level!(message_level, msg)
|
77
|
+
if @stdout_logger
|
78
|
+
level = GELF::Levels.constants[message_level].to_s.downcase.to_sym
|
79
|
+
@stdout_logger.send(level, msg)
|
80
|
+
end
|
81
|
+
msg.extend(ExtraHashMethods).shallow! if msg.is_a?(Hash)
|
82
|
+
super(message_level, msg)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
module Blinkbox
|
2
|
+
module ExtraHashMethods
|
3
|
+
# Turns a deep/nested hash into a shallow one by concatenating nested key names.
|
4
|
+
#
|
5
|
+
# @example: Removing nesting
|
6
|
+
# hash = {
|
7
|
+
# "a" => {
|
8
|
+
# "1" => 'will be a.1'
|
9
|
+
# },
|
10
|
+
# "b" => 'will be b'
|
11
|
+
# }
|
12
|
+
# hash.extend(ExtraHashMethods).shallow!
|
13
|
+
# # => true
|
14
|
+
# p hash
|
15
|
+
# # => { "a.1" => 'will be a.1', "b" => 'will be b' }
|
16
|
+
#
|
17
|
+
# @example: Removing nesting - custom join string
|
18
|
+
# hash = {
|
19
|
+
# "a" => {
|
20
|
+
# "1" => 'will be merged down'
|
21
|
+
# },
|
22
|
+
# "b" => 'will be the same'
|
23
|
+
# }
|
24
|
+
# hash.extend(ExtraHashMethods).shallow!("~")
|
25
|
+
# # => true
|
26
|
+
# p hash
|
27
|
+
# # => { "a~1" => 'will be merged down', "b" => 'will be the same' }
|
28
|
+
#
|
29
|
+
# @example: No removal needed
|
30
|
+
# hash = {
|
31
|
+
# "a" => 'no deep keys here'
|
32
|
+
# }
|
33
|
+
# hash.extend(ExtraHashMethods).shallow!
|
34
|
+
# # => false
|
35
|
+
# p hash
|
36
|
+
# # => { "a" => 'no deep keys here' }
|
37
|
+
#
|
38
|
+
# @example: Conflicting key names
|
39
|
+
# hash = {
|
40
|
+
# "a" => {
|
41
|
+
# "1" => 'will be a.1'
|
42
|
+
# },
|
43
|
+
# "a.1" => 'will be lost!'
|
44
|
+
# }
|
45
|
+
# hash.extend(ExtraHashMethods).shallow!
|
46
|
+
# # => ["a.1"]
|
47
|
+
# p hash
|
48
|
+
# # => { "a.1" => 'will be a.1' }
|
49
|
+
#
|
50
|
+
# @param [String] join The string to use to join keys.
|
51
|
+
# @return [Boolean] Whether any keys were changed
|
52
|
+
def shallow!(join = ".")
|
53
|
+
shallowed = false
|
54
|
+
keys = self.keys.select { |k| self[k].is_a?(Hash) }
|
55
|
+
while (k = keys.shift) do
|
56
|
+
shallowed = shallowed || true
|
57
|
+
v = self.delete(k)
|
58
|
+
v.each do |sub_k, sub_v|
|
59
|
+
new_key = [k, sub_k].join(join)
|
60
|
+
if self.has_key?(new_key)
|
61
|
+
shallowed = [] unless shallowed.is_a?(Array)
|
62
|
+
shallowed.push(new_key)
|
63
|
+
end
|
64
|
+
self[new_key] = if sub_v.is_a?(Hash)
|
65
|
+
sub_v.extend(ExtraHashMethods).shallow!(join)
|
66
|
+
keys.push(new_key)
|
67
|
+
sub_v
|
68
|
+
else
|
69
|
+
sub_v
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
shallowed
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
metadata
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: blinkbox-common_logging
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- JP Hastings-Spital
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: gelf
|
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: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec-mocks
|
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: simplecov
|
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
|
+
description: Simple helper for logging to gelf in the format required by blinkbox
|
84
|
+
Books
|
85
|
+
email:
|
86
|
+
- jphastings@blinkbox.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files:
|
90
|
+
- README.md
|
91
|
+
- CHANGELOG.md
|
92
|
+
files:
|
93
|
+
- CHANGELOG.md
|
94
|
+
- README.md
|
95
|
+
- VERSION
|
96
|
+
- lib/blinkbox/common_logging.rb
|
97
|
+
- lib/blinkbox/utilities/extra_hash_methods.rb
|
98
|
+
homepage: ''
|
99
|
+
licenses: []
|
100
|
+
metadata: {}
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ! '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubyforge_project:
|
117
|
+
rubygems_version: 2.4.5
|
118
|
+
signing_key:
|
119
|
+
specification_version: 4
|
120
|
+
summary: Simple helper for logging to gelf in the format required by blinkbox Books
|
121
|
+
test_files: []
|