logman_rails 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +43 -0
- data/Rakefile +1 -0
- data/lib/logman_rails.rb +137 -0
- data/lib/logman_rails/log.rb +31 -0
- data/lib/logman_rails/version.rb +3 -0
- data/logman_rails.gemspec +23 -0
- metadata +88 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Branko Krstic
|
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,43 @@
|
|
1
|
+
# LogmanRails
|
2
|
+
|
3
|
+
This is Rails client library for [Logman](https://github.com/saicoder/logman).
|
4
|
+
Library tracks exceptions in Rails and sends them
|
5
|
+
to [Logman](https://github.com/saicoder/logman) endpoint.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'logman_rails'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install logman_rails
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Place following code in your environment config with your endpoint and token:
|
24
|
+
```ruby
|
25
|
+
config.middleware.use Logman::Rails,
|
26
|
+
:endpoint=> 'http://endpoint.location.com/',
|
27
|
+
:token=> 'your bucket token'
|
28
|
+
```
|
29
|
+
|
30
|
+
You can also send custom logs with methods below:
|
31
|
+
```ruby
|
32
|
+
Logman.info 'Some message'
|
33
|
+
Logman.success 'File uploaded', {optional: 'data'}
|
34
|
+
```
|
35
|
+
Available methods are: `error`, `warn`, `success`, `info`
|
36
|
+
|
37
|
+
## Contributing
|
38
|
+
|
39
|
+
1. Fork it
|
40
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
41
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
42
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
43
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/logman_rails.rb
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
require 'socket'
|
2
|
+
require 'net/http'
|
3
|
+
require 'json'
|
4
|
+
require 'uri'
|
5
|
+
|
6
|
+
require "logman_rails/version"
|
7
|
+
require 'action_dispatch'
|
8
|
+
require 'logman_rails/log'
|
9
|
+
|
10
|
+
|
11
|
+
class Logman
|
12
|
+
def self.options
|
13
|
+
@@options
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.options=(val)
|
17
|
+
@@options = val
|
18
|
+
end
|
19
|
+
|
20
|
+
class Rails
|
21
|
+
def self.default_ignore_exceptions
|
22
|
+
[].tap do |exceptions|
|
23
|
+
exceptions << ActiveRecord::RecordNotFound if defined? ActiveRecord
|
24
|
+
# exceptions << AbstractController::ActionNotFound if defined? AbstractController
|
25
|
+
exceptions << ActionController::RoutingError if defined? ActionController
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def initialize(app, options = {})
|
30
|
+
@app, @options = app, options
|
31
|
+
@options[:ignore_exceptions] ||= self.class.default_ignore_exceptions
|
32
|
+
Logman.options = @options
|
33
|
+
end
|
34
|
+
|
35
|
+
def call(env)
|
36
|
+
@app.call(env)
|
37
|
+
|
38
|
+
rescue Exception => exception
|
39
|
+
options = (env['exception_notifier.options'] ||= {})
|
40
|
+
options.reverse_merge!(@options)
|
41
|
+
|
42
|
+
raise exception if options[:endpoint].nil? || options[:token].nil?
|
43
|
+
|
44
|
+
unless Array.wrap(options[:ignore_exceptions]).include?(exception.class)
|
45
|
+
# Notifier.exception_notification(env, exception).deliver
|
46
|
+
# env['exception_notifier.delivered'] = true
|
47
|
+
newlog = Log.new
|
48
|
+
newlog.message = exception.to_s
|
49
|
+
|
50
|
+
data = {}
|
51
|
+
data[:exception_class] = exception.class.name
|
52
|
+
data[:backtrace] = exception.backtrace
|
53
|
+
data[:params] = env["action_dispatch.request.parameters"]
|
54
|
+
|
55
|
+
newlog.data = data
|
56
|
+
|
57
|
+
Logman.send(newlog, options[:endpoint], options[:token])
|
58
|
+
end
|
59
|
+
|
60
|
+
raise exception
|
61
|
+
end
|
62
|
+
end
|
63
|
+
# end of rails class
|
64
|
+
|
65
|
+
def self.send(log, endpoint, token)
|
66
|
+
begin
|
67
|
+
uri = URI(endpoint)
|
68
|
+
uri.path = '/api/write'
|
69
|
+
uri.query = "key=#{token}"
|
70
|
+
|
71
|
+
req = Net::HTTP::Post.new(uri.request_uri, initheader = {'Content-Type' =>'application/json'})
|
72
|
+
req.body = log.to_json
|
73
|
+
|
74
|
+
res = Net::HTTP.start(uri.hostname, uri.port) do |http|
|
75
|
+
http.request(req)
|
76
|
+
end
|
77
|
+
rescue
|
78
|
+
puts 'Can not send message to logman endpoint'
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
# Writes custom message to logman endpoint
|
83
|
+
# Logtype is symbol, avalabile symbols are:
|
84
|
+
# Prameters:
|
85
|
+
# - log_type => type of log, avalabile types are
|
86
|
+
# :error :success :warn :info
|
87
|
+
# - message => message in string format
|
88
|
+
# - optional data
|
89
|
+
def self.log(log_type, message, data={})
|
90
|
+
return if message.class != String || message.empty?
|
91
|
+
|
92
|
+
map = {:error=>1, :success=>2, :warn=>3, :info=>4}
|
93
|
+
ltype = map[log_type]
|
94
|
+
ltype = 4 if ltype.nil? #info
|
95
|
+
|
96
|
+
newlog = Log.new
|
97
|
+
newlog.message = message
|
98
|
+
newlog.log_type = ltype
|
99
|
+
newlog.data = data
|
100
|
+
Logman.send(newlog, Logman.options[:endpoint], Logman.options[:token])
|
101
|
+
end
|
102
|
+
|
103
|
+
# Sends a warn message
|
104
|
+
def self.warn(message, data={})
|
105
|
+
Logman.log(:warn, message ,data)
|
106
|
+
end
|
107
|
+
|
108
|
+
# Sends a error message
|
109
|
+
def self.error(message, data={})
|
110
|
+
Logman.log(:error, message ,data)
|
111
|
+
end
|
112
|
+
|
113
|
+
# Sends a success message
|
114
|
+
def self.success(message, data={})
|
115
|
+
Logman.log(:success, message ,data)
|
116
|
+
end
|
117
|
+
|
118
|
+
# Sends a success message
|
119
|
+
def self.info(message, data={})
|
120
|
+
Logman.log(:info, message ,data)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
|
@@ -0,0 +1,31 @@
|
|
1
|
+
|
2
|
+
class Logman
|
3
|
+
class Log
|
4
|
+
attr_accessor :message
|
5
|
+
attr_accessor :log_type
|
6
|
+
|
7
|
+
attr_accessor :data
|
8
|
+
attr_accessor :datetime
|
9
|
+
|
10
|
+
attr_accessor :data_type
|
11
|
+
|
12
|
+
attr_accessor :sources
|
13
|
+
|
14
|
+
def initialize
|
15
|
+
@data_type = 'rails.exception'
|
16
|
+
@log_type = 1 #error
|
17
|
+
@datetime = Time.now.utc
|
18
|
+
|
19
|
+
@sources = []
|
20
|
+
@sources << Log.get_current_source
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
def self.get_current_source
|
25
|
+
sr = {:name=>Socket.gethostname}
|
26
|
+
ip = Socket.ip_address_list.detect{|intf| intf.ipv4_private?}
|
27
|
+
sr[:ip_address] = ip.ip_address if ip
|
28
|
+
sr
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'logman_rails/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "logman_rails"
|
8
|
+
spec.version = LogmanRails::VERSION
|
9
|
+
spec.authors = ["Branko Krstic"]
|
10
|
+
spec.email = ["saicoder.net@gmail.com"]
|
11
|
+
spec.description = %q{This is Rails client library for Logman. Library tracks exceptions in Rails and sends them to Logman endpoint.}
|
12
|
+
spec.summary = %q{Rails client library for Logman.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
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_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: logman_rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Branko Krstic
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-01-31 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: This is Rails client library for Logman. Library tracks exceptions in
|
47
|
+
Rails and sends them to Logman endpoint.
|
48
|
+
email:
|
49
|
+
- saicoder.net@gmail.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- Gemfile
|
56
|
+
- LICENSE.txt
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- lib/logman_rails.rb
|
60
|
+
- lib/logman_rails/log.rb
|
61
|
+
- lib/logman_rails/version.rb
|
62
|
+
- logman_rails.gemspec
|
63
|
+
homepage: ''
|
64
|
+
licenses:
|
65
|
+
- MIT
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
78
|
+
requirements:
|
79
|
+
- - ! '>='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
requirements: []
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 1.8.23
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: Rails client library for Logman.
|
88
|
+
test_files: []
|