plogger 0.2.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: 110fbede7ba261da02ee2861e3d5375e0e31ccea
4
+ data.tar.gz: 0a832143e20bfa24ad41746b10d4f6569e6aa3de
5
+ SHA512:
6
+ metadata.gz: edcab56c3b72cc592f5ceaab0bb6a7f6d5935cf24d6f1ec6092c03f94d8fc6398f0c943316091c84951a0af941cf67d9239deab788e89a239c5ab50b3a1fddc0
7
+ data.tar.gz: 1250d98bd14d0a5cdd805badf2cf9a7f16475c5bc0e27bc310faf75107668601c55bf3a11cf52cef6308644227e7c5381d3a6851083eb237e600397a98a493f0
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.0
5
+ before_install: gem install bundler -v 1.13.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in Plogger.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Daniel Merrill
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/Plogger.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'Plogger/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "plogger"
8
+ spec.version = Plogger::VERSION
9
+ spec.authors = ["Daniel Merrill"]
10
+ spec.email = ["daniel@papinotas.com"]
11
+
12
+ spec.summary = "Phoenix Logging Interface."
13
+ spec.description = "Provides a standard way to generate logs in the application."
14
+ spec.homepage = "https://github.com/Papinotas-Desarrollo/Plogger"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.13"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_dependency "sentry-raven", '~> 2.7.1'
28
+ end
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # Plogger
2
+
3
+ ## Introduction
4
+
5
+ Plogger stands for Papinotas Logger or Phoenix Logger (our current project, which raised the need for this gem). Plogger is a simple logging gem which receives a little more information than `Rails.Logger`, and can also send an exception to third party APIs (currently only Raven is supported). Plogger outputs the log in a parser friendly format, ready for other software such as Logstash to process it.
6
+
7
+ ## Instalation
8
+
9
+ Add `gem 'plogger'` to your gemfile.
10
+
11
+ ## Configuring
12
+
13
+ ```ruby
14
+ # environments/[Environment].rb
15
+ Rails.application.configure do
16
+ Plogger.configure do |config|
17
+ config.logger = ActiveSupport::Logger.new(STDOUT) # Or whatever logger you want to configure
18
+ config.module = 'MainModule' # Optional
19
+ config.raven_dsn = 'http://public:secret@example.com/project-id' # Optional
20
+ end
21
+ end
22
+ ```
23
+
24
+ ## Usage
25
+
26
+ The usage is similar to how you use the Rails Logger, but you can also log exceptions.
27
+
28
+ ```ruby
29
+ begin
30
+ 1/0
31
+ rescue e
32
+ trace = Plogger.exception(e, category: 'Bundle Creation')
33
+ # => trace = "130AH93MWS2"
34
+ return render "Too bad, an exception was raised. Read the full error at #{trace}"
35
+ end
36
+ trace_2 = Plogger.info("Processing ready", category: 'Bundle Creation')
37
+ render "Your process finished successfully, check the logs at #{trace_2}"
38
+ ```
39
+
40
+ Plogger has the following methods: `exception`, `error`, `warning`, `info`, `debug`. It will use the logger you supplied in the config to output them, so make sure you have enabled the log level you are trying to use.
41
+
42
+ ## Params
43
+
44
+ Each of these methods can receive the following keyword params:
45
+
46
+ | Param | Explanation | Default |
47
+ | ------------- |---------------|-----------|
48
+ | category | Use it to categorize the thing you are logging, so you can later group related logs in Kibana or something |''|
49
+ | type | Use it to manifest if the current log is generated by a system action or a user action | 'system' |
50
+ | user_id | Use it to track the user that is generating the log | ''
51
+ | account_id | In Phoenix we use accounts (a user can have many accounts). You can track that too | '' |
52
+ | extra_info | A hash with additional tags you can print in the log | {} |
53
+
54
+ Example call:
55
+
56
+ ```ruby
57
+ Plogger.info("Processing ready", type: 'user', category: "Bundle Creation", user_id: 1, extra_info: {happy: "yes"})
58
+ # Will output 'Processing ready -- type='user' category='Bundle Creation' user_id=1 happy='yes'
59
+ ```
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 "Plogger"
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
@@ -0,0 +1,9 @@
1
+ module Plogger
2
+ class Config
3
+ attr_accessor :raven_dsn, :logger, :module
4
+
5
+ def initialize(logger)
6
+ @logger = logger
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ module Plogger
2
+ class Formatter
3
+ def self.format(message, trace, type: 'system', user_id: 'null', account_id: 'null', category: '', extra_info: {})
4
+ result = "#{message} -- type='#{type}' category='#{category}' user_id=#{user_id} account_id=#{account_id} trace=#{trace}"
5
+ extra_info.keys.each do |key|
6
+ is_string = extra_info[key].class == String
7
+ result += " #{key}=#{is_string ? "'" : ''}#{extra_info[key]}#{is_string ? "'" : ''}"
8
+ end
9
+ result
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,70 @@
1
+ require 'Plogger/id_generator'
2
+ require 'Plogger/formatter'
3
+
4
+ module Plogger
5
+ class Handler
6
+ def initialize(config)
7
+ @logger = config.logger
8
+ @module = config.module
9
+ @raven_dsn = config.raven_dsn
10
+ end
11
+
12
+ def handle_exception(exception, type: '', category: '', user_id: nil, account_id: nil, extra_info: {})
13
+ unless @raven_dsn == nil || @raven_dsn.blank?
14
+ Raven.user_context(id: user_id)
15
+ Raven.tags_context(account_id: account_id)
16
+ Raven.capture_exception(exception)
17
+ end
18
+ handle_error(exception.message, category: category, user_id: user_id, account_id: account_id,
19
+ extra_info: extra_info, type: type)
20
+ end
21
+
22
+ def handle_error(message, type: '', category: '', user_id: '', account_id: '', extra_info: {})
23
+ result = generate_id_and_message(message, category: category, user_id: user_id,
24
+ account_id: account_id, type: type,
25
+ extra_info: generate_extra_info(extra_info))
26
+ @logger.error(result[:message])
27
+ result[:trace]
28
+ end
29
+
30
+ def handle_warning(message, type: '', category: '', user_id: '', account_id: '', extra_info: {})
31
+ result = generate_id_and_message(message, category: category, user_id: user_id,
32
+ account_id: account_id, type: type,
33
+ extra_info: generate_extra_info(extra_info))
34
+ @logger.warn(result[:message])
35
+ result[:trace]
36
+ end
37
+
38
+ def handle_info(message, type: '', category: '', user_id: '', account_id: '', extra_info: {})
39
+ result = generate_id_and_message(message, category: category, user_id: user_id,
40
+ account_id: account_id, type: type,
41
+ extra_info: generate_extra_info(extra_info))
42
+ @logger.info(result[:message])
43
+ result[:trace]
44
+ end
45
+
46
+ def handle_debug(message, type: '', category: '', user_id: '', account_id: '', extra_info: {})
47
+ result = generate_id_and_message(message, category: category, user_id: user_id,
48
+ account_id: account_id, type: type,
49
+ extra_info: generate_extra_info(extra_info))
50
+ @logger.debug(result[:message])
51
+ result[:trace]
52
+ end
53
+
54
+ private
55
+
56
+ def generate_id_and_message(message, type: '', category: '', user_id: '', account_id: '', extra_info: {})
57
+ trace = Plogger::IdGenerator.generate
58
+ formatted_message = Plogger::Formatter.format(message, trace, user_id: user_id,
59
+ account_id: account_id,
60
+ category: category,
61
+ type: type,
62
+ extra_info: extra_info)
63
+ {trace: trace, message: formatted_message}
64
+ end
65
+
66
+ def generate_extra_info extra_info
67
+ extra_info.merge({module: @module})
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,11 @@
1
+ module Plogger
2
+ class IdGenerator
3
+ def self.generate
4
+ [('a'..'z'), ('A'..'Z'), (0..9)].map(&:to_a).flatten.shuffle[0, id_length].join
5
+ end
6
+
7
+ def self.id_length
8
+ 10
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module Plogger
2
+ VERSION = "0.2.0"
3
+ end
data/lib/Plogger.rb ADDED
@@ -0,0 +1,50 @@
1
+ require "Plogger/version"
2
+ require 'Plogger/config'
3
+ require 'Plogger/handler'
4
+ require 'logger'
5
+
6
+ module Plogger
7
+ def self.logger
8
+ @@logger ||= defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
9
+ end
10
+
11
+ def self.config
12
+ @@config ||= Plogger::Config.new(logger)
13
+ end
14
+
15
+ def self.configure
16
+ yield(config)
17
+ @@logger = config.logger
18
+ @@config = config
19
+ end
20
+
21
+ def self.exception(exception, type: '', category: '', user_id: '', account_id: '', extra_info: {})
22
+ handler = Plogger::Handler.new(config)
23
+ handler.handle_exception(exception, category: category, user_id: user_id, account_id: account_id,
24
+ extra_info: extra_info)
25
+ end
26
+
27
+ def self.error(message, type: '', category: '', user_id: '', account_id: '', extra_info: {})
28
+ handler = Plogger::Handler.new(config)
29
+ handler.handle_error(message, category: category, user_id: user_id, account_id: account_id,
30
+ extra_info: extra_info)
31
+ end
32
+
33
+ def self.warn(message, type: '', category: '', user_id: '', account_id: '', extra_info: {})
34
+ handler = Plogger::Handler.new(config)
35
+ handler.handle_warning(message, category: category, user_id: user_id, account_id: account_id,
36
+ extra_info: extra_info)
37
+ end
38
+
39
+ def self.info(message, type: '', category: '', user_id: '', account_id: '', extra_info: {})
40
+ handler = Plogger::Handler.new(config)
41
+ handler.handle_info(message, category: category, user_id: user_id, account_id: account_id,
42
+ extra_info: extra_info)
43
+ end
44
+
45
+ def self.debug(message, type: '', category: '', user_id: '', account_id: '', extra_info: {})
46
+ handler = Plogger::Handler.new(config)
47
+ handler.handle_debug(message, category: category, user_id: user_id, account_id: account_id,
48
+ extra_info: extra_info)
49
+ end
50
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: plogger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Merrill
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-04-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sentry-raven
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 2.7.1
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 2.7.1
69
+ description: Provides a standard way to generate logs in the application.
70
+ email:
71
+ - daniel@papinotas.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - Plogger.gemspec
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - lib/Plogger.rb
87
+ - lib/Plogger/config.rb
88
+ - lib/Plogger/formatter.rb
89
+ - lib/Plogger/handler.rb
90
+ - lib/Plogger/id_generator.rb
91
+ - lib/Plogger/version.rb
92
+ homepage: https://github.com/Papinotas-Desarrollo/Plogger
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.5.1
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Phoenix Logging Interface.
116
+ test_files: []