logger-better 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ff288b763a26f53f6a1d762c4cafa28541d48865
4
- data.tar.gz: 9a278ad87bf71421485b3fa2acb76d24123042a6
3
+ metadata.gz: b6d88295a70c17338039ffd8e70b35019aff3d61
4
+ data.tar.gz: dfe4d323fd414fc884bb00389a14e073a30990bb
5
5
  SHA512:
6
- metadata.gz: ec084359da6bec5f41efec43f1ac6aa91de086cd4c97d61cee861eb0dc481cfdba90a3de34dbd1c8ce5abb065ca48f1d8861a4a3bd8b6177958b868ebd86b1a1
7
- data.tar.gz: 25d8c7756ae7b57200ed9a54773a02603bb88c8789a4dcdd5072af2462e796c1d30284fc071b30b151add3a75e5e41ca0b4b78aec2d9a6c9c1f85802fdc205b5
6
+ metadata.gz: 03407ced98200a6e2cb6439f25cfbf923cee0604a93d3935804a6492dc9d4d54c252f9f3c22d938b9603ac23ef61c7c2e16e7c19e8d2b2fc97caab6cd4688a05
7
+ data.tar.gz: 58df092fccffee92e549a9e5d18f36215ae9c62c931a3ccac53fb73591ef78f0e92f25eab6753b43bfb8c11cffa1cbdfdb5957a28de77c42c6d3751231ce8f97
data/README.md CHANGED
@@ -1,6 +1,9 @@
1
1
  # logger-better
2
2
 
3
- Simple monkey patch to stdlib's `Logger` to make it easier to use.
3
+ Simple `Logger` subclasses to make it easier to use and print more
4
+ compact log messages. It also contains modules you can monkey patch
5
+ into `Logger` if you don't want to use the subclasses, or don't have
6
+ control over objects instantiating `Logger` instances.
4
7
 
5
8
  I wrote this gem because I've done this same basic thing in many
6
9
  projects and got tired of it.
@@ -22,10 +25,52 @@ Or install it yourself as:
22
25
  ## Enhancements
23
26
 
24
27
  * Default log format is more readable: `2013-08-10T15:19:21Z [app#68530] INFO: hi`
25
- * Set `logger.level` with symbols. `logger.level = :info`
28
+ * Includes a formatter without timestamps. Useful when your logging
29
+ solution automatically inserts them.
30
+ * Set `logger.level` with symbols. `logger.level = :info` (Works
31
+ nicely with `ENV['LOG_LEVEL']`)
26
32
  * Provide a `NullLogger` object to be used in cases where you need to
27
33
  tell some code to shut up.
28
34
 
35
+ ## Usage
36
+
37
+ logger-better has two modes: subclasses and monkey patches to the
38
+ `Logger` class. Monkey patching is **OPT IN**. This library includes a
39
+ `Logger::Better` subclass. It just a subclass with the appropriate
40
+ modules included. `Logger::Better` instances will use bundled
41
+ formatter by default. `Logger::Better` also provides a factory for
42
+ creating timestampless loggers (using one of the bundled formatters).
43
+
44
+ ### Sublcass Usage
45
+
46
+ ```ruby
47
+ require 'logger-better'
48
+
49
+ logger = Logger::Better.new $stdout
50
+ logger.info('testing') { 'Hello World' } # => 2013-08-10T15:19:21Z [app#68530] INFO: Hello World
51
+
52
+ # Notimestamp factory method behaves the same as new, just sets the
53
+ # default formatter
54
+ logger = Logger::Better.no_timestamp $stdout
55
+ logger.info('testing') { 'Hello World' } # => [app#68530] INFO: Hello World
56
+ ```
57
+
58
+ ### Monkey Patching Logger
59
+
60
+ ```ruby
61
+ require 'logger/better'
62
+
63
+ logger = Logger.new $stdout
64
+ logger.info('testing') { 'Hello World' } # => 2013-08-10T15:19:21Z [app#68530] INFO: Hello World
65
+
66
+ require 'logger/no_timestamp'
67
+
68
+ # Notimestamp factory method behaves the same as new, just sets the
69
+ # default formatter
70
+ logger = Logger.new $stdout
71
+ logger.info('testing') { 'Hello World' } # => [app#68530] INFO: Hello World
72
+ ```
73
+
29
74
  ## Contributing
30
75
 
31
76
  1. Fork it
@@ -0,0 +1,26 @@
1
+ # Version 0.2.0
2
+
3
+ This version significantly reworks the internals and provides new
4
+ public facing features. This came out of real world experience using
5
+ the library in other projects. This release changes two things:
6
+
7
+ 1. `Logger` is **NO LONGER** monkey patched by default
8
+ 2. Add a formatter that does not include timestamps.
9
+
10
+ The first point was done to make this library a better citizen in the
11
+ ruby community by following this principle: global non-opt-in monkey
12
+ patching is bad and should be avoided. Monkey patching in itself is
13
+ fine, as long as the developers knows it's happening and has control
14
+ over it.
15
+
16
+ The second point is a minor enhancement but goes a long way in keeping
17
+ down the noise. Most distributed logging systems will insert their own
18
+ timestamp on all messages. This becomes annoying when your log
19
+ messages contain timestamps. This creates a bunch of noise and
20
+ pointless duplication. This functionality is available either via
21
+ monkey patch or subclass.
22
+
23
+ ## Upgrading from 0.1.x
24
+
25
+ 0.2.x is backward compatible with 0.1.0 depending on how the library
26
+ is required. `require 'logger/better'` will keep the 0.1.x behavior.
data/Rakefile CHANGED
@@ -1,8 +1,20 @@
1
1
  require "bundler/gem_tasks"
2
2
  require 'rake/testtask'
3
3
 
4
- Rake::TestTask.new :test do |t|
5
- t.test_files = Dir['test/**/*_test.rb']
4
+ namespace :test do
5
+ Rake::TestTask.new :lib do |t|
6
+ t.test_files = Dir['test/**/*_test.rb']
7
+ end
8
+
9
+ task :require do
10
+ require 'bundler/setup'
11
+ require 'logger/better'
12
+ require 'logger/no_timestamp'
13
+ end
14
+
15
+ task ci: [ :lib, :require ]
6
16
  end
7
17
 
18
+ task test: 'test:ci'
19
+
8
20
  task default: :test
@@ -0,0 +1,3 @@
1
+ machine:
2
+ ruby:
3
+ version: 2.1.0
@@ -1,4 +1,101 @@
1
- require "logger-better/version"
2
-
3
- require_relative 'logger/better'
4
1
  require_relative 'logger/null_logger'
2
+
3
+ require 'logger'
4
+ require 'time'
5
+
6
+ class Logger
7
+ class Better < Logger
8
+ module LevelPatch
9
+ def level=(value)
10
+ if value.is_a? Symbol
11
+ @level = mapping.fetch(value, Logger::UNKNOWN)
12
+ else
13
+ @level = value
14
+ end
15
+ end
16
+
17
+ def mapping
18
+ {
19
+ debug: Logger::DEBUG,
20
+ info: Logger::INFO,
21
+ warn: Logger::WARN,
22
+ error: Logger::ERROR,
23
+ fatal: Logger::FATAL,
24
+ unknown: Logger::UNKNOWN
25
+ }
26
+ end
27
+ end
28
+
29
+ include LevelPatch
30
+
31
+ class << self
32
+ def no_timestamp(*args, &block)
33
+ new(*args, &block).tap do |logger|
34
+ logger.formatter = NoTimestampFormatter.new
35
+ end
36
+ end
37
+ end
38
+
39
+ def initialize(*)
40
+ super
41
+ @formatter = TimestampFormatter.new
42
+ end
43
+
44
+ class TimestampFormatter < Formatter
45
+ def call(severity, time, progname, msg)
46
+ format % [
47
+ time.utc.iso8601,
48
+ progname,
49
+ $$,
50
+ severity,
51
+ msg2str(msg).strip
52
+ ]
53
+ end
54
+
55
+ def format
56
+ "%s [%s#%d] %5s: %s\n"
57
+ end
58
+ end
59
+
60
+ class NoTimestampFormatter < Formatter
61
+ def call(severity, time, progname, msg)
62
+ format % [
63
+ progname,
64
+ $$,
65
+ severity,
66
+ msg2str(msg).strip
67
+ ]
68
+ end
69
+
70
+ def format
71
+ "[%s#%d] %5s: %s\n"
72
+ end
73
+ end
74
+ end
75
+
76
+ module BetterPatch
77
+ def initialize(*)
78
+ super
79
+ @formatter = Better::TimestampFormatter.new
80
+ end
81
+
82
+ class << self
83
+ def included(base)
84
+ base.include Better::LevelPatch
85
+ end
86
+ end
87
+ end
88
+
89
+ module NoTimestampPatch
90
+ def initialize(*)
91
+ super
92
+ @formatter = Better::NoTimestampFormatter.new
93
+ end
94
+
95
+ class << self
96
+ def included(base)
97
+ base.include Better::LevelPatch
98
+ end
99
+ end
100
+ end
101
+ end
@@ -1,39 +1,5 @@
1
- require 'logger'
2
- require 'time'
1
+ require 'logger-better'
3
2
 
4
3
  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 % [
27
- time.utc.iso8601,
28
- progname,
29
- $$,
30
- severity,
31
- msg2str(msg).strip
32
- ]
33
- end
34
-
35
- def format
36
- "%s [%s#%d] %5s: %s\n"
37
- end
38
- end
4
+ include BetterPatch
39
5
  end
@@ -0,0 +1,5 @@
1
+ require 'logger-better'
2
+
3
+ class Logger
4
+ include NoTimestampPatch
5
+ end
@@ -1,11 +1,7 @@
1
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
2
  Gem::Specification.new do |spec|
7
3
  spec.name = "logger-better"
8
- spec.version = Logger::Better::VERSION
4
+ spec.version = "0.2.0"
9
5
  spec.authors = ["ahawkins"]
10
6
  spec.email = ["adam@hawkins.io"]
11
7
  spec.description = %q{Simple monkey patch to stdlib's Logger to make it easier to use.}
@@ -0,0 +1,23 @@
1
+ require_relative 'test_helper'
2
+
3
+ class BetterPatchTest < MiniTest::Unit::TestCase
4
+ def test_patching_sets_correct_formatter_on_new_instances
5
+ patched_logger = Class.new(Logger) do
6
+ include Logger::BetterPatch
7
+ end
8
+
9
+ logger = patched_logger.new $stdout
10
+ assert_instance_of Logger::Better::TimestampFormatter, logger.formatter
11
+ end
12
+
13
+ def test_patching_allows_setting_level_by_symbol
14
+ patched_logger = Class.new(Logger) do
15
+ include Logger::BetterPatch
16
+ end
17
+
18
+ logger = patched_logger.new $stdout
19
+
20
+ logger.level = :debug
21
+ assert_equal Logger::DEBUG, logger.level
22
+ end
23
+ end
@@ -1,12 +1,15 @@
1
1
  require_relative 'test_helper'
2
- require 'logger/better'
3
- require 'stringio'
4
2
 
5
3
  class BetterTest < MiniTest::Unit::TestCase
6
4
  attr_reader :formatter
7
5
 
8
6
  def setup
9
- @formatter = Logger::Formatter.new
7
+ @formatter = Logger::Better::TimestampFormatter.new
8
+ end
9
+
10
+ def test_uses_the_correct_formatter
11
+ logger = Logger::Better.new $stdout
12
+ assert_instance_of Logger::Better::TimestampFormatter, logger.formatter
10
13
  end
11
14
 
12
15
  def test_uses_utc_iso_8601_times
@@ -51,7 +54,7 @@ class BetterTest < MiniTest::Unit::TestCase
51
54
  end
52
55
 
53
56
  def test_can_set_level_with_symbol
54
- logger = Logger.new StringIO.new
57
+ logger = Logger::Better.new StringIO.new
55
58
 
56
59
  logger.level = :debug
57
60
  assert_equal Logger::DEBUG, logger.level
@@ -68,7 +71,7 @@ class BetterTest < MiniTest::Unit::TestCase
68
71
  end
69
72
 
70
73
  def tests_sets_level_unkown_when_passing_an_unkown_symol
71
- logger = Logger.new StringIO.new
74
+ logger = Logger::Better.new StringIO.new
72
75
 
73
76
  logger.level = :foo
74
77
  assert_equal Logger::UNKNOWN, logger.level
@@ -0,0 +1,23 @@
1
+ require_relative 'test_helper'
2
+
3
+ class NoTimestampPatchTest < MiniTest::Unit::TestCase
4
+ def test_patching_sets_correct_formatter_on_new_instances
5
+ patched_logger = Class.new(Logger) do
6
+ include Logger::NoTimestampPatch
7
+ end
8
+
9
+ logger = patched_logger.new $stdout
10
+ assert_instance_of Logger::Better::NoTimestampFormatter, logger.formatter
11
+ end
12
+
13
+ def test_patching_allows_setting_level_by_symbol
14
+ patched_logger = Class.new(Logger) do
15
+ include Logger::BetterPatch
16
+ end
17
+
18
+ logger = patched_logger.new $stdout
19
+
20
+ logger.level = :debug
21
+ assert_equal Logger::DEBUG, logger.level
22
+ end
23
+ end
@@ -0,0 +1,57 @@
1
+ require_relative 'test_helper'
2
+
3
+ class NoTimestampTest < MiniTest::Unit::TestCase
4
+ attr_reader :formatter
5
+
6
+ def setup
7
+ @formatter = Logger::Better::NoTimestampFormatter.new
8
+ end
9
+
10
+ def test_building_loggers_without_timestamp
11
+ logger = Logger::Better.no_timestamp $stdout
12
+ assert_instance_of Logger::Better::NoTimestampFormatter, logger.formatter
13
+ end
14
+
15
+ def test_does_not_include_the_time
16
+ time = Time.now
17
+ log = formatter.call 'info', time, 'app', 'hi'
18
+ refute_includes log, time.utc.iso8601
19
+ end
20
+
21
+ def test_progname_is_first
22
+ log = formatter.call 'info', Time.now, 'app', 'hi'
23
+ # NOTE: format includes [ ] around progrname & PID, so this test
24
+ # really asserts that it's the first "text" in the output
25
+ assert_equal 1, log.index('app')
26
+ end
27
+
28
+ def test_pid_comes_after_progname
29
+ log = formatter.call 'info', Time.now, 'app', 'hi'
30
+ assert_before log, $$.to_s, 'app'
31
+ end
32
+
33
+ def test_level_comes_after_progname
34
+ log = formatter.call 'info', Time.now, 'app', 'hi'
35
+ assert_before log, 'info', $$.to_s
36
+ end
37
+
38
+ def test_message_comes_last
39
+ log = formatter.call 'info', Time.now, 'app', 'hi'
40
+ assert_before log, 'hi', 'info'
41
+ end
42
+
43
+ def test_removes_extra_new_lines_from_end_of_message
44
+ message = "!\n\n"
45
+
46
+ log = formatter.call 'info', Time.now, 'app', message
47
+ # strip here because all lines will have a new line.
48
+ assert_equal '!', log.strip[-1]
49
+ end
50
+
51
+ private
52
+ def assert_before(string, second, first)
53
+ assert_includes string, first
54
+ assert_includes string, second
55
+ assert string.index(first) < string.index(second)
56
+ end
57
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logger-better
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ahawkins
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-07 00:00:00.000000000 Z
11
+ date: 2015-03-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -49,13 +49,18 @@ files:
49
49
  - Gemfile
50
50
  - LICENSE.txt
51
51
  - README.md
52
+ - RELEASE_NOTES.md
52
53
  - Rakefile
54
+ - circle.yml
53
55
  - lib/logger-better.rb
54
- - lib/logger-better/version.rb
55
56
  - lib/logger/better.rb
57
+ - lib/logger/no_timestamp.rb
56
58
  - lib/logger/null_logger.rb
57
59
  - logger-better.gemspec
60
+ - test/better_patch_test.rb
58
61
  - test/better_test.rb
62
+ - test/no_timestamp_patch_test.rb
63
+ - test/no_timestamp_test.rb
59
64
  - test/null_logger_test.rb
60
65
  - test/test_helper.rb
61
66
  homepage: https://github.com/ahawkins/logger-better
@@ -83,6 +88,9 @@ signing_key:
83
88
  specification_version: 4
84
89
  summary: Simple monkey patch to stdlib's Logger to make it easier to use.
85
90
  test_files:
91
+ - test/better_patch_test.rb
86
92
  - test/better_test.rb
93
+ - test/no_timestamp_patch_test.rb
94
+ - test/no_timestamp_test.rb
87
95
  - test/null_logger_test.rb
88
96
  - test/test_helper.rb
@@ -1,5 +0,0 @@
1
- class Logger
2
- module Better
3
- VERSION = "0.1.3"
4
- end
5
- end