yell-adapters-gelf 0.4.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.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +62 -0
- data/Rakefile +1 -0
- data/lib/yell-adapters-gelf.rb +35 -0
- data/lib/yell/adapters/gelf.rb +148 -0
- data/lib/yell/adapters/gelf/version.rb +13 -0
- data/yell-adapters-gelf.gemspec +26 -0
- metadata +65 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2011-2012 Rudolf Schmidt
|
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.
|
21
|
+
|
data/README.md
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
Yell Gelf - Graylog2 Adapter for Your Extensible Logging Library
|
2
|
+
|
3
|
+
If you are not yet familiar with **Yell - Your Extensible Logging Library**
|
4
|
+
check out the githup project under https://github.com/rudionrails/yell or jump
|
5
|
+
directly into the Yell wiki at https://github.com/rudionrails/yell/wiki.
|
6
|
+
|
7
|
+
Just in case you wonder: GELF means Graylog Extended Log Format. Read all
|
8
|
+
it at http://www.graylog2.org/about/gelf.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
System wide:
|
13
|
+
|
14
|
+
```console
|
15
|
+
gem install yell-gelf
|
16
|
+
```
|
17
|
+
|
18
|
+
Or in your Gemfile:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
gem "yell-gelf"
|
22
|
+
```
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
After you set-up Graylog2 accordingly, you may use the Gelf adapter just like
|
27
|
+
any other.
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
logger = Yell.new :gelf
|
31
|
+
|
32
|
+
logger.info "Hello World"
|
33
|
+
# Check your Graylog2 web server for the received message.
|
34
|
+
```
|
35
|
+
|
36
|
+
Or alternatively with the block syntax:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
logger = Yell.new do
|
40
|
+
adapter :gelf
|
41
|
+
end
|
42
|
+
|
43
|
+
logger.info 'Hello World!'
|
44
|
+
```
|
45
|
+
|
46
|
+
If you are running Graylog2 on a different server or port, you can pass those
|
47
|
+
options to the adapter:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
logger = Yell.new :gelf, :host => 'hostname', :port => 1234
|
51
|
+
|
52
|
+
# Or with the block syntax
|
53
|
+
logger = Yell.new do
|
54
|
+
adapter :gelf, :host => 'hostname', :port => 1234
|
55
|
+
end
|
56
|
+
|
57
|
+
logger.info 'Hello World!'
|
58
|
+
```
|
59
|
+
|
60
|
+
|
61
|
+
Copyright © 2011-2012 Rudolf Schmidt, released under the MIT license
|
62
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# Copyright (c) 2011-2012 Rudolf Schmidt
|
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.
|
23
|
+
|
24
|
+
require 'yell'
|
25
|
+
|
26
|
+
begin
|
27
|
+
require 'yell/adapters/gelf'
|
28
|
+
rescue LoadError
|
29
|
+
$: << File.dirname(__FILE__)
|
30
|
+
require 'yell/adapters/gelf'
|
31
|
+
end
|
32
|
+
|
33
|
+
module Yell #:nodoc:
|
34
|
+
end
|
35
|
+
|
@@ -0,0 +1,148 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'socket'
|
4
|
+
require 'json'
|
5
|
+
require 'zlib'
|
6
|
+
require 'digest/md5'
|
7
|
+
|
8
|
+
module Yell #:nodoc:
|
9
|
+
module Adapters #:nodoc:
|
10
|
+
|
11
|
+
# GELF for Graylog2.
|
12
|
+
class Gelf < Yell::Adapters::Base
|
13
|
+
|
14
|
+
# Syslog severities
|
15
|
+
Severities = [7, 6, 4, 3, 2, 1]
|
16
|
+
|
17
|
+
# Combines syslog severities with internal representation:
|
18
|
+
# 'DEBUG' => 7
|
19
|
+
# 'INFO' => 6
|
20
|
+
# 'WARN' => 4
|
21
|
+
# 'ERROR' => 3
|
22
|
+
# 'FATAL' => 2
|
23
|
+
# 'UNKNOWN' => 1
|
24
|
+
SeverityMap = Hash[ *(Yell::Severities.zip(Severities).flatten) ]
|
25
|
+
|
26
|
+
class Sender
|
27
|
+
def initialize( *hosts )
|
28
|
+
@hosts = hosts
|
29
|
+
@socket = UDPSocket.new
|
30
|
+
end
|
31
|
+
|
32
|
+
def send( *datagrams )
|
33
|
+
datagrams.each do |d|
|
34
|
+
@socket.send( d, 0, *host_and_port )
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def close
|
39
|
+
@socket.close unless @socket.closed?
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def host_and_port
|
45
|
+
# Don't cycle the elements when only one present
|
46
|
+
return @hosts.first if @hosts.size == 1
|
47
|
+
|
48
|
+
# Cycle host and port
|
49
|
+
host = @hosts.shift
|
50
|
+
@hosts << host
|
51
|
+
|
52
|
+
host
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def initialize( options = {}, &block )
|
57
|
+
@uid = 0
|
58
|
+
|
59
|
+
# initialize the UDP Sender
|
60
|
+
@host = options.fetch(:host, 'localhost')
|
61
|
+
@port = options.fetch(:port, 12201)
|
62
|
+
|
63
|
+
max_chunk_size options.fetch(:max_chunk_size, :wan)
|
64
|
+
|
65
|
+
super( options, &block )
|
66
|
+
end
|
67
|
+
|
68
|
+
|
69
|
+
# The sender (UDP Socket)
|
70
|
+
def sender
|
71
|
+
@sender ||= Yell::Adapters::Gelf::Sender.new( [@host, @port] )
|
72
|
+
end
|
73
|
+
|
74
|
+
# Close the UDP sender
|
75
|
+
def close
|
76
|
+
@sender.close if @sender.respond_to? :close
|
77
|
+
|
78
|
+
@sender = nil
|
79
|
+
end
|
80
|
+
|
81
|
+
def max_chunk_size( val )
|
82
|
+
@max_chunk_size = case val
|
83
|
+
when :wan then 1420
|
84
|
+
when :lan then 8154
|
85
|
+
else val
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
|
90
|
+
private
|
91
|
+
|
92
|
+
def write!( event )
|
93
|
+
# See https://github.com/Graylog2/graylog2-docs/wiki/GELF
|
94
|
+
# for formatting options.
|
95
|
+
_datagrams = datagrams(
|
96
|
+
'version' => '1.0',
|
97
|
+
'facility' => 'yell',
|
98
|
+
|
99
|
+
'level' => SeverityMap[event.level],
|
100
|
+
'short_message' => event.message,
|
101
|
+
'timestamp' => event.time.to_f,
|
102
|
+
'host' => Socket.gethostname,
|
103
|
+
|
104
|
+
'file' => event.file,
|
105
|
+
'line' => event.line,
|
106
|
+
'_method' => event.method,
|
107
|
+
'_pid' => Process.pid
|
108
|
+
)
|
109
|
+
|
110
|
+
sender.send( *_datagrams )
|
111
|
+
rescue Exception => e
|
112
|
+
close
|
113
|
+
|
114
|
+
# re-raise the exception
|
115
|
+
raise( e, caller )
|
116
|
+
end
|
117
|
+
|
118
|
+
def datagrams( data )
|
119
|
+
bytes = Zlib::Deflate.deflate( data.to_json ).bytes
|
120
|
+
_datagrams = []
|
121
|
+
|
122
|
+
if bytes.count > @max_chunk_size
|
123
|
+
_id = Digest::MD5.digest( "#{Time.now.to_f}-#{object_id}-#{uid}" )[0, 8]
|
124
|
+
|
125
|
+
num, count = 0, (bytes.count.to_f / @max_chunk_size).ceil
|
126
|
+
bytes.each_slice( @max_chunk_size ) do |slice|
|
127
|
+
_datagrams << "\x1e\x0f" + _id + [num, count, *slice].pack('C*')
|
128
|
+
num += 1
|
129
|
+
end
|
130
|
+
else
|
131
|
+
_datagrams << bytes.to_a.pack('C*')
|
132
|
+
end
|
133
|
+
|
134
|
+
_datagrams
|
135
|
+
end
|
136
|
+
|
137
|
+
def uid
|
138
|
+
@uid += 1
|
139
|
+
end
|
140
|
+
|
141
|
+
end
|
142
|
+
|
143
|
+
register( :gelf, Yell::Adapters::Gelf )
|
144
|
+
register( :graylog2, Yell::Adapters::Gelf )
|
145
|
+
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
$:.push File.expand_path("../lib", __FILE__)
|
4
|
+
require "yell/adapters/gelf/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "yell-adapters-gelf"
|
8
|
+
s.version = Yell::Adapters::Gelf::VERSION
|
9
|
+
s.authors = ["Rudolf Schmidt"]
|
10
|
+
|
11
|
+
|
12
|
+
s.homepage = "http://rubygems.org/gems/yell"
|
13
|
+
s.summary = %q{Yell - Your Extensible Logging Library }
|
14
|
+
s.description = %q{Graylog2 adapter for Yell}
|
15
|
+
|
16
|
+
s.rubyforge_project = "yell"
|
17
|
+
|
18
|
+
s.files = `git ls-files`.split("\n")
|
19
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
20
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
21
|
+
s.require_paths = ["lib"]
|
22
|
+
|
23
|
+
# specify any dependencies here; for example:
|
24
|
+
# s.add_development_dependency "rspec"
|
25
|
+
s.add_runtime_dependency "yell", "~> 0.4"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yell-adapters-gelf
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.4.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Rudolf Schmidt
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-27 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: yell
|
16
|
+
requirement: &11062060 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0.4'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *11062060
|
25
|
+
description: Graylog2 adapter for Yell
|
26
|
+
email:
|
27
|
+
executables: []
|
28
|
+
extensions: []
|
29
|
+
extra_rdoc_files: []
|
30
|
+
files:
|
31
|
+
- .gitignore
|
32
|
+
- Gemfile
|
33
|
+
- LICENSE.txt
|
34
|
+
- README.md
|
35
|
+
- Rakefile
|
36
|
+
- lib/yell-adapters-gelf.rb
|
37
|
+
- lib/yell/adapters/gelf.rb
|
38
|
+
- lib/yell/adapters/gelf/version.rb
|
39
|
+
- yell-adapters-gelf.gemspec
|
40
|
+
homepage: http://rubygems.org/gems/yell
|
41
|
+
licenses: []
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubyforge_project: yell
|
60
|
+
rubygems_version: 1.8.10
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: Yell - Your Extensible Logging Library
|
64
|
+
test_files: []
|
65
|
+
has_rdoc:
|