oohlalog 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in oohlalog.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 David Estes
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,57 @@
1
+ # Oohlalog
2
+
3
+ A gem designed to tie into the Oohlalog logging service.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'oohlalog'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install oohlalog
18
+
19
+ ## Usage
20
+
21
+ ### Rails 3 & Rails 4 (Edge)
22
+
23
+ This gem automatically ties into the rails standard BufferedLogger. Set your configuration options in your environment file.
24
+
25
+ ```ruby
26
+ Oohlalog.api_key = "YOUR API KEY HERE"
27
+ ```
28
+
29
+ ### Standard Ruby
30
+
31
+ Oohlalog is designed as an independent logging class. You can use it this way or tie it into the main ruby logger.
32
+
33
+ To run an independent instance simply create a new log object
34
+
35
+ ```ruby
36
+ require 'oohlalog'
37
+ Oohlalog.api_key = "YOUR API KEY HERE"
38
+
39
+ logger = Oohlalog::Logger.new(100, Oohlalog::Logger.DEBUG) #First argument is the buffer size, second is log level
40
+
41
+ logger.warn("Warning Log")
42
+ logger.info("Info log")
43
+ logger.debug("Debug log message")
44
+ logger.error("Error message")
45
+ logger.fatal("Fatal error message")
46
+ ```
47
+
48
+ TODO: Implement Counter support
49
+
50
+
51
+ ## Contributing
52
+
53
+ 1. Fork it
54
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
55
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
56
+ 4. Push to the branch (`git push origin my-new-feature`)
57
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,19 @@
1
+ module Oohlalog
2
+ module BufferedLogger
3
+ extend ActiveSupport::Concern
4
+ included do
5
+ @@oohla_logger ||= Oohlalog::Logger.new()
6
+ alias_method_chain :add, :oohlalog
7
+ end
8
+
9
+ private
10
+
11
+ def add_with_oohlalog(severity, message, prog_name, &block)
12
+ if severity >= self.level
13
+ @@oohla_logger.add(severity, message, nil)
14
+ end
15
+ add_without_oohlalog(severity, message, prog_name, &block)
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,21 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ class Oohlalog::Counter
5
+ def initialzie
6
+ end
7
+
8
+ def increment(code,counter=1)
9
+ Thread.new do
10
+ begin
11
+ request = Net::HTTP::Post.new("/api/counter/increment?apiKey=#{Oohlalog.api_key}&incr=#{counter}",{'Content-Type' =>'application/json'})
12
+
13
+ http_net = Net::HTTP.new(Oohlalog.host, Oohlalog.port)
14
+ http_net.read_timeout = 5
15
+ http_net.start {|http| http.request(request) }
16
+ rescue
17
+ # puts "Oohlalog Exception**:"
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,105 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'socket'
4
+
5
+ class Oohlalog::Logger
6
+ module Severity
7
+ DEBUG = 0
8
+ INFO = 1
9
+ WARN = 2
10
+ ERROR = 3
11
+ FATAL = 4
12
+ UNKNOWN = 5
13
+ end
14
+ include Severity
15
+
16
+ def initialize(buffer_size=100, level = DEBUG, options={})
17
+ @api_key = options["api_key"] || options[:api_key] || Oohlalog.api_key
18
+ @buffer_size = buffer_size
19
+ @buffer = []
20
+ self.level = level
21
+ end
22
+
23
+ def add(severity, message, category=nil, details=nil)
24
+ return if message.nil? || message.empty?
25
+ if severity >= self.level
26
+ @buffer << {level: severity_string(severity), message: message.gsub(/\e\[(\d+)m/, '') , category: category, details: details, timestamp:Time.now.to_i * 1000, hostName: Socket.gethostname}
27
+ check_buffer_size
28
+ end
29
+ end
30
+
31
+ Severity.constants.each do |severity|
32
+ define_method severity.downcase do |message, category=nil, details=nil|
33
+ add(Severity.const_get(severity), message, category, details)
34
+ end
35
+ end
36
+
37
+ def level=(l)
38
+ @level = l
39
+ end
40
+
41
+ def level
42
+ @level
43
+ end
44
+
45
+ def severity_string(severity_level)
46
+ Severity.constants.each do |severity|
47
+ if Severity.const_get(severity) == severity_level
48
+ return severity.downcase
49
+ end
50
+ end
51
+ return "unknown"
52
+ end
53
+
54
+ def flush_buffer
55
+ return if @buffer.size == 0
56
+
57
+ payload = { logs: [] }
58
+ Thread.new do
59
+ while @buffer.size > 0
60
+ entry = @buffer.shift
61
+ payload[:logs] << entry
62
+ end
63
+ send_payload(payload)
64
+ end
65
+ end
66
+
67
+ private
68
+ def send_payload(payload)
69
+ begin
70
+ request = Net::HTTP::Post.new("#{Oohlalog.path}?apiKey=#{@api_key}",{'Content-Type' =>'application/json'})
71
+ request.body = payload.to_json
72
+ http_net = Net::HTTP.new(Oohlalog.host, Oohlalog.port)
73
+ http_net.read_timeout = 5
74
+ http_net.start {|http| http.request(request) }
75
+ rescue Exception => ex
76
+ # puts "Oohlalog Exception**: #{ex}"
77
+ end
78
+ end
79
+
80
+ def check_buffer_size
81
+ if @buffer.size >= @buffer_size
82
+ flush_buffer
83
+ end
84
+ reset_watchdog
85
+ end
86
+
87
+ def reset_watchdog
88
+ terminate_watchdog
89
+ init_watchdog
90
+ end
91
+
92
+ def terminate_watchdog
93
+ if @watchdog.nil? == false
94
+ @watchdog.terminate
95
+ @watchdog = nil
96
+ end
97
+ end
98
+
99
+ def init_watchdog
100
+ @watchdog = Thread.new do
101
+ sleep 5
102
+ flush_buffer if @buffer.size > 0
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,23 @@
1
+ module Oohlalog
2
+ if defined?(Rails) && Rails::VERSION::MAJOR == 3
3
+ require 'oohlalog/buffered_logger'
4
+ class Railtie < Rails::Railtie
5
+ initializer "oohlalog" do |app|
6
+ ActiveSupport::BufferedLogger.instance_eval do
7
+ include BufferedLogger
8
+ end
9
+ end
10
+ end
11
+ elsif defined?(Rails) && Rails::VERSION::MAJOR == 4
12
+ require 'logger'
13
+ require 'oohlalog/buffered_logger'
14
+ class Railtie < Rails::Railtie
15
+ initializer "oohlalog" do |app|
16
+ ::Logger.instance_eval do
17
+ include BufferedLogger
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ end
@@ -0,0 +1,3 @@
1
+ module Oohlalog
2
+ VERSION = "0.0.1"
3
+ end
data/lib/oohlalog.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'oohlalog/version'
3
+ require 'oohlalog/logger'
4
+ require 'oohlalog/counter'
5
+ require 'oohlalog/railtie'
6
+
7
+ module Oohlalog
8
+ @host = "oohlalog.com"
9
+ @port = 80
10
+ @path = "/api/logging/save.json"
11
+ @api_key = nil
12
+ class << self
13
+ attr_accessor :api_key, :host, :port, :path
14
+ end
15
+ end
data/oohlalog.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'oohlalog/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "oohlalog"
8
+ gem.version = Oohlalog::VERSION
9
+ gem.authors = ["David Estes"]
10
+ gem.email = ["destes@redwindsw.com"]
11
+ gem.description = "Logger for Ooohlalog.com"
12
+ gem.summary = "Temporary summary"
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oohlalog
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David Estes
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-26 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Logger for Ooohlalog.com
15
+ email:
16
+ - destes@redwindsw.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - lib/oohlalog.rb
27
+ - lib/oohlalog/buffered_logger.rb
28
+ - lib/oohlalog/counter.rb
29
+ - lib/oohlalog/logger.rb
30
+ - lib/oohlalog/railtie.rb
31
+ - lib/oohlalog/version.rb
32
+ - oohlalog.gemspec
33
+ homepage: ''
34
+ licenses: []
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project:
53
+ rubygems_version: 1.8.25
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: Temporary summary
57
+ test_files: []