mini_logger 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/mini_logger.rb CHANGED
@@ -4,41 +4,64 @@ require 'logger'
4
4
 
5
5
  module MiniLogger
6
6
  extend self
7
+
8
+ LOG_LEVEL_MAP = {
9
+ :debug =>::Logger::DEBUG,
10
+ :info =>::Logger::INFO,
11
+ :warn =>::Logger::WARN,
12
+ :error =>::Logger::ERROR,
13
+ :faltal =>::Logger::FATAL
14
+ }
7
15
 
8
- DEBUG = ::Logger::DEBUG
9
- INFO = ::Logger::INFO
10
- WARN = ::Logger::WARN
11
- ERROR = ::Logger::ERROR
12
- FATAL = ::Logger::FATAL
16
+ DEBUG = LOG_LEVEL_MAP[:debug]
17
+ INFO = LOG_LEVEL_MAP[:info]
18
+ WARN = LOG_LEVEL_MAP[:warn]
19
+ ERROR = LOG_LEVEL_MAP[:error]
20
+ FATAL = LOG_LEVEL_MAP[:fatal]
13
21
 
14
22
  DEFAULT_LOGCHANNEL = STDERR
15
- DEFAULT_LOGLEVEL = DEBUG
23
+ DEFAULT_LOGLEVEL = LOG_LEVEL_MAP[:info]
16
24
  DEFAULT_DATETIME_FORMAT = "%Y/%m/%d %H:%M:%S"
17
25
 
18
26
 
19
27
  def configure( atts = {} )
20
28
 
21
- @logger ||= ::Logger.new( atts[:log_channel] ? atts[:log_channel] : DEFAULT_LOGCHANNEL )
22
- @logger.level = atts[:log_level] ? atts[:log_level] : DEFAULT_LOGLEVEL
23
- @logger.datetime_format = atts[:dt_format] ? atts[:dt_format] : DEFAULT_DATETIME_FORMAT
29
+ log_channel = atts[:log_channel] ? atts[:log_channel] : DEFAULT_LOGCHANNEL
30
+ log_level = LOG_LEVEL_MAP.has_value?( atts[:log_level] ) ? atts[:log_level] : DEFAULT_LOGLEVEL
31
+ dt_format = atts[:dt_format] ? atts[:dt_format] : DEFAULT_DATETIME_FORMAT
32
+
33
+ @logger ||= ::Logger.new( log_channel )
34
+ @logger.level ||= log_level
35
+ @logger.datetime_format ||= dt_format
24
36
 
25
37
  self
26
38
  end
27
39
 
40
+
41
+ def level( )
42
+ self.configure unless @logger
43
+
44
+ @logger.level
45
+ end
46
+
47
+
48
+ def level=( new_log_level )
49
+
50
+ self.configure unless @logger
51
+
52
+ if LOG_LEVEL_MAP.has_value?( new_log_level )
53
+ @logger.level = new_log_level
54
+ else
55
+ raise ArgumentError.new( "Bad log level: #{new_log_level}" )
56
+ end
57
+ end
58
+
28
59
 
29
60
  def method_missing( method, *arguments, &block )
30
61
 
31
62
  self.configure unless @logger
32
63
 
33
64
  return unless( [:debug, :debug?, :info, :info?, :warn, :warn?, :error, :error?, :fatal].include?( method ) )
34
-
35
- case arguments.length
36
- when 0
37
- @logger.send( method )
38
- when 1
39
- @logger.send( method, arguments[0] )
40
- else
41
- @logger.send( method, arguments )
42
- end
65
+ @logger.send( method, *arguments, &block )
43
66
  end
44
67
  end
data/lib/version.rb CHANGED
@@ -2,7 +2,7 @@ module MiniLogger
2
2
  module Version
3
3
  INFO = {
4
4
  :major =>0,
5
- :minor =>3,
5
+ :minor =>4,
6
6
  :patch =>0
7
7
  }
8
8
 
@@ -11,47 +11,56 @@ class TestMiniLogger < Test::Unit::TestCase
11
11
  context "A Default Logger" do
12
12
 
13
13
  setup do
14
- MiniLogger.configure( :log_channel=>"/dev/null", :log_level=>MiniLogger::DEBUG )
14
+ @mini_logger_debug = MiniLogger.configure( :log_channel=>"/dev/null", :log_level=>MiniLogger::DEBUG )
15
15
  end
16
-
16
+
17
17
  should "write a debug line" do
18
- assert( MiniLogger.debug( "debug" ) )
18
+ assert( @mini_logger_debug.debug( "debug" ) )
19
19
  end
20
20
 
21
21
  should "write a info line" do
22
- assert( MiniLogger.info( "info" ) )
22
+ assert( @mini_logger_debug.info( "info" ) )
23
23
  end
24
24
 
25
25
  should "write a warn line" do
26
- assert( MiniLogger.warn( "warn" ) )
26
+ assert( @mini_logger_debug.warn( "warn" ) )
27
27
  end
28
28
 
29
29
  should "write a error line" do
30
- assert( MiniLogger.error( "error" ) )
30
+ assert( @mini_logger_debug.error( "error" ) )
31
31
  end
32
32
 
33
33
  should "write a fatal line" do
34
- assert( MiniLogger.fatal( "fatal" ) )
34
+ assert( @mini_logger_debug.fatal( "fatal" ) )
35
+ end
36
+
37
+ should "change log level to ERROR" do
38
+
39
+ @mini_logger_debug.level = MiniLogger::ERROR
40
+ assert( @mini_logger_debug.error? )
35
41
  end
36
42
 
37
- should "log level ERROR" do
38
- MiniLogger.configure( :log_level=>MiniLogger::ERROR )
39
- assert( MiniLogger.error? )
43
+ should "change log level to WARN" do
44
+
45
+ @mini_logger_debug.level = MiniLogger::WARN
46
+ assert( @mini_logger_debug.warn? )
40
47
  end
41
48
 
42
- should "log level WARN" do
43
- MiniLogger.configure( :log_level=>MiniLogger::WARN )
44
- assert( MiniLogger.warn? )
49
+ should "change log level to INFO" do
50
+
51
+ @mini_logger_debug.level = MiniLogger::INFO
52
+ assert( @mini_logger_debug.info? )
45
53
  end
46
54
 
47
- should "log level INFO" do
48
- MiniLogger.configure( :log_level=>MiniLogger::INFO )
49
- assert( MiniLogger.info? )
55
+ should "change log level to DEBUG" do
56
+
57
+ @mini_logger_debug.level = MiniLogger::DEBUG
58
+ assert( @mini_logger_debug.debug? )
50
59
  end
51
60
 
52
- should "log level DEBUG" do
53
- MiniLogger.configure( :log_level=>MiniLogger::DEBUG )
54
- assert( MiniLogger.debug? )
61
+ should "raise an ArgumentError" do
62
+
63
+ assert_raise( ArgumentError ) { @mini_logger_debug.level = "bad value" }
55
64
  end
56
65
  end
57
66
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: mini_logger
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.3.0
5
+ version: 0.4.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Javier Juarez
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-06 00:00:00 +01:00
13
+ date: 2011-03-14 00:00:00 +01:00
14
14
  default_executable:
15
15
  dependencies: []
16
16
 
@@ -51,7 +51,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
51
51
  requirements: []
52
52
 
53
53
  rubyforge_project: http://github.com/jjuarez/mini_logger
54
- rubygems_version: 1.5.2
54
+ rubygems_version: 1.6.2
55
55
  signing_key:
56
56
  specification_version: 3
57
57
  summary: A real simple logger utility