buffered_logger 0.1.1 → 0.1.2

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.
data/TODO CHANGED
@@ -1,3 +1,3 @@
1
1
  TODO
2
2
  - tests for Buffering module
3
- - tests for Logger (include classic functionality)
3
+ - tests for active_support/buffered_logger functionality
@@ -3,23 +3,29 @@ require 'term/ansicolor'
3
3
  module BufferedLogger::Formatting
4
4
  DEFAULT_LEVEL = :default
5
5
 
6
- private
7
-
8
6
  def initialize
9
7
  @formatter = BufferedLogger::ThreadHash.new { |h,k| h[k] = {} }
8
+ @color = stdout?
10
9
  super()
11
10
  end
12
11
 
12
+ def color?; @color end
13
+ def toggle_color; @color = !@color end
14
+ def disable_color; @color = false end
15
+ def enable_color; @color = true end
16
+
17
+ private
18
+
13
19
  def formatter(severity = DEFAULT_LEVEL)
14
20
  @formatter[Thread.current][severity] || @formatter[master_thread][severity] || default_formatter
15
21
  end
16
22
 
17
23
  def set_formatter(severity = DEFAULT_LEVEL, format = nil)
18
- @formatter[Thread.current][severity] = BufferedLogger::Formatter.new(:format => format, :color => color?)
24
+ @formatter[Thread.current][severity] = BufferedLogger::Formatter.new(:format => format, :logger => self)
19
25
  end
20
26
 
21
27
  def default_formatter
22
- @formatter[master_thread][DEFAULT_LEVEL] ||= BufferedLogger::Formatter.new(:color => color?)
28
+ @formatter[master_thread][DEFAULT_LEVEL] ||= BufferedLogger::Formatter.new(:logger => self)
23
29
  end
24
30
 
25
31
  # magic format getters/setters
@@ -44,7 +50,7 @@ class BufferedLogger::Formatter
44
50
 
45
51
  def initialize(params = {})
46
52
  @format = params[:format] || FORMAT
47
- @color = params[:color] == false ? false : COLOR
53
+ @logger = params[:logger]
48
54
  end
49
55
 
50
56
  # format accessors
@@ -60,11 +66,7 @@ class BufferedLogger::Formatter
60
66
  # color accessors
61
67
 
62
68
  def color?
63
- @color
64
- end
65
-
66
- def toggle_color
67
- @color = !@color
69
+ @logger ? @logger.color? : COLOR
68
70
  end
69
71
 
70
72
  # formatting
@@ -81,15 +83,12 @@ private
81
83
  color_matcher = /#{color_methods.map {|m| "\\$#{m}\\s?"}.join('|')}/
82
84
 
83
85
  strings = message.split(color_matcher)
84
- if color?
85
- colors = message.scan(color_matcher).map { |c| c[1..-1].strip }
86
+ return strings.join if (!color? || strings.empty?)
86
87
 
87
- colored_message = ''
88
- strings[1..-1].each_with_index { |s,i| colored_message << self.send(colors[i], s) }
89
- strings[0] + colored_message
90
- else
91
- strings.join('')
92
- end
88
+ colors = message.scan(color_matcher).map { |c| c[1..-1].strip }
89
+ colored_message = ''
90
+ strings[1..-1].each_with_index { |s,i| colored_message << self.send(colors[i], s) }
91
+ strings[0] + colored_message
93
92
  end
94
93
 
95
94
  end
@@ -1,5 +1,5 @@
1
1
  module BufferedLogger::Indentation
2
- # padding is set at the thread level
2
+ INDENT_LEVEL = 2
3
3
 
4
4
  def initialize
5
5
  @padding = BufferedLogger::ThreadHash.new { |h,k| h[k] = BufferedLogger::Padding.new }
@@ -10,7 +10,7 @@ module BufferedLogger::Indentation
10
10
  @padding[Thread.current]
11
11
  end
12
12
 
13
- def indent(level, &block)
13
+ def indent(level = INDENT_LEVEL, &block)
14
14
  if block_given?
15
15
  padding.indent(level)
16
16
  ret_val = block.call
@@ -34,8 +34,6 @@ class BufferedLogger
34
34
 
35
35
  def initialize(log, level = DEBUG, params = {})
36
36
  @master_thread = Thread.current
37
- super()
38
-
39
37
  @level = severity_to_const(level)
40
38
  if log.respond_to?(:write)
41
39
  @log = log
@@ -45,6 +43,7 @@ class BufferedLogger
45
43
  FileUtils.mkdir_p(File.dirname(log))
46
44
  @log = open_log(log, (File::WRONLY | File::APPEND | File::CREAT))
47
45
  end
46
+ super()
48
47
  params.each { |k,v| SEVERITY_LEVELS.include?(k) ? set_formatter(k, v) : next }
49
48
  end
50
49
 
@@ -69,7 +68,7 @@ class BufferedLogger
69
68
  for severity in SEVERITY_LEVELS
70
69
  class_eval <<-EOT, __FILE__, __LINE__ + 1
71
70
  def #{severity}(message = nil, progname = nil, &block)
72
- add(#{SEVERITY_MAP[severity]}, padding % (formatter(:#{severity}) % message.to_s), progname, &block)
71
+ add(#{SEVERITY_MAP[severity]}, formatter(:#{severity}) % (padding % message.to_s), progname, &block)
73
72
  nil
74
73
  end
75
74
 
@@ -79,6 +78,10 @@ class BufferedLogger
79
78
  EOT
80
79
  end
81
80
 
81
+ def print_blank_line
82
+ add(0, "\n")
83
+ end
84
+
82
85
  # Silences the logger for the duration of the block.
83
86
  def silence(temporary_level = ERROR)
84
87
  if silencer
@@ -95,7 +98,7 @@ class BufferedLogger
95
98
 
96
99
  private
97
100
 
98
- def color?
101
+ def stdout?
99
102
  @log == STDOUT
100
103
  end
101
104
 
@@ -23,17 +23,11 @@ describe BufferedLogger::Formatter do
23
23
  @f.color?.should be_true
24
24
  end
25
25
 
26
- it 'should toggle the color setting' do
27
- @f.toggle_color
28
- @f.color?.should be_false
29
- @f.toggle_color
30
- @f.color?.should be_true
31
- end
32
-
33
26
  it 'should construct a formatter object based on params' do
34
- f = BufferedLogger::Formatter.new(:format => '%s%s%s', :color => false)
27
+ l = BufferedLogger.new(STDOUT)
28
+ f = BufferedLogger::Formatter.new(:format => '%s%s%s', :logger => l)
35
29
  f.to_s.should == '%s%s%s'
36
- f.color?.should be_false
30
+ f.color?.should be_true
37
31
  end
38
32
  end
39
33
 
@@ -58,6 +52,7 @@ describe BufferedLogger do
58
52
  before :each do
59
53
  @f = StringIO.new
60
54
  @l = BufferedLogger.new(@f)
55
+ @l.enable_color
61
56
  end
62
57
 
63
58
  describe 'formatting' do
@@ -77,7 +72,7 @@ describe BufferedLogger do
77
72
  @l.info "haha"
78
73
 
79
74
  # colors are off so the keywords won't be parsed
80
- @f.string.should == "oh\nblah\nhey\nhaha\n"
75
+ @f.string.should == "\e[34moh\e[0m\n\e[31mblah\e[0m\n\e[34mhey\e[0m\n\e[34mhaha\e[0m\n"
81
76
  end
82
77
 
83
78
  it 'should use appropriate formatting for each severity level' do
@@ -85,7 +80,7 @@ describe BufferedLogger do
85
80
  @l.send(:set_formatter, :info, "$blue %s")
86
81
  @l.error 'error'
87
82
  @l.info 'info'
88
- @f.string.should == "error\ninfo\n"
83
+ @f.string.should == "\e[31merror\e[0m\n\e[34minfo\e[0m\n"
89
84
  end
90
85
 
91
86
  it 'should use the master thread formatter if one isnt set' do
@@ -95,7 +90,7 @@ describe BufferedLogger do
95
90
  t = Thread.new do
96
91
  @l.error 'blah'
97
92
  end; t.join
98
- @f.string.should == "test\nblah\n"
93
+ @f.string.should == "\e[31mtest\e[0m\n\e[31mblah\e[0m\n"
99
94
  end
100
95
 
101
96
  it 'should use the master thread default formatter if one isnt set' do
@@ -59,7 +59,7 @@ end
59
59
  describe BufferedLogger do
60
60
  before :each do
61
61
  @f = StringIO.new
62
- @l = BufferedLogger.new(@f)
62
+ @l = BufferedLogger.new(@f, 0, :error => "ERROR: %s")
63
63
  end
64
64
 
65
65
  describe 'indentation' do
@@ -85,8 +85,17 @@ describe BufferedLogger do
85
85
  @l.indent(4) do
86
86
  @l.info 'blah'
87
87
  end
88
+ @l.indent do
89
+ @l.info 'hehe'
90
+ end
88
91
  @l.info 'blah'
89
- @f.string.should == " blah\nblah\n"
92
+ @f.string.should == " blah\n hehe\nblah\n"
93
+ end
94
+
95
+ it "should indent message content but not formatting" do
96
+ @l.indent(4)
97
+ @l.error "error"
98
+ @f.string.should == "ERROR: error\n"
90
99
  end
91
100
  end
92
101
  end
data/spec/logger_spec.rb CHANGED
@@ -21,13 +21,26 @@ describe BufferedLogger do
21
21
  @f.string.should == "info\nwarn\n"
22
22
  end
23
23
 
24
- it 'should support color when printing to STDOUT' do
24
+ it 'should be able to toggle color' do
25
+ @l.color?.should == false
26
+ @l.enable_color
27
+ @l.color?.should == true
28
+ @l.disable_color
29
+ @l.color?.should == false
30
+ @l.toggle_color
31
+ @l.color?.should == true
32
+ end
33
+
34
+ it 'should support color for STDOUT unless forced' do
25
35
  l = BufferedLogger.new(STDOUT)
26
- l.send(:color?).should == true
36
+ l.send(:stdout?).should == true
37
+ l.color?.should == true
27
38
  end
28
39
 
29
- it 'should not support color when not printing to STDOUT' do
30
- @l.send(:color?).should == false
40
+ it 'should not support color when not printing to STDOUT unless forced' do
41
+ @l.color?.should == false
42
+ @l.toggle_color
43
+ @l.color?.should == true
31
44
  end
32
45
 
33
46
  it 'should autoset formatters during init' do
@@ -70,6 +83,16 @@ describe BufferedLogger do
70
83
  @l.flush
71
84
  @f.string.should == "test\ntest2\n"
72
85
  end
86
+
87
+ it 'should insert a blank line' do
88
+ @l.print_blank_line
89
+ @f.string.should == "\n"
90
+ end
91
+
92
+ it 'should log an empty string' do
93
+ @l.info
94
+ @f.string.should == "\n"
95
+ end
73
96
  end
74
97
 
75
98
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: buffered_logger
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Alex Skryl
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-07-20 00:00:00 -05:00
18
+ date: 2011-07-26 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency