swoop 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5e59ecc6235c38f57cf06ef8a1357feccbb7c519
4
+ data.tar.gz: bccfc97e1f80e6ab3fc82c0361fe6537c6ce17de
5
+ SHA512:
6
+ metadata.gz: f98f8839aabcd1953dfd11159e54751196608d87d99ef92043b346a7ad7bd57c6b1199f32e3bd93ab95779a4de5eab1901897291253fdb1053798389ef795006
7
+ data.tar.gz: 23c55f1be381aede06c23d820cb81aab168f6377668a9be7323ce39254f55467ed6bb3243c5f4908ff7df119e9ce6a2ff9951eda2e3aedab3906d52375dc6d27
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+ /.yardoc/
12
+ /_yardoc/
13
+ /doc/
14
+ /rdoc/
15
+ /.bundle/
16
+ /lib/bundler/man/
17
+ Gemfile.lock
18
+ .ruby-version
19
+ .ruby-gemset
20
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "https://rubygems.org"
2
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Philip Vieira
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,2 @@
1
+ # Swoop
2
+ Ruby gem for dxw flavored rails logging
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/lib/swoop.rb ADDED
@@ -0,0 +1,21 @@
1
+ require "swoop/version"
2
+
3
+ module Swoop
4
+
5
+ require "swoop/rails"
6
+ require "swoop/formatter"
7
+ require "swoop/railtie" if defined?(::Rails::Railtie)
8
+
9
+ def self.colorize?
10
+ self.load_dependencies
11
+ return false unless defined?(::Colored)
12
+ defined?(::Rails) ? ::Rails.configuration.colorize_logging : true
13
+ end
14
+
15
+ def self.load_dependencies
16
+ require "colored"
17
+ rescue LoadError
18
+ return nil
19
+ end
20
+
21
+ end
@@ -0,0 +1,22 @@
1
+ class Swoop::Formatter
2
+
3
+ def call(severity, datetime, progname, msg)
4
+ if Swoop.colorize?
5
+ "#{ severity[0].bold.send(severity_color(severity)) } #{ datetime.iso8601.bold.black } #{ (progname || "mytvh-web").bold }: #{ msg.strip }\n"
6
+ else
7
+ "#{ severity[0] } #{ datetime.iso8601 } #{ progname || "rails" }: #{ msg.strip }\n"
8
+ end
9
+ end
10
+
11
+ def severity_color(severity)
12
+ case severity
13
+ when "DEBUG" then :black
14
+ when "INFO" then :blue
15
+ when "WARN" then :yellow
16
+ when "ERROR" then :red
17
+ when "FATAL" then :red
18
+ else :black
19
+ end
20
+ end
21
+
22
+ end
@@ -0,0 +1,38 @@
1
+ module Swoop
2
+ module Formatters
3
+ class ColoredKeyValue
4
+
5
+ def call(data)
6
+ fields = fields_to_display(data)
7
+ event = fields.map { |key| format(key, data[key]) }
8
+ event.join(' ')
9
+ end
10
+
11
+ def fields_to_display(data)
12
+ data.keys
13
+ end
14
+
15
+ def format(key, value)
16
+ if key == :error
17
+ value = "'#{value}'"
18
+ else
19
+ value = Kernel.format('%.2f', value) if value.is_a? Float
20
+ end
21
+ return Swoop.colorize? ? "#{ key.to_s.yellow }=#{ colorize(value) }" : "#{ key }=#{ value }"
22
+ end
23
+
24
+ def colorize(value)
25
+ if value.is_a?(Numeric) || value.to_s.match(/\A[\d\.]+\z/)
26
+ value = "#{ value }".magenta.bold
27
+ elsif value.is_a?(String)
28
+ value = "#{ value }".green.bold
29
+ elsif value.is_a?(Symbol)
30
+ value = "#{ value }".blue.bold
31
+ elsif value.is_a?(Hash)
32
+ value = value.to_json.black.bold
33
+ end
34
+ end
35
+
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,25 @@
1
+ class Swoop::Rails
2
+
3
+ def self.set_logger(config)
4
+ ::Rails.logger = config.logger = swoop_logger(config)
5
+ end
6
+
7
+ def self.swoop_logger(config)
8
+ logger = if ENV["LOG_FILE"]
9
+ ::ActiveSupport::Logger.new ::Rails.root.join(ENV["LOG_FILE"])
10
+ else
11
+ ::STDOUT.sync = true
12
+ ::ActiveSupport::Logger.new(::STDOUT)
13
+ end
14
+
15
+ logger.level = Logger.const_get(log_level)
16
+ logger.progname = config.log_progname
17
+ logger.formatter = Swoop::Formatter.new
18
+ return logger
19
+ end
20
+
21
+ def self.log_level
22
+ ([(ENV["LOG_LEVEL"] || ::Rails.application.config.log_level).to_s.upcase, "INFO"] & %w[DEBUG INFO WARN ERROR FATAL UNKNOWN]).compact.first
23
+ end
24
+
25
+ end
@@ -0,0 +1,19 @@
1
+ class Swoop::Railtie < ::Rails::Railtie
2
+
3
+ config.log_progname ||= nil if config.respond_to?(:log_progname=)
4
+ config.before_initialize { Swoop::Rails.set_logger(config) }
5
+
6
+ if defined?(::Lograge)
7
+
8
+ require "swoop/formatters/colored_key_value"
9
+
10
+ config.lograge.enabled = true
11
+ config.lograge.formatter = Swoop::Formatters::ColoredKeyValue.new
12
+ config.lograge.custom_options = lambda do |event|
13
+ params = event.payload[:params].reject { |k| ["controller", "action"].include? k }
14
+ { "params" => params, "ip" => event.payload[:ip] }
15
+ end
16
+
17
+ end
18
+
19
+ end
@@ -0,0 +1,3 @@
1
+ module Swoop
2
+ VERSION = "0.0.4"
3
+ end
data/swoop.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'swoop/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "swoop"
8
+ spec.version = Swoop::VERSION
9
+ spec.authors = ["Philip Vieira"]
10
+ spec.email = ["philip@dxw.com"]
11
+ spec.summary = %q{Ruby gem for dxw flavored rails logging.}
12
+ spec.homepage = "https://github.com/dxw/swoop"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.6"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: swoop
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Philip Vieira
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-19 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
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
+ description:
42
+ email:
43
+ - philip@dxw.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/swoop.rb
54
+ - lib/swoop/formatter.rb
55
+ - lib/swoop/formatters/colored_key_value.rb
56
+ - lib/swoop/rails.rb
57
+ - lib/swoop/railtie.rb
58
+ - lib/swoop/version.rb
59
+ - swoop.gemspec
60
+ homepage: https://github.com/dxw/swoop
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.2.2
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: Ruby gem for dxw flavored rails logging.
84
+ test_files: []