log4r 1.1.9 → 1.1.10

Sign up to get free protection for your applications and to get access to all the features.
Binary file
@@ -6,9 +6,9 @@ require 'log4r/yamlconfigurator'
6
6
  # we use various outputters, so require them, otherwise config chokes
7
7
  require 'log4r/outputter/datefileoutputter'
8
8
  require 'log4r/outputter/emailoutputter'
9
- include Log4r
9
+ require 'log4r/outputter/scribeoutputter'
10
10
 
11
- cfg = YamlConfigurator # shorthand
11
+ cfg = Log4r::YamlConfigurator # shorthand
12
12
  cfg['HOME'] = '.' # the only parameter in the YAML, our HOME directory
13
13
 
14
14
  # load the YAML file with this
@@ -25,6 +25,6 @@ def do_logging(log)
25
25
  end
26
26
 
27
27
  # turn off the email outputter
28
- Outputter['email'].level = OFF
28
+ Log4r::Outputter['email'].level = Log4r::OFF
29
29
  # the other two outputters log to stderr and a timestamped file in ./logs
30
- do_logging( Logger['mylogger'])
30
+ do_logging( Log4r::Logger['mylogger'])
@@ -5,6 +5,7 @@
5
5
  # Author:: Leon Torres
6
6
  # Version:: $Id$
7
7
 
8
+ require 'log4r/version'
8
9
  require "log4r/outputter/fileoutputter"
9
10
  require "log4r/outputter/consoleoutputters"
10
11
  require "log4r/outputter/staticoutputter"
@@ -14,7 +15,3 @@ require "log4r/loggerfactory"
14
15
  require "log4r/GDC"
15
16
  require "log4r/NDC"
16
17
  require "log4r/MDC"
17
-
18
- module Log4r
19
- Log4rVersion = [1, 1, 9].join '.'
20
- end
@@ -70,7 +70,7 @@ module Log4r
70
70
  def format_object(obj)
71
71
  if obj.kind_of? Exception
72
72
  return "Caught #{obj.class}: #{obj.message}\n\t" +\
73
- obj.backtrace[0...@depth].join("\n\t")
73
+ (obj.backtrace.nil? ? [] : obj.backtrace[0...@depth]).join("\n\t")
74
74
  elsif obj.kind_of? String
75
75
  return obj
76
76
  else # inspect the object
@@ -1,4 +1,4 @@
1
- # INCLUDE TODO ../rdoc/log4jxmlformatter
1
+ # :include: ../rdoc/log4jxmlformatter
2
2
  #
3
3
  # == Other Info
4
4
  #
@@ -7,7 +7,11 @@
7
7
  require "log4r/formatter/formatter"
8
8
 
9
9
  require "rubygems"
10
- require "builder"
10
+ begin
11
+ require "builder"
12
+ rescue LoadError
13
+ puts "builder gem is required to use log4jxmlformatter, i.e. gem install builder"
14
+ end
11
15
 
12
16
  module Log4r
13
17
 
@@ -40,7 +40,7 @@ module Log4r
40
40
 
41
41
  @filename = _filename
42
42
  if ( @create == true ) then
43
- @out = File.new(@filename, (@trunc ? "w" : "a"))
43
+ @out = File.new(@filename, (@trunc ? "wb" : "ab"))
44
44
  Logger.log_internal {
45
45
  "FileOutputter '#{@name}' writing to #{@filename}"
46
46
  }
@@ -0,0 +1,37 @@
1
+ # :nodoc:
2
+ # Version:: $Id$
3
+
4
+ require "log4r/outputter/outputter"
5
+ require "rubygems"
6
+ require "scribe"
7
+
8
+ module Log4r
9
+ class ScribeOutputter < Outputter
10
+ attr_reader :host, :port, :category
11
+
12
+ def initialize(_name, hash={})
13
+ super(_name, hash)
14
+ @host = (hash[:host] or hash[:host] or 'localhost')
15
+ @port = (hash[:port] or hash[:port] or '1463')
16
+ @category = (hash[:category] or hash[:category] or 'default')
17
+
18
+ @client = Scribe.new("#{@host}:#{@port}", category=@category, add_newlines=false)
19
+ end
20
+
21
+ private
22
+
23
+ def write(data)
24
+ begin
25
+ @client.log(data.strip, @category)
26
+ rescue ScribeThrift::Client::TransportException => e
27
+ Logger.log_internal(-2) {
28
+ "Caught TransportException, is the scribe server alive?"
29
+ }
30
+ rescue ThriftClient::NoServersAvailable => e
31
+ Logger.log_internal(-2) {
32
+ "No scribe servers are available!"
33
+ }
34
+ end
35
+ end
36
+ end
37
+ end
@@ -17,8 +17,8 @@ module Log4r
17
17
 
18
18
  def initialize(_name, hash={})
19
19
  super(_name, hash)
20
- @host = hash[:hostname]
21
- @port = hash[:port]
20
+ @host = (hash[:hostname] or hash["hostname"])
21
+ @port = (hash[:port] or hash["port"])
22
22
 
23
23
  begin
24
24
  Logger.log_internal {
@@ -0,0 +1,21 @@
1
+ = Configuring Log4r with Log4r::YamlConfigurator
2
+
3
+ The YamlConfigurator class allows one to set up Log4r via YAML.
4
+ It is used almost exactly as Log4r::Configurator and has the same features,
5
+
6
+ ycfg = YamlConfigurator # handy shorthand
7
+ ycfg['foo'] = bar # replaces instances of #{foo} in the YAML with bar
8
+ ycfg.load_yaml_file('foo.yaml')
9
+
10
+ Ruby 1.7 and 1.8 comes with a YAML parser. Hence, YAML can be used
11
+ to configure Log4r out of the box.
12
+
13
+ A comprehensive example of a Log4r YAML configuration is provided in the
14
+ examples directory.
15
+
16
+ To use this class:
17
+
18
+ require 'log4r/yamlconfigurator'
19
+
20
+ Thanks to Andreas Hund for making this possible.
21
+
@@ -0,0 +1,16 @@
1
+ = ScribeOutputter
2
+
3
+ A ScribeOutputter transforms a Log4r::LogEvent into an event passed to Scribe. The user can configure the outputter with
4
+ the host and port of the scribe scribe server.
5
+
6
+ == Usage
7
+
8
+ To use,
9
+ <tt>require 'log4r'</tt>
10
+
11
+ An example,
12
+
13
+ require 'log4r'
14
+
15
+ logger = Log4r::ScribeOutputter.new('name', '127.0.0.1', 1463)
16
+ logger.debug("Hello World")
@@ -0,0 +1,4 @@
1
+ module Log4r
2
+ Log4rVersion = [1, 1, 10].join '.' # deprecate?
3
+ VERSION = Log4rVersion
4
+ end
@@ -119,13 +119,13 @@ module Log4r
119
119
  end
120
120
 
121
121
  formatter = decode_formatter( op['formatter'])
122
- # build the eval string
123
- buff = "Outputter[name] = #{type}.new name"
124
- buff += ",:level=>#{LNAMES.index(level)}" unless level.nil?
125
- buff += ",:formatter=>formatter" unless formatter.nil?
126
- params = decode_hash_params( op)
127
- buff += "," + params.join(',') if params.size > 0
128
- begin eval buff
122
+
123
+ opts = {}
124
+ opts[:level] = LNAMES.index(level) unless level.nil?
125
+ opts[:formatter] = formatter unless formatter.nil?
126
+ opts.merge!(decode_hash_params(op))
127
+ begin
128
+ Outputter[name] = Log4r.const_get(type).new name, opts
129
129
  rescue Exception => ae
130
130
  raise ConfigError,
131
131
  "Problem creating outputter: #{ae.message}", caller[1..-3]
@@ -138,8 +138,8 @@ module Log4r
138
138
  return nil if fo.nil?
139
139
  type = fo['type']
140
140
  raise ConfigError, "Formatter missing type", caller[1..-4] if type.nil?
141
- buff = "#{type}.new " + decode_hash_params( fo).join(',')
142
- begin return eval( buff)
141
+ begin
142
+ return Log4r.const_get(type).new(decode_hash_params(fo))
143
143
  rescue Exception => ae
144
144
  raise ConfigError,
145
145
  "Problem creating outputter: #{ae.message}", caller[1..-4]
@@ -149,23 +149,25 @@ module Log4r
149
149
  ExcludeParams = %w{formatter level name type only_at}
150
150
 
151
151
  # Does the fancy parameter to hash argument transformation
152
- def self.decode_hash_params( ph)
153
- buff = []
154
- ph.each{ |name, value|
155
- next if ExcludeParams.include? name
156
- buff << ":" + name + "=>" + paramsub( value)
157
- }
158
- buff
152
+ def self.decode_hash_params(ph)
153
+ case ph
154
+ when Hash
155
+ ph.inject({}){|a,(k,v)| a[k] = self.decode_hash_params(v); a}
156
+ when Array
157
+ ph.map{|v| self.decode_hash_params(v)}
158
+ when String
159
+ self.paramsub(ph)
160
+ else
161
+ ph
162
+ end
159
163
  end
160
-
164
+
161
165
  # Substitues any #{foo} in the YAML with Parameter['foo']
162
- def self.paramsub( str)
163
- return nil if str.nil?
164
- return nil if str.class != String
166
+ def self.paramsub(str)
165
167
  @@params.each {|param, value|
166
- str.sub!( '#{' + param + '}', value)
168
+ str = str.sub("\#{#{param}}", value)
167
169
  }
168
- "'" + str + "'"
170
+ str
169
171
  end
170
172
 
171
173
  def self.decode_logger( lo)
@@ -1,9 +1,7 @@
1
- $: << File.join("..","lib")
2
- require "test/unit"
3
- require "log4r"
4
- include Log4r
1
+ require 'test_helper'
5
2
 
6
- class TestGDC < Test::Unit::TestCase
3
+ class TestGDC < TestCase
4
+ include Log4r
7
5
 
8
6
  def test_gdc_default
9
7
  assert(GDC.get() == "testGDC.rb", "Expected 'testGDC.rb' got '#{GDC.get()}'" )
@@ -1,9 +1,7 @@
1
- $: << File.join("..","lib")
2
- require "test/unit"
3
- require "log4r"
4
- include Log4r
1
+ require 'test_helper'
5
2
 
6
- class TestMDC < Test::Unit::TestCase
3
+ class TestMDC < TestCase
4
+ include Log4r
7
5
 
8
6
  def test_multithread_copy
9
7
  Log4r::MDC.put("user","colbygk")
@@ -24,9 +22,9 @@ class TestMDC < Test::Unit::TestCase
24
22
  Log4r::MDC.put("string", "string")
25
23
  Log4r::MDC.put(5, "number")
26
24
  l = Logger.new 'test'
27
- o = StdoutOutputter.new 'test'
25
+ o = StdoutOutputter.new 'test'
28
26
  l.add o
29
- assert_nothing_raised {
27
+ assert_nothing_raised {
30
28
  f = PatternFormatter.new :pattern=> "%l user: %X{:user} %X{strng} %X{5}"
31
29
  Outputter['test'].formatter = f
32
30
  l.debug "And this?"
@@ -1,9 +1,7 @@
1
- $: << File.join("..","lib")
2
- require "test/unit"
3
- require "log4r"
4
- include Log4r
1
+ require 'test_helper'
5
2
 
6
- class TestNDC < Test::Unit::TestCase
3
+ class TestNDC < TestCase
4
+ include Log4r
7
5
 
8
6
  def test_ndc_remove_push
9
7
  NDC.remove()
@@ -0,0 +1,12 @@
1
+ $:.unshift(File.dirname(__FILE__))
2
+
3
+ require "test/unit"
4
+ require 'log4r'
5
+ require 'log4r/configurator'
6
+ require 'log4r/staticlogger'
7
+ require 'log4r/formatter/log4jxmlformatter'
8
+ require 'log4r/outputter/udpoutputter'
9
+ require 'log4r/outputter/consoleoutputters'
10
+ require 'log4r/yamlconfigurator'
11
+
12
+ include Test::Unit
@@ -1,4 +1,4 @@
1
- $: << File.join("..", "lib")
1
+ require 'test_helper'
2
2
 
3
3
  # because constants are dynamically defined, some tests need to
4
4
  # be opened in a fresh instance of Ruby, hence the popens
@@ -1,9 +1,8 @@
1
- $: << File.join("..","lib")
2
- require "test/unit"
3
- require "log4r"
4
- include Log4r
1
+ require 'test_helper'
2
+
3
+ class TestBase < TestCase
4
+ include Log4r
5
5
 
6
- class TestBase < Test::Unit::TestCase
7
6
  # check that LNAMES loads properly (it uses an eval to load)
8
7
  def test_default_levels
9
8
  Logger.root # doing this loads the default levels
@@ -20,10 +19,10 @@ class TestBase < Test::Unit::TestCase
20
19
  # check bad input and bounds for validate_level
21
20
  def test_validate_level
22
21
  7.times{|i| assert_nothing_raised {Log4rTools.validate_level(i)} }
23
- assert_raises(ArgumentError) {Log4rTools.validate_level(-1)}
24
- assert_raises(ArgumentError) {Log4rTools.validate_level(LEVELS)}
25
- assert_raises(ArgumentError) {Log4rTools.validate_level(String)}
26
- assert_raises(ArgumentError) {Log4rTools.validate_level("bogus")}
22
+ assert_raise(ArgumentError) {Log4rTools.validate_level(-1)}
23
+ assert_raise(ArgumentError) {Log4rTools.validate_level(LEVELS)}
24
+ assert_raise(ArgumentError) {Log4rTools.validate_level(String)}
25
+ assert_raise(ArgumentError) {Log4rTools.validate_level("bogus")}
27
26
  end
28
27
  # decode_bool turns a string 'true' into true and so on
29
28
  def test_decode_bool
@@ -1,10 +1,4 @@
1
- $: << File.join("..", "lib")
2
-
3
- require 'log4r'
4
- require 'log4r/staticlogger'
5
- require 'log4r/formatter/log4jxmlformatter'
6
- require 'log4r/outputter/udpoutputter'
7
- require 'log4r/outputter/consoleoutputters'
1
+ require 'test_helper'
8
2
 
9
3
  include Log4r
10
4
 
@@ -1,27 +1,30 @@
1
+ require 'test_helper'
1
2
 
2
3
  # tests the customization of Log4r levels
3
4
  class TestCustom < TestCase
5
+ include Log4r
6
+
4
7
  def test_validation
5
- assert_exception(TypeError) { Configurator.custom_levels "lowercase" }
6
- assert_exception(TypeError) { Configurator.custom_levels "With space" }
8
+ assert_raise(TypeError) { Configurator.custom_levels "lowercase" }
9
+ assert_raise(TypeError) { Configurator.custom_levels "With space" }
7
10
  end
8
11
 
9
12
  def test_create
10
- assert_no_exception { Configurator.custom_levels "Foo", "Bar", "Baz" }
11
- assert_no_exception { Configurator.custom_levels }
12
- assert_no_exception { Configurator.custom_levels "Bogus", "Levels" }
13
- end
14
- def test_methods
15
- l = Logger.new 'custom1'
16
- assert_respond_to(:foo, l)
17
- assert_respond_to(:foo?, l)
18
- assert_respond_to(:bar, l)
19
- assert_respond_to(:bar?, l)
20
- assert_respond_to(:baz, l)
21
- assert_respond_to(:baz?, l)
22
- assert_no_exception(NameError) { Bar }
23
- assert_no_exception(NameError) { Baz }
24
- assert_no_exception(NameError) { Foo }
13
+ assert_nothing_raised { Configurator.custom_levels "Foo", "Bar", "Baz" }
14
+ assert_nothing_raised { Configurator.custom_levels }
15
+ assert_nothing_raised { Configurator.custom_levels "Bogus", "Levels" }
25
16
  end
26
-
17
+ # def test_methods
18
+ # l = Logger.new 'custom1'
19
+ # assert_respond_to(l, :foo)
20
+ # assert_respond_to(l, :foo?)
21
+ # assert_respond_to(l, :bar)
22
+ # assert_respond_to(l, :bar?)
23
+ # assert_respond_to(l, :baz)
24
+ # assert_respond_to(l, :baz?)
25
+ # assert_nothing_raised { Bar }
26
+ # assert_nothing_raised { Baz }
27
+ # assert_nothing_raised { Foo }
28
+ # end
29
+
27
30
  end
@@ -1,7 +1,11 @@
1
+ require 'test_helper'
2
+
1
3
  class TestFormatter < TestCase
4
+ include Log4r
5
+
2
6
  def test_creation
3
- assert_no_exception { Formatter.new.format(3) }
4
- assert_no_exception { DefaultFormatter.new }
7
+ assert_nothing_raised { Formatter.new.format(3) }
8
+ assert_nothing_raised { DefaultFormatter.new }
5
9
  assert_kind_of(Formatter, DefaultFormatter.new)
6
10
  end
7
11
  def test_simple_formatter
@@ -24,4 +28,4 @@ class TestFormatter < TestCase
24
28
  assert_match(b.format(event3), /ArgumentError/)
25
29
  assert_match(b.format(LogEvent.new(0,f,nil,[1,2,3])), /Array/)
26
30
  end
27
- end
31
+ end
@@ -1,16 +1,20 @@
1
- class MyFormatter1 < Formatter
1
+ require 'test_helper'
2
+
3
+ class MyFormatter1 < Log4r::Formatter
2
4
  def format(event)
3
5
  return "MyFormatter1\n"
4
6
  end
5
7
  end
6
8
 
7
- class MyFormatter2 < Formatter
9
+ class MyFormatter2 < Log4r::Formatter
8
10
  def format(event)
9
11
  return "MyFormatter2\n"
10
12
  end
11
13
  end
12
14
 
13
15
  class TestLogger < TestCase
16
+ include Log4r
17
+
14
18
  def test_root
15
19
  l1 = Logger.root
16
20
  l2 = Logger['root']
@@ -21,8 +25,8 @@ class TestLogger < TestCase
21
25
  assert(l1.parent == nil, "Root's parent wasn't nil!")
22
26
  end
23
27
  def test_validation
24
- assert_exception(ArgumentError) { Logger.new }
25
- assert_no_exception { Logger.new('validate', nil) }
28
+ assert_raise(ArgumentError) { Logger.new }
29
+ assert_nothing_raised { Logger.new('validate', nil) }
26
30
  end
27
31
  def test_all_off
28
32
  l = Logger.new("create_method")
@@ -45,14 +49,14 @@ class TestLogger < TestCase
45
49
  StdoutOutputter.new('fake1')
46
50
  StdoutOutputter.new('fake2')
47
51
  a = Logger.new("add")
48
- assert_exception(TypeError) { a.add 'bogus' }
49
- assert_exception(TypeError) { a.add Class }
50
- assert_exception(TypeError) { a.add 'fake1', Class }
51
- assert_no_exception { a.add 'fake1', 'fake2' }
52
+ assert_raise(TypeError) { a.add 'bogus' }
53
+ assert_raise(TypeError) { a.add Class }
54
+ assert_raise(TypeError) { a.add 'fake1', Class }
55
+ assert_nothing_raised { a.add 'fake1', 'fake2' }
52
56
  end
53
57
  def test_repository
54
- assert_exception(NameError) { Logger.get('bogusbogus') }
55
- assert_no_exception { Logger['bogusbogus'] }
58
+ assert_raise(NameError) { Logger.get('bogusbogus') }
59
+ assert_nothing_raised { Logger['bogusbogus'] }
56
60
  end
57
61
  def test_heiarchy
58
62
  a = Logger.new("a")
@@ -79,7 +83,7 @@ class TestLogger < TestCase
79
83
  assert(d.path == "a", "path wasn't set properly")
80
84
  assert(d.level == a.level, "didn't inherit parent's level")
81
85
  assert(d.parent == a, "parent wasn't what is expected")
82
- assert_exception(ArgumentError) { Logger.new("::a") }
86
+ assert_raise(ArgumentError) { Logger.new("::a") }
83
87
  end
84
88
  def test_undefined_parents
85
89
  a = Logger.new 'has::no::real::parents::me'
@@ -130,7 +134,7 @@ class TestLogger < TestCase
130
134
  l = Logger.new 'logblocks'
131
135
  l.level = WARN
132
136
  l.add(Outputter.stdout)
133
- assert_no_exception {
137
+ assert_nothing_raised {
134
138
  l.debug { puts "should not show up"; "LOGBLOCKS" }
135
139
  l.fatal { puts "should show up"; "LOGBLOCKS" }
136
140
  l.fatal { nil }