logger-better 0.1.0

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: 4e0c09b94e167758c7a2321fdfd131fa4e41d24a
4
+ data.tar.gz: 0e69278c6e1dfab3f57419303763656588935776
5
+ SHA512:
6
+ metadata.gz: c0b88a14912444091f9397d8f107060e8b2523471c3b9a9c6d94729d3cc45dbb5daa9039830a18593a3714accb6c91d3f655c789693dafd10798f5df1c6aad86
7
+ data.tar.gz: 44c566786298ffb598fa4ba546f52ba04692b907011942d64444da50ff63b467c82a65d925803d450c8bb2bacfdb6bc09e6692c642b8a0c5bd26842e92d006c1
@@ -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 better_logger.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 ahawkins
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,35 @@
1
+ # BetterLogger
2
+
3
+ Simple monkey patch to stdlib's `Logger` to make it easier to use.
4
+
5
+ I wrote this gem because I've done this same basic thing in many
6
+ projects and got tired of it.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'better_logger'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install better_logger
21
+
22
+ ## Enhancements
23
+
24
+ * Default log format is more readable: `2013-08-10T15:19:21Z [app#68530] INFO: hi`
25
+ * Set `logger.level` with symbols. `logger.level = :info`
26
+ * Provide a `NullLogger` object to be used in cases where you need to
27
+ tell some code to shut up.
28
+
29
+ ## Contributing
30
+
31
+ 1. Fork it
32
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
33
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
34
+ 4. Push to the branch (`git push origin my-new-feature`)
35
+ 5. Create new Pull Request
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new :test do |t|
5
+ t.test_files = Dir['test/**/*_test.rb']
6
+ end
7
+
8
+ task default: :test
@@ -0,0 +1,4 @@
1
+ require "logger-better/version"
2
+
3
+ require_relative 'logger/better'
4
+ require_relative 'logger/null_logger'
@@ -0,0 +1,5 @@
1
+ class Logger
2
+ module Better
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,33 @@
1
+ require 'logger'
2
+ require 'time'
3
+
4
+ class Logger
5
+ def level=(value)
6
+ if value.is_a? Symbol
7
+ @level = mapping.fetch(value, UNKNOWN)
8
+ else
9
+ @level = value
10
+ end
11
+ end
12
+
13
+ def mapping
14
+ {
15
+ debug: DEBUG,
16
+ info: INFO,
17
+ warn: WARN,
18
+ error: ERROR,
19
+ fatal: FATAL,
20
+ unknown: UNKNOWN
21
+ }
22
+ end
23
+
24
+ class Formatter
25
+ def call(severity, time, progname, msg)
26
+ format % [time.iso8601, progname, $$, severity, msg2str(msg)]
27
+ end
28
+
29
+ def format
30
+ "%s [%s#%d] %5s: %s\n"
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,13 @@
1
+ require 'logger'
2
+
3
+ class NullLogger < Logger
4
+ def initialize(*args)
5
+
6
+ end
7
+
8
+ # All other methods (debug/info) etc use add internally.
9
+ # It's the lowest level public interface.
10
+ def add(*args, &block)
11
+
12
+ end
13
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'logger-better/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "logger-better"
8
+ spec.version = Logger::Better::VERSION
9
+ spec.authors = ["ahawkins"]
10
+ spec.email = ["adam@hawkins.io"]
11
+ spec.description = %q{Simple monkey patch to stdlib's Logger to make it easier to use.}
12
+ spec.summary = %q{Simple monkey patch to stdlib's Logger to make it easier to use.}
13
+ spec.homepage = "https://github.com/ahawkins/logger-better"
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
+ end
@@ -0,0 +1,81 @@
1
+ require_relative 'test_helper'
2
+ require 'logger/better'
3
+ require 'stringio'
4
+
5
+ class BetterTest < MiniTest::Unit::TestCase
6
+ def test_uses_the_iso_8601_times
7
+ formatter = Logger::Formatter.new
8
+
9
+ time = Time.now
10
+ log = formatter.call 'info', time, 'app', 'hi'
11
+ assert_includes log, time.iso8601
12
+ end
13
+
14
+ def test_starts_with_the_time
15
+ formatter = Logger::Formatter.new
16
+
17
+ time = Time.now
18
+ log = formatter.call 'info', time, 'app', 'hi'
19
+ assert_equal 0, log.index(time.iso8601)
20
+ end
21
+
22
+ def test_progname_comes_after_time
23
+ formatter = Logger::Formatter.new
24
+
25
+ time = Time.now
26
+ log = formatter.call 'info', time, 'app', 'hi'
27
+ assert_before log, 'app', time.iso8601
28
+ end
29
+
30
+ def test_pid_comes_after_progname
31
+ formatter = Logger::Formatter.new
32
+
33
+ log = formatter.call 'info', Time.now, 'app', 'hi'
34
+ assert_before log, $$.to_s, 'app'
35
+ end
36
+
37
+ def test_level_comes_after_progname
38
+ formatter = Logger::Formatter.new
39
+
40
+ log = formatter.call 'info', Time.now, 'app', 'hi'
41
+ assert_before log, 'info', $$.to_s
42
+ end
43
+
44
+ def test_message_comes_last
45
+ formatter = Logger::Formatter.new
46
+
47
+ log = formatter.call 'info', Time.now, 'app', 'hi'
48
+ assert_before log, 'hi', 'info'
49
+ end
50
+
51
+ def test_can_set_level_with_symbol
52
+ logger = Logger.new StringIO.new
53
+
54
+ logger.level = :debug
55
+ assert_equal Logger::DEBUG, logger.level
56
+ logger.level = :info
57
+ assert_equal Logger::INFO, logger.level
58
+ logger.level = :warn
59
+ assert_equal Logger::WARN, logger.level
60
+ logger.level = :error
61
+ assert_equal Logger::ERROR, logger.level
62
+ logger.level = :fatal
63
+ assert_equal Logger::FATAL, logger.level
64
+ logger.level = :unknown
65
+ assert_equal Logger::UNKNOWN, logger.level
66
+ end
67
+
68
+ def tests_sets_level_unkown_when_passing_an_unkown_symol
69
+ logger = Logger.new StringIO.new
70
+
71
+ logger.level = :foo
72
+ assert_equal Logger::UNKNOWN, logger.level
73
+ end
74
+
75
+ private
76
+ def assert_before(string, second, first)
77
+ assert_includes string, first
78
+ assert_includes string, second
79
+ assert string.index(first) < string.index(second)
80
+ end
81
+ end
@@ -0,0 +1,9 @@
1
+ require_relative 'test_helper'
2
+ require 'logger/null_logger'
3
+
4
+ class NullLoggerTest < MiniTest::Unit::TestCase
5
+ def test_add_does_nothing
6
+ logger = NullLogger.new
7
+ logger.add Logger::INFO, 'hi', 'tests'
8
+ end
9
+ end
@@ -0,0 +1,4 @@
1
+ require 'bundler/setup'
2
+ require 'logger-better'
3
+
4
+ require 'minitest/autorun'
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logger-better
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ahawkins
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-11 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
+ description: Simple monkey patch to stdlib's Logger to make it easier to use.
42
+ email:
43
+ - adam@hawkins.io
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/logger-better.rb
54
+ - lib/logger-better/version.rb
55
+ - lib/logger/better.rb
56
+ - lib/logger/null_logger.rb
57
+ - logger-better.gemspec
58
+ - test/better_test.rb
59
+ - test/null_logger_test.rb
60
+ - test/test_helper.rb
61
+ homepage: https://github.com/ahawkins/logger-better
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.2.0
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Simple monkey patch to stdlib's Logger to make it easier to use.
85
+ test_files:
86
+ - test/better_test.rb
87
+ - test/null_logger_test.rb
88
+ - test/test_helper.rb