logmaster 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0373b64411b242583449c3964dec0af7ba046084
4
+ data.tar.gz: 495b5011311803dd68cecab49ccbe006386add72
5
+ SHA512:
6
+ metadata.gz: e9b0c64385948186a75c0305202510e9d08a9464c976fa46f25ba23259f1db8155786f6b93753cd667891b5fdfc63043467c7074fd4691513a84dfdd595ab1eb
7
+ data.tar.gz: 58717ffcb9e8332fa0603de31bbacb2176098a800fe5db1da6dfac6a466289edb1ecaaed1fc82eb6175cf0085104ed698d058e721c1666833a9e964edf087896
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "pony"
4
+
5
+ # Add dependencies to develop your gem here.
6
+ # Include everything needed to run rake, tests, features, etc.
7
+ group :development do
8
+ gem "bundler", "~> 1.0"
9
+ gem "jeweler", "~> 2.0.1"
10
+ gem "github_api", "0.11.3"
11
+ end
12
+
13
+ group :test do
14
+ gem 'rspec'
15
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,76 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ addressable (2.3.6)
5
+ builder (3.2.2)
6
+ descendants_tracker (0.0.4)
7
+ thread_safe (~> 0.3, >= 0.3.1)
8
+ diff-lcs (1.2.5)
9
+ faraday (0.9.0)
10
+ multipart-post (>= 1.2, < 3)
11
+ git (1.2.8)
12
+ github_api (0.11.3)
13
+ addressable (~> 2.3)
14
+ descendants_tracker (~> 0.0.1)
15
+ faraday (~> 0.8, < 0.10)
16
+ hashie (>= 1.2)
17
+ multi_json (>= 1.7.5, < 2.0)
18
+ nokogiri (~> 1.6.0)
19
+ oauth2
20
+ hashie (3.3.1)
21
+ highline (1.6.21)
22
+ jeweler (2.0.1)
23
+ builder
24
+ bundler (>= 1.0)
25
+ git (>= 1.2.5)
26
+ github_api
27
+ highline (>= 1.6.15)
28
+ nokogiri (>= 1.5.10)
29
+ rake
30
+ rdoc
31
+ json (1.8.1)
32
+ jwt (1.0.0)
33
+ mail (2.6.1)
34
+ mime-types (>= 1.16, < 3)
35
+ mime-types (2.3)
36
+ mini_portile (0.6.0)
37
+ multi_json (1.10.1)
38
+ multi_xml (0.5.5)
39
+ multipart-post (2.0.0)
40
+ nokogiri (1.6.3.1)
41
+ mini_portile (= 0.6.0)
42
+ oauth2 (1.0.0)
43
+ faraday (>= 0.8, < 0.10)
44
+ jwt (~> 1.0)
45
+ multi_json (~> 1.3)
46
+ multi_xml (~> 0.5)
47
+ rack (~> 1.2)
48
+ pony (1.11)
49
+ mail (>= 2.0)
50
+ rack (1.5.2)
51
+ rake (10.3.2)
52
+ rdoc (4.1.2)
53
+ json (~> 1.4)
54
+ rspec (3.1.0)
55
+ rspec-core (~> 3.1.0)
56
+ rspec-expectations (~> 3.1.0)
57
+ rspec-mocks (~> 3.1.0)
58
+ rspec-core (3.1.4)
59
+ rspec-support (~> 3.1.0)
60
+ rspec-expectations (3.1.1)
61
+ diff-lcs (>= 1.2.0, < 2.0)
62
+ rspec-support (~> 3.1.0)
63
+ rspec-mocks (3.1.1)
64
+ rspec-support (~> 3.1.0)
65
+ rspec-support (3.1.0)
66
+ thread_safe (0.3.4)
67
+
68
+ PLATFORMS
69
+ ruby
70
+
71
+ DEPENDENCIES
72
+ bundler (~> 1.0)
73
+ github_api (= 0.11.3)
74
+ jeweler (~> 2.0.1)
75
+ pony
76
+ rspec
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2014 Roman Snitko
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,48 @@
1
+ Logmaster
2
+ =========
3
+
4
+ An enhanced logger library that handles different types of loggers
5
+ (at the moment STDOUT and FILE) and sends emails if email config is provided.
6
+
7
+ It can also watch and rescue exceptions logging them as FATAL.
8
+
9
+ Here's a usage example listing all of the possible options:
10
+
11
+
12
+ logmaster = Logmaster.new(
13
+ log_level: Logger::WARN, # Default is Logger::INFO
14
+ file: '/var/log/myapp.log', # if nil, will not log into any file
15
+ stdout: true, # if false, will not log into STDOUR
16
+ raise_exception: false, # if true, will a raise an Exception after logging it
17
+ email_config: nil, # see email config options below
18
+ name: "Logmaster" # currently useful for the email subjects,
19
+ # so you can see who's emailing you
20
+ )
21
+
22
+ logmaster.warn("This is a warning message") # logs into file and into the STDOUT
23
+ logmaster.info("This is an info message") # doesn't log anything, log level isn't sufficient
24
+
25
+
26
+ Sending email notifications
27
+ ---------------------------
28
+ Logmaster can also send emails. It uses Pony (https://github.com/benprew/pony) to do that. Pony usually
29
+ makes use of sendmail, but you can also specify SMTP options. See Pony docs to learn more. In our
30
+ example I will use the sendmail (which is default):
31
+
32
+ logmaster = Logmaster.new( email_config: { to: 'me@email.foo', from: "logmaster@yourapp.com" })
33
+ logmaster.warn("Wow, this is another warning!")
34
+
35
+ The second line will trigger sending an email to your address.
36
+
37
+
38
+ Wathcing the code for Exceptions
39
+ --------------------------------
40
+ You can watch your code for exceptions and then also log them. After you created your logmaster
41
+ instance, it's as simple as that:
42
+
43
+ logmaster.watch_exceptions do
44
+ 1/0
45
+ end
46
+
47
+ the exception will be rescued and logged as FATAL. If you also wish to actually raise it after it is
48
+ logged, don't forget to set `Logmaster#raise_exception` to `true`.
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
17
+ gem.name = "logmaster"
18
+ gem.homepage = "http://github.com/snitko/logmaster"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{A wrapper around ruby stdlib Logger with emailing capabilities}
21
+ gem.description = %Q{A wrapper around ruby stdlib Logger with emailing capabilities}
22
+ gem.email = "roman.snitko@gmail.com"
23
+ gem.authors = ["Roman Snitko"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,3 @@
1
+ <p>Logger message type: <b><%= type %></b></p>
2
+ <br/>
3
+ <pre><%= message %></pre>
data/lib/logmaster.rb ADDED
@@ -0,0 +1,86 @@
1
+ require 'logger'
2
+
3
+ class Logmaster
4
+
5
+ attr_accessor :loggers, :log_level, :name, :email_config, :raise_exception
6
+
7
+ def initialize(
8
+ log_level: Logger::INFO,
9
+ file: nil, # if nil, will not log into any file
10
+ stdout: true, # if false, will not log into STDOUR
11
+ raise_exception: false, # if true, will a raise an Exception after logging it
12
+ email_config: nil, # see email config options for Pony gem
13
+ name: "Logmaster"
14
+ )
15
+
16
+ @name = name
17
+ @raise_exception = raise_exception
18
+ @log_level = log_level
19
+ @loggers = []
20
+
21
+ self.email_config = email_config if email_config
22
+
23
+ @loggers << ::Logger.new(STDOUT) if stdout
24
+ @loggers << ::Logger.new(file, 10, 1024000) if file
25
+ @loggers.each { |l| l.level = @log_level }
26
+
27
+ end
28
+
29
+ def email_config=(settings)
30
+
31
+ require 'pony'
32
+ require 'erb'
33
+
34
+ @email_config = { via: :sendmail, from: 'logmaster@localhost', subject: "#{@name} message" }
35
+ @email_config.merge!(settings)
36
+ if @email_config[:to].nil?
37
+ raise "Please specify email addresses of email recipients using :to key in email_config attr (value should be array)"
38
+ end
39
+
40
+ end
41
+
42
+ def watch_exceptions
43
+ raise "Please provide a block to this method" unless block_given?
44
+ begin
45
+ yield
46
+ rescue Exception => e
47
+ message = e.class.to_s
48
+ message += ": "
49
+ message += e.message
50
+ message += "\n"
51
+ message += "Backtrace:\n"
52
+ e.backtrace.each { |l| message += " #{l}\n" }
53
+ self.fatal(message)
54
+ raise e if @raise_exception
55
+ end
56
+ end
57
+
58
+ private
59
+
60
+ def method_missing(name, *args)
61
+
62
+ if [:unknown, :fatal, :error, :warn, :info, :debug].include?(name)
63
+
64
+ if @email_config && @log_level <= Logger.const_get(name.to_s.upcase)
65
+ send_email(type: name, message: args[0])
66
+ end
67
+
68
+ args[0] = args[0] + "\n\n"
69
+ @loggers.each do |logger|
70
+ logger.send(name, *args)
71
+ end
72
+
73
+ end
74
+
75
+ end
76
+
77
+ def send_email(type:, message:)
78
+ template = ERB.new(File.read(File.expand_path(File.dirname(__FILE__)) +
79
+ "/../email_templates/message.erb")
80
+ ).result(binding)
81
+
82
+ Pony.mail(@email_config.merge({ html_body: template }))
83
+ end
84
+
85
+
86
+ end
@@ -0,0 +1,59 @@
1
+ require 'rspec'
2
+ require 'logmaster'
3
+ require 'fileutils'
4
+
5
+ class Logger
6
+ # Suppress log messages when testing
7
+ def add(*args);end
8
+ end
9
+
10
+ describe Logmaster do
11
+
12
+ LOGFILE = File.expand_path(File.dirname(__FILE__) + '/logfile')
13
+
14
+ before(:each) do
15
+ @logmaster = Logmaster.new(file: LOGFILE)
16
+ end
17
+
18
+ after(:each) do
19
+ FileUtils.rm LOGFILE
20
+ end
21
+
22
+ it "creates two loggers (stdout and logfile)" do
23
+ expect(@logmaster.loggers[0]).to be_kind_of(Logger)
24
+ expect(@logmaster.loggers[1]).to be_kind_of(Logger)
25
+ end
26
+
27
+ it "sets email settings" do
28
+ @logmaster.email_config = { to: 'your-email@here.com' }
29
+ expect(@logmaster.email_config).to eq({ via: :sendmail, from: 'logmaster@localhost', subject: "Logmaster message", to: 'your-email@here.com' })
30
+ end
31
+
32
+ it "sends log messages to each logger" do
33
+ @logmaster.loggers.each do |logger|
34
+ expect(logger).to receive(:warn).once
35
+ end
36
+ @logmaster.warn("WARNING bitches!")
37
+ end
38
+
39
+ it "sends emails when the log level of a log message is appropriate" do
40
+ @logmaster.email_config = { to: 'your-email@here.com' }
41
+ @logmaster.log_level = Logger::WARN
42
+
43
+ expect(Pony).to receive(:mail)
44
+
45
+ @logmaster.warn("WARNING bitches!") # Should call Pony.mail
46
+ @logmaster.log_level = Logger::FATAL
47
+ @logmaster.warn("WARNING bitches!") # This time shouldn't, log_level is wrong
48
+ end
49
+
50
+ it "watches for exceptions" do
51
+ @logmaster.loggers.each do |logger|
52
+ expect(logger).to receive(:fatal).once
53
+ end
54
+ @logmaster.watch_exceptions do
55
+ 1/0
56
+ end
57
+ end
58
+
59
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logmaster
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Roman Snitko
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pony
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.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: jeweler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 2.0.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 2.0.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: github_api
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 0.11.3
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 0.11.3
69
+ description: A wrapper around ruby stdlib Logger with emailing capabilities
70
+ email: roman.snitko@gmail.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files:
74
+ - LICENSE.txt
75
+ - README.md
76
+ files:
77
+ - ".document"
78
+ - ".rspec"
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - VERSION
85
+ - email_templates/message.erb
86
+ - lib/logmaster.rb
87
+ - spec/logmaster_spec.rb
88
+ homepage: http://github.com/snitko/logmaster
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.2.2
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: A wrapper around ruby stdlib Logger with emailing capabilities
112
+ test_files: []