logvisible 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0911f3db7fce87a3ef925f836f650e54a11d53e4
4
+ data.tar.gz: 6910f13e8ba87b1c33ad91d9a642db19cb2faaa1
5
+ SHA512:
6
+ metadata.gz: cc648687b272bd14f03c53d2c4e47bcf07b6e47007fc32a9c9e44b002fc7abde85550ab6f51896f10e213027fdbbcd76cbfa094a5721fc25926a8d75a7602418
7
+ data.tar.gz: cd10dfceb8102cfd51f953efec71d36653e37227db18e62731cdbdedb857934bc563876c32df15f1f9de96b066c34eca31c4a574f1c7d6762697c0bfc7440ce2
@@ -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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in logvisible.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Dan Olson
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.
@@ -0,0 +1,137 @@
1
+ # Logvisible
2
+
3
+ Easy visible logging for Rails!
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'logvisible'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install logvisible
20
+
21
+ ## Usage
22
+
23
+ If you're using Rails, Logvisible will default to using Rails.logger.
24
+ If you're not using Rails, or you'd like to use a different logger:
25
+
26
+ ```ruby
27
+ my_logger = Logger.new('/var/log/my_log')
28
+
29
+ Logvisible.configure do |c|
30
+ c.logger = my_logger
31
+ end
32
+ ```
33
+
34
+ Logvisible exposes two methods that make visible logging easy.
35
+
36
+ `logvisible`, which takes a string and a hash of options and wraps it in a separator.
37
+ Default separator is "\n".
38
+
39
+ Options
40
+
41
+ * `:sep` The string with which to wrap your log statement. Default: "\n"
42
+ * `:level` The log level. One of :debug, :info, :warn, :error, or :fatal. Default: :debug
43
+ * `:text` Text options. A hash, containing these optional keys: :color, :style
44
+ * `:color` Color of the text. One of :black, :red, :green, :yellow, :blue, :magenta, :cyan, or :white
45
+ * `:style` Style of the text. One of :bold, :underline, or :inverse
46
+
47
+
48
+ `logvisible_location`, also takes a string and a hash of options, but creates a tagged
49
+ log containing the class and method where it is called.
50
+
51
+ Options
52
+
53
+ * `:location` The specific string with which your log statement will be tagged. Default: "ClassName#method_name"
54
+ * `:additional_tag` Additional string to be appended to the tag. Default: ''
55
+ * `:level` The log level. One of :debug, :info, :warn, :error, or :fatal. Default: :debug
56
+ * `:text` Text options. A hash, containing these optional keys: :color, :style
57
+ * `:color` Color of the text. One of :black, :red, :green, :yellow, :blue, :magenta, :cyan, or :white
58
+ * `:style` Style of the text. One of :bold, :underline, or :inverse
59
+
60
+ Within models, views, or controllers at the class or instance level scope, toss these methods around!
61
+
62
+ ```ruby
63
+ class Foo < ActiveRecord::Base
64
+
65
+ class << self
66
+ def with_status(status)
67
+ logvisible status
68
+ where(status: status)
69
+ end
70
+ end
71
+
72
+ def foo
73
+ # ...
74
+ logvisible "variable x: #{x.inspect}", text: { style: :inverse, color: :cyan }
75
+ end
76
+
77
+ def bar
78
+ logvisible_location "Tracking the status of account_balance: #{account_balance.inspect}", text: { style: :bold, color: :blue }
79
+ # Some complex calculations ...
80
+ logvisible_location "Tracking the status of account_balance: #{account_balance.inspect}", text: { style: :bold, color: :blue }
81
+ end
82
+
83
+ def baz
84
+ logvisible_location "Tracking the status of account_balance: #{account_balance.inspect}", text: { style: :bold, color: :blue }
85
+ end
86
+ end
87
+ ```
88
+
89
+ Works in controllers:
90
+
91
+ ```ruby
92
+ class FooController < ApplicationController
93
+ def index
94
+ logvisible_location "params: #{params.inspect}", text: { style: :bold, color: :green }
95
+ @foos = Foo.with_status params[:status]
96
+ end
97
+ end
98
+ ```
99
+
100
+ Works in views:
101
+
102
+ ```ruby
103
+ .foos_index
104
+ .foos_container
105
+ - logvisible @foos.inspect
106
+ ```
107
+
108
+ Inside service objects, or any POROs that fall outside the Rails framework, use `Rails` as an explicit receiver:
109
+
110
+ ```ruby
111
+ class SomeServiceObj
112
+ def foo
113
+ # ...
114
+ Rails.logvisible "variable x: #{x.inspect}", text: { style: :underline, color: :red }
115
+ end
116
+ end
117
+ ```
118
+
119
+ Stave off arthritis for a good 8 months: stop typing:
120
+
121
+ ```ruby
122
+ logger.debug("\n\n\n#{value}\n\n\n")
123
+ ```
124
+
125
+ and start using logvisible!
126
+
127
+ ```ruby
128
+ logvisible value
129
+ ```
130
+
131
+ ## Contributing
132
+
133
+ 1. Fork it
134
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
135
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
136
+ 4. Push to the branch (`git push origin my-new-feature`)
137
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,20 @@
1
+ require 'logvisible/version'
2
+ require 'logvisible/settings'
3
+ require 'logvisible/log_methods'
4
+ require 'logvisible/railtie' if defined? Rails
5
+
6
+ module Logvisible
7
+ class << self
8
+ def configure(&block)
9
+ yield settings
10
+ end
11
+
12
+ def settings
13
+ @settings ||= Settings.new
14
+ end
15
+
16
+ def logger
17
+ settings.logger || Rails.logger
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,96 @@
1
+ module Logvisible
2
+ module LogMethods
3
+ LOG_LEVELS = [
4
+ :debug,
5
+ :info,
6
+ :warn,
7
+ :error,
8
+ :fatal
9
+ ]
10
+
11
+ STYLES = {
12
+ bold: "\e[1m",
13
+ underline: "\e[4m",
14
+ inverse: "\e[7m"
15
+ }
16
+
17
+ COLORS = {
18
+ black: "\e[30m",
19
+ red: "\e[31m",
20
+ green: "\e[32m",
21
+ yellow: "\e[33m",
22
+ blue: "\e[34m",
23
+ magenta: "\e[35m",
24
+ cyan: "\e[36m",
25
+ white: "\e[37m"
26
+ }
27
+
28
+ RESET = "\e[0m"
29
+
30
+ def self.included(base)
31
+ base.extend self
32
+ end
33
+
34
+ def logvisible(string, opts={})
35
+ opts = { sep: "\n", level: :debug }.merge opts
36
+ level = _validate_log_level opts[:level]
37
+ sep = opts[:sep].to_s * 5
38
+ msg = sep + string.to_s + sep
39
+ _decorate!(msg, opts[:text]) if opts[:text]
40
+ logger.send level, msg
41
+ end
42
+
43
+ # Decorates messages passed to Rails' TaggedLogger
44
+ def logvisible_location(msg, opts={})
45
+ return logvisible(msg, opts) unless logger.respond_to? :tagged
46
+ opts = { location: find_logvisible_location, additional_tag: '', level: :debug }.merge opts
47
+ tag = opts[:location].to_s + opts[:additional_tag].to_s
48
+ _decorate! tag, opts[:text] if opts[:text]
49
+ _decorate! msg, opts[:text] if opts[:text]
50
+ logger.tagged(tag) do
51
+ logvisible msg, { sep: '', level: opts[:level] }
52
+ end
53
+ end
54
+
55
+ private
56
+
57
+ def logger
58
+ Logvisible.logger
59
+ end
60
+
61
+ def find_logvisible_location
62
+ match = caller[1].match(/`(\w+)'/)
63
+ method = match ? match.captures.first : ''
64
+ location = Class == self.class ? self.name : "#{self.class.name}##{method}"
65
+ end
66
+
67
+ def _decorate!(string, opts={})
68
+ style = _get_styles opts[:style]
69
+ color = _get_color opts[:color]
70
+ string.insert(0, color)
71
+ string.insert(0, style)
72
+ string.insert(-1, _reset!) if string.start_with? "\e["
73
+ end
74
+
75
+ def _reset!
76
+ RESET
77
+ end
78
+
79
+ def _get_styles(style)
80
+ if style.is_a? Array
81
+ style.inject('') { |str, s| str += STYLES[s].to_s }
82
+ else
83
+ STYLES[style].to_s
84
+ end
85
+ end
86
+
87
+ def _get_color(color)
88
+ COLORS[color].to_s
89
+ end
90
+
91
+ def _validate_log_level(level)
92
+ level = :debug unless LOG_LEVELS.include? level
93
+ level
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,8 @@
1
+ module Logvisible
2
+ class Railtie < Rails::Railtie
3
+ Rails.extend Logvisible::LogMethods
4
+ [:action_controller, :active_record, :action_view].each do |framework|
5
+ ActiveSupport.on_load(framework) { include LogMethods }
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,6 @@
1
+ module Logvisible
2
+ class Settings
3
+ attr_accessor :logger
4
+
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ module Logvisible
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'logvisible/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "logvisible"
8
+ spec.version = Logvisible::VERSION
9
+ spec.authors = ["Dan Olson"]
10
+ spec.email = ["daniel.olson@code42.com"]
11
+ spec.description = %q{Create easy to see log statements for debugging in Rails applications}
12
+ spec.summary = %q{Easy visible logging for Rails!}
13
+ spec.homepage = "https://github.com/code42/logvisible"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec", "~> 2.13.0"
24
+ end
@@ -0,0 +1,113 @@
1
+ require 'spec_helper'
2
+
3
+ module Logvisible
4
+ describe LogMethods do
5
+ let(:logger){ stub }
6
+ let(:foo) do
7
+ Class.new do
8
+ include Logvisible::LogMethods
9
+ end
10
+ end
11
+
12
+ before do
13
+ Logvisible.configure do |c|
14
+ c.logger = logger
15
+ end
16
+ end
17
+
18
+ describe '#logvisible' do
19
+
20
+ it 'surrounds the given input with newlines' do
21
+ logger.should_receive(:send).with(:debug, "\n\n\n\n\nfoobar\n\n\n\n\n")
22
+ foo.logvisible 'foobar'
23
+ end
24
+
25
+ it 'accepts a :sep option' do
26
+ logger.should_receive(:send).with(:debug, '$$$$$foobar$$$$$')
27
+ foo.logvisible 'foobar', sep: '$'
28
+ end
29
+
30
+ it 'accepts a :level option' do
31
+ logger.should_receive(:send).with(:fatal, '$$$$$foobar$$$$$')
32
+ foo.logvisible 'foobar', level: :fatal, sep: '$'
33
+ end
34
+
35
+ it 'calls #to_s on the first argument' do
36
+ arg = Object.new
37
+ logger.should_receive(:send).with(:debug, "\n\n\n\n\n#{arg.to_s}\n\n\n\n\n")
38
+ foo.logvisible arg
39
+ end
40
+
41
+ describe 'the :text option' do
42
+ describe 'color support' do
43
+ it 'supports blue' do
44
+ logger.should_receive(:send).with(:debug, "\e[34m\n\n\n\n\nfoobar\n\n\n\n\n\e[0m")
45
+ foo.logvisible 'foobar', text: { color: :blue }
46
+ end
47
+
48
+ it 'supports red' do
49
+ logger.should_receive(:send).with(:debug, "\e[31m\n\n\n\n\nfoobar\n\n\n\n\n\e[0m")
50
+ foo.logvisible 'foobar', text: { color: :red }
51
+ end
52
+
53
+ it 'supports yellow' do
54
+ logger.should_receive(:send).with(:debug, "\e[33m\n\n\n\n\nfoobar\n\n\n\n\n\e[0m")
55
+ foo.logvisible 'foobar', text: { color: :yellow }
56
+ end
57
+
58
+ it 'supports green' do
59
+ logger.should_receive(:send).with(:debug, "\e[32m\n\n\n\n\nfoobar\n\n\n\n\n\e[0m")
60
+ foo.logvisible 'foobar', text: { color: :green }
61
+ end
62
+
63
+ it 'supports magenta' do
64
+ logger.should_receive(:send).with(:debug, "\e[35m\n\n\n\n\nfoobar\n\n\n\n\n\e[0m")
65
+ foo.logvisible 'foobar', text: { color: :magenta }
66
+ end
67
+
68
+ it 'supports cyan' do
69
+ logger.should_receive(:send).with(:debug, "\e[36m\n\n\n\n\nfoobar\n\n\n\n\n\e[0m")
70
+ foo.logvisible 'foobar', text: { color: :cyan }
71
+ end
72
+
73
+ it "doesn't support puce" do
74
+ logger.should_receive(:send).with(:debug, "\n\n\n\n\nfoobar\n\n\n\n\n")
75
+ foo.logvisible 'foobar', text: { color: :puce }
76
+ end
77
+ end
78
+
79
+ describe 'style support' do
80
+ it 'supports bold' do
81
+ logger.should_receive(:send).with(:debug, "\e[1m\n\n\n\n\nfoobar\n\n\n\n\n\e[0m")
82
+ foo.logvisible 'foobar', text: { style: :bold }
83
+ end
84
+
85
+ it 'supports underline' do
86
+ logger.should_receive(:send).with(:debug, "\e[4m\n\n\n\n\nfoobar\n\n\n\n\n\e[0m")
87
+ foo.logvisible 'foobar', text: { style: :underline }
88
+ end
89
+
90
+ it 'supports inverse' do
91
+ logger.should_receive(:send).with(:debug, "\e[7m\n\n\n\n\nfoobar\n\n\n\n\n\e[0m")
92
+ foo.logvisible 'foobar', text: { style: :inverse }
93
+ end
94
+
95
+ it "doesn't support foobar" do
96
+ logger.should_receive(:send).with(:debug, "\n\n\n\n\nfoobar\n\n\n\n\n")
97
+ foo.logvisible 'foobar', text: { style: :foobar }
98
+ end
99
+
100
+ it 'supports multiple styles' do
101
+ logger.should_receive(:send).with(:debug, "\e[7m\e[1m\n\n\n\n\nfoobar\n\n\n\n\n\e[0m")
102
+ foo.logvisible 'foobar', text: { style: [:inverse, :bold] }
103
+ end
104
+
105
+ it 'rejects invalid styles' do
106
+ logger.should_receive(:send).with(:debug, "\e[7m\n\n\n\n\nfoobar\n\n\n\n\n\e[0m")
107
+ foo.logvisible 'foobar', text: { style: [:inverse, :foobar] }
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ describe Logvisible do
4
+ describe 'configuration' do
5
+ it 'accepts a logger' do
6
+ logger = stub
7
+ Logvisible.configure do |c|
8
+ c.logger = logger
9
+ end
10
+
11
+ Logvisible.logger.should == logger
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,12 @@
1
+ require 'logvisible'
2
+
3
+ RSpec.configure do |config|
4
+ config.treat_symbols_as_metadata_keys_with_true_values = true
5
+ config.run_all_when_everything_filtered = true
6
+
7
+ # Run specs in random order to surface order dependencies. If you find an
8
+ # order dependency and want to debug it, you can fix the order by providing
9
+ # the seed, which is printed after each run.
10
+ # --seed 1234
11
+ config.order = 'random'
12
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logvisible
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dan Olson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-06-17 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '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: 2.13.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 2.13.0
55
+ description: Create easy to see log statements for debugging in Rails applications
56
+ email:
57
+ - daniel.olson@code42.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .rspec
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/logvisible.rb
69
+ - lib/logvisible/log_methods.rb
70
+ - lib/logvisible/railtie.rb
71
+ - lib/logvisible/settings.rb
72
+ - lib/logvisible/version.rb
73
+ - logvisible.gemspec
74
+ - spec/lib/logvisible/log_methods_spec.rb
75
+ - spec/lib/logvisible_spec.rb
76
+ - spec/spec_helper.rb
77
+ homepage: https://github.com/code42/logvisible
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.0.3
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Easy visible logging for Rails!
101
+ test_files:
102
+ - spec/lib/logvisible/log_methods_spec.rb
103
+ - spec/lib/logvisible_spec.rb
104
+ - spec/spec_helper.rb