logdna 0.0.1

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: d025f71abf5afb886b942d4092c6e902dbf88ee8
4
+ data.tar.gz: 4748c857fec602e2b4e56217e6f6d908bc99c84a
5
+ SHA512:
6
+ metadata.gz: fe6bba13754c9a64124a2a56401b76a08bcdd97d48fa5e8f62c6808f558fc1c99bc46d821ad78c2506b7482d7b744256c36036420802cf87cac424891fe8e38b
7
+ data.tar.gz: 486b11af0975e3a5543b1792b06d169fc4938f2005f6ec28071b42fcdfb7aa69e48235379a31ab8f91007d9e4283ff550c2dffed0f7e3cb4302018215f35d277
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in logdna_ruby.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 edwin-lai
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,81 @@
1
+ <p align="center">
2
+ <a href="https://app.logdna.com">
3
+ <img height="95" width="201" src="https://raw.githubusercontent.com/logdna/artwork/master/logo%2Bruby.png">
4
+ </a>
5
+ <p align="center">Ruby gem for logging to <a href="https://app.logdna.com">LogDNA</a></p>
6
+ </p>
7
+
8
+ ---
9
+
10
+ * **[Overview](#overview)**
11
+ * **[Installation](#installation)**
12
+ * **[API](#api)**
13
+ * **[Development](#development)**
14
+ * **[Contributing](#contributing)**
15
+ * **[License](#license)**
16
+
17
+ # Overview
18
+
19
+ This gem contains LogDNA::RubyLogger, an extension to the logger from Ruby's standard library, as well as LogDNA::RailsLogger, which inherits from ActiveSupport::Logger from Rails. LogDNA::RailsLogger is only loaded if ActiveSupport is present.
20
+
21
+ # Installation
22
+
23
+ Add this line to your application's Gemfile:
24
+
25
+ ```ruby
26
+ gem 'logdna'
27
+ ```
28
+
29
+ And then execute:
30
+
31
+ $ bundle
32
+
33
+ Or install it yourself as:
34
+
35
+ $ gem install logdna
36
+
37
+ # API
38
+
39
+ ## Shared by LogDNA::RubyLogger and LogDNA::RailsLogger
40
+
41
+ ### ::new(api_key, hostname, options = {})
42
+
43
+ Instantiates a new instance of the class it is called on. api_key and hostname are required.
44
+
45
+ Options:
46
+ * logdev: The log device. This is a filename (String) or IO object (e.g. STDOUT, STDERR, an open file). Default: STDOUT.
47
+ * shift_age: Number of old log files to keep, or frequency of rotation (daily, weekly, or monthly). Default: 7.
48
+ * shift_size: Maximum logfile size (only applies when shift_age is a number). Default: 1,048,576
49
+ * mac: MAC address. Default: nil.
50
+ * ip: IP address. Default: nil.
51
+
52
+ ### \#add
53
+
54
+ Log a message if the given severity is high enough and post it to the LogDNA ingester. This is the generic logging method. Users will be more inclined to use debug, info, warn, error, and fatal (which all call \#add), as [described in the Ruby Logger documentation](https://ruby-doc.org/stdlib-2.3.0/libdoc/logger/rdoc/Logger.html). Note that these methods take a source as the argument and a block which returns a message. It returns the http response.
55
+
56
+ ### \#close_http
57
+
58
+ Close the HTTP connection to LogDNA's ingester.
59
+
60
+ ### \#reopen_http
61
+
62
+ Open another HTTP connection to LogDNA's ingester if the connection is alread closed.
63
+
64
+ ## Only in LogDNA::RubyLogger
65
+
66
+ ### \#<<(message)
67
+
68
+ Dump given message to the log device without any formatting, then posts it to the LogDNA ingester. If no log device exists, return nil.
69
+
70
+ # Development
71
+
72
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
73
+
74
+ # Contributing
75
+
76
+ Bug reports and pull requests are welcome on GitHub at https://github.com/logdna/logdna_ruby.
77
+
78
+ # License
79
+
80
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
81
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "logdna"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/logdna.rb ADDED
@@ -0,0 +1,73 @@
1
+ require 'json'
2
+ require_relative './logdna/version.rb'
3
+ require_relative './logdna/ruby_logger.rb'
4
+ begin
5
+ require_relative './logdna/rails_logger.rb'
6
+ rescue
7
+ end
8
+
9
+ module LogDNA
10
+ INGESTER_DOMAIN = 'https://logs.logdna.com'.freeze
11
+
12
+ LEVELS = {
13
+ 0 => 'DEBUG',
14
+ 1 => 'INFO',
15
+ 2 => 'WARN',
16
+ 3 => 'ERROR',
17
+ 4 => 'FATAL',
18
+ 5 => 'UNKNOWN'
19
+ }.freeze
20
+
21
+ def add(severity, message = nil, progname = nil)
22
+ super
23
+ return true if severity < @level
24
+ message ||= yield
25
+ post_to_logdna(message, severity, progname) if @open
26
+ end
27
+
28
+ def close_http
29
+ return false unless @open
30
+ @conn.close
31
+ @open = false
32
+ true
33
+ end
34
+
35
+ def reopen_http
36
+ return false if @open
37
+ @conn = HTTP.persistent LogDNA::INGESTER_DOMAIN
38
+ @open = true
39
+ end
40
+
41
+ private
42
+
43
+ def fill_opts_with_defaults(opts)
44
+ # defaults from ruby standard library logger documentation
45
+ opts[:logdev] ||= STDOUT
46
+ opts[:shift_age] ||= 7
47
+ opts[:shift_size] ||= 1_048_576
48
+ opts
49
+ end
50
+
51
+ def set_ivars(api_key, hostname, opts)
52
+ raise ArgumentError 'api_key must be a string' unless api_key.is_a?(String)
53
+ @api_key = api_key
54
+ @host = hostname.to_s
55
+ @mac = opts[:mac].to_s
56
+ @ip = opts[:ip].to_s
57
+ @open = true
58
+ end
59
+
60
+ def post_to_logdna(message, level = nil, source = 'none')
61
+ res = @conn.headers(apikey: @api_key, 'Content-Type' => 'application/json')
62
+ .post("/logs/ingest?hostname=#{@host}&mac=#{@mac}&ip=#{@ip}",
63
+ json: request_body(message, level, source))
64
+ res.flush
65
+ end
66
+
67
+ def request_body(message, level, source)
68
+ body = { e: 'line', line: message, timestamp: Time.now.to_i }
69
+ body[:level] = LEVELS[level] if level
70
+ body[:app] = source if source
71
+ body
72
+ end
73
+ end
@@ -0,0 +1,15 @@
1
+ require 'active_support/logger'
2
+ require 'http'
3
+
4
+ module LogDNA
5
+ class RailsLogger < ActiveSupport::Logger
6
+ include LogDNA
7
+
8
+ def initialize(api_key, hostname, options = {})
9
+ @conn = HTTP.persistent LogDNA::INGESTER_DOMAIN
10
+ opts = fill_opts_with_defaults(options)
11
+ super(opts[:logdev], opts[:shift_age], opts[:shift_size])
12
+ set_ivars(api_key, hostname, options)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,20 @@
1
+ require 'logger'
2
+ require 'http'
3
+
4
+ module LogDNA
5
+ class RubyLogger < ::Logger
6
+ include LogDNA
7
+
8
+ def initialize(api_key, hostname, options = {})
9
+ @conn = HTTP.persistent LogDNA::INGESTER_DOMAIN
10
+ opts = fill_opts_with_defaults(options)
11
+ super(opts[:logdev], opts[:shift_age], opts[:shift_size])
12
+ set_ivars(api_key, hostname, options)
13
+ end
14
+
15
+ def <<(msg)
16
+ super
17
+ post_to_logdna(msg)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module LogDNA
2
+ VERSION = "0.0.1"
3
+ end
data/logdna.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'logdna/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'logdna'
8
+ spec.version = LogDNA::VERSION
9
+ spec.authors = ['edwin-lai']
10
+ spec.email = ['edwinlai@ucla.edu']
11
+
12
+ spec.summary = 'LogDNA extension to ruby logger'
13
+ spec.homepage = 'https://github.com/logdna/logdna_ruby'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_runtime_dependency 'http', '~> 2.0', '>= 2.0.3'
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.13'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'rspec', '~> 3.0'
28
+ spec.add_development_dependency 'webmock', '~> 2.1'
29
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logdna
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - edwin-lai
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-10-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: http
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.0.3
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '2.0'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 2.0.3
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.13'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.13'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '10.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '10.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rspec
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '3.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '3.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: webmock
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '2.1'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '2.1'
89
+ description:
90
+ email:
91
+ - edwinlai@ucla.edu
92
+ executables: []
93
+ extensions: []
94
+ extra_rdoc_files: []
95
+ files:
96
+ - ".gitignore"
97
+ - ".rspec"
98
+ - Gemfile
99
+ - LICENSE.txt
100
+ - README.md
101
+ - Rakefile
102
+ - bin/console
103
+ - bin/setup
104
+ - lib/logdna.rb
105
+ - lib/logdna/rails_logger.rb
106
+ - lib/logdna/ruby_logger.rb
107
+ - lib/logdna/version.rb
108
+ - logdna.gemspec
109
+ homepage: https://github.com/logdna/logdna_ruby
110
+ licenses:
111
+ - MIT
112
+ metadata: {}
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.5.1
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: LogDNA extension to ruby logger
133
+ test_files: []