mini_logger 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/mini_logger.rb CHANGED
@@ -6,11 +6,11 @@ module MiniLogger
6
6
  extend self
7
7
 
8
8
  LLM = {
9
- :debug =>Logger::DEBUG,
10
- :info =>Logger::INFO,
11
- :warn =>Logger::WARN,
12
- :error =>Logger::ERROR,
13
- :faltal =>Logger::FATAL
9
+ :debug =>::Logger::DEBUG,
10
+ :info =>::Logger::INFO,
11
+ :warn =>::Logger::WARN,
12
+ :error =>::Logger::ERROR,
13
+ :faltal =>::Logger::FATAL
14
14
  }
15
15
 
16
16
  DEBUG = :debug
@@ -19,26 +19,35 @@ module MiniLogger
19
19
  ERROR = :error
20
20
  FATAL = :fatal
21
21
 
22
+ DEFAULT_LOG_CHANNEL = STDERR
23
+ DEFAULT_LOG_LEVEL = DEBUG
24
+ VALID_METHODS = [ :debug, :info, :warn, :error, :fatal, :debug?, :info?, :warn?, :error? ]
25
+
22
26
  private
23
27
  def validate_log_level?( log_level )
28
+
24
29
  LLM.has_key?( log_level )
25
30
  end
26
31
 
27
- def get_log_level( log_level )
28
- LLM[log_level]
29
- end
30
-
31
-
32
+
32
33
  public
33
- def configure( atts = { } )
34
+ def configure( attributes = { } )
35
+
36
+ log_channel = attributes[:log_channel] ? attributes[:log_channel] : DEFAULT_LOG_CHANNEL
37
+ log_level = attributes[:log_level] ? attributes[:log_level].to_sym : DEFAULT_LOG_LEVEL
38
+
39
+ raise ArgumentError.new( "Invalid log level: #{log_level}" ) unless( validate_log_level?( log_level ) )
40
+
41
+ @logger = Logger.new( log_channel )
42
+ @logger.level = LLM[log_level]
34
43
 
35
- @logger = Logger.new( atts[:log_channel] ? atts[:log_channel] : STDERR )
36
- @logger.level = validate_log_level?( atts[:log_level] ) ? get_log_level( atts[:log_level] ) : Logger::INFO
37
44
  self
38
45
  end
39
46
 
40
- def level( )
47
+ def level( )
48
+
41
49
  @logger || configure
50
+
42
51
  @logger.level
43
52
  end
44
53
 
@@ -47,18 +56,19 @@ module MiniLogger
47
56
  @logger || configure
48
57
 
49
58
  if( validate_log_level?( new_log_level ) )
50
- @logger.level = get_log_level( new_log_level )
51
- else
59
+
60
+ @logger.level = LLM[new_log_level]
61
+ else
62
+
52
63
  raise ArgumentError.new( "Bad log level: #{new_log_level}" )
53
64
  end
54
65
  end
55
66
 
67
+
56
68
  def method_missing( method, *arguments, &block )
57
69
 
58
70
  @logger || configure
59
-
60
- if( [ :debug, :debug?, :info, :info?, :warn, :warn?, :error, :error?, :fatal ].include?( method ) )
61
- @logger.send( method, *arguments, &block )
62
- end
71
+
72
+ @logger.send( method, *arguments, &block ) if( VALID_METHODS.include?( method ) )
63
73
  end
64
74
  end
data/lib/version.rb CHANGED
@@ -1,12 +1,10 @@
1
- module MiniLogger
2
- module Version
3
- INFO = {
4
- :major =>0,
5
- :minor =>4,
6
- :patch =>1
7
- }
1
+ module Version
2
+ INFO = {
3
+ :major =>0,
4
+ :minor =>4,
5
+ :patch =>2
6
+ }
8
7
 
9
- NAME = 'mini_logger'
10
- NUMBER = INFO.values.join( '.' )
11
- end
12
- end
8
+ NAME = 'mini_logger'
9
+ NUMBER = [INFO[:major], INFO[:minor], INFO[:patch]].join( '.' )
10
+ end
@@ -0,0 +1,133 @@
1
+ $:.unshift( File.join( File.dirname( __FILE__ ), %w[.. lib] ) )
2
+
3
+
4
+ require 'rubygems'
5
+ require 'shoulda'
6
+ require 'config_context'
7
+ require 'file/tail'
8
+ require 'mini_logger'
9
+
10
+
11
+ class TestMiniLogger < Test::Unit::TestCase
12
+
13
+ context "A default logger" do
14
+
15
+ setup do
16
+ @dl = MiniLogger.configure()
17
+ end
18
+
19
+ should "create a logger in debug level" do
20
+ assert( @dl.debug? )
21
+ end
22
+ end
23
+
24
+
25
+ context "A bad configured logger" do
26
+
27
+ should "raise and ArgumentError" do
28
+
29
+ bad_config_file = File.join( File.dirname( __FILE__ ), %w[.. fixtures bad_test_settings.yml] )
30
+
31
+ assert_raises( ArgumentError ) { MiniLogger.configure( ConfigContext.load( bad_config_file ) ) }
32
+ assert_raises( ArgumentError ) { MiniLogger.configure( :log_level=>"bad_log_level" ) }
33
+ end
34
+ end
35
+
36
+
37
+ context "A well configured logger" do
38
+
39
+ setup do
40
+
41
+ config_file = File.join( File.dirname( __FILE__ ), %w[.. fixtures test_settings.yml] )
42
+ @mld = MiniLogger.configure( ConfigContext.load( config_file ) )
43
+
44
+ class MyLogFile < File
45
+
46
+ include File::Tail
47
+ end
48
+
49
+ @log_file = MyLogFile.new( ConfigContext.log_channel )
50
+ @log_file.interval = 10
51
+
52
+ @log_file.backward( 1 )
53
+ @log_file.tail(1) #Skip file header
54
+ end
55
+
56
+ teardown do
57
+ File.delete( ConfigContext.log_channel ) if File.exist?( ConfigContext.log_channel )
58
+ end
59
+
60
+ should "write >= debug message" do
61
+
62
+ assert( @mld.debug( "debug" ) )
63
+ assert( @log_file.tail( 1 )[0] =~ /^D\, \[\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6} #(.+)\](\s*)DEBUG \-\- : debug/ )
64
+ assert( @mld.info( "info" ) )
65
+ assert( @log_file.tail( 1 )[0] =~ /^I\, \[\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6} #(.+)\](\s*)INFO \-\- : info/ )
66
+ assert( @mld.warn( "warn" ) )
67
+ assert( @log_file.tail( 1 )[0] =~ /^W\, \[\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6} #(.+)\](\s*)WARN \-\- : warn/ )
68
+ assert( @mld.error( "error" ) )
69
+ assert( @log_file.tail( 1 )[0] =~ /^E\, \[\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6} #(.+)\](\s*)ERROR \-\- : error/ )
70
+ assert( @mld.fatal( "fatal" ) )
71
+ assert( @log_file.tail( 1 )[0] =~ /^F\, \[\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6} #(.+)\](\s*)FATAL \-\- : fatal/ )
72
+ end
73
+
74
+ should "write >= info message" do
75
+
76
+ assert( @mld.info( "info" ) )
77
+ assert( @log_file.tail( 1 )[0] =~ /^I\, \[\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6} #(.+)\](\s*)INFO \-\- : info/ )
78
+ assert( @mld.warn( "warn" ) )
79
+ assert( @log_file.tail( 1 )[0] =~ /^W\, \[\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6} #(.+)\](\s*)WARN \-\- : warn/ )
80
+ assert( @mld.error( "error" ) )
81
+ assert( @log_file.tail( 1 )[0] =~ /^E\, \[\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6} #(.+)\](\s*)ERROR \-\- : error/ )
82
+ assert( @mld.fatal( "fatal" ) )
83
+ assert( @log_file.tail( 1 )[0] =~ /^F\, \[\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6} #(.+)\](\s*)FATAL \-\- : fatal/ )
84
+ end
85
+
86
+ should "write >= warn message" do
87
+
88
+ assert( @mld.warn( "warn" ) )
89
+ assert( @log_file.tail( 1 )[0] =~ /^W\, \[\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6} #(.+)\](\s*)WARN \-\- : warn/ )
90
+ assert( @mld.error( "error" ) )
91
+ assert( @log_file.tail( 1 )[0] =~ /^E\, \[\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6} #(.+)\](\s*)ERROR \-\- : error/ )
92
+ assert( @mld.fatal( "fatal" ) )
93
+ assert( @log_file.tail( 1 )[0] =~ /^F\, \[\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6} #(.+)\](\s*)FATAL \-\- : fatal/ )
94
+ end
95
+
96
+ should "write >= error message" do
97
+
98
+ assert( @mld.error( "error" ) )
99
+ assert( @log_file.tail( 1 )[0] =~ /^E\, \[\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6} #(.+)\](\s*)ERROR \-\- : error/ )
100
+ assert( @mld.fatal( "fatal" ) )
101
+ assert( @log_file.tail( 1 )[0] =~ /^F\, \[\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6} #(.+)\](\s*)FATAL \-\- : fatal/ )
102
+ end
103
+
104
+ should "write a fatal message" do
105
+
106
+ assert( @mld.fatal( "fatal" ) )
107
+ assert( @log_file.tail( 1 )[0] =~ /^F\, \[\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6} #(.+)\](\s*)FATAL \-\- : fatal/ )
108
+ end
109
+
110
+ should "be in DEBUG level" do
111
+
112
+ assert( @mld.debug? )
113
+ end
114
+
115
+ should "change log level to INFO" do
116
+
117
+ @mld.level = MiniLogger::INFO
118
+ assert( @mld.info? )
119
+ end
120
+
121
+ should "change log level to WARN" do
122
+
123
+ @mld.level = MiniLogger::WARN
124
+ assert( @mld.warn? )
125
+ end
126
+
127
+ should "change log level to ERROR" do
128
+
129
+ @mld.level = MiniLogger::ERROR
130
+ assert( @mld.error? )
131
+ end
132
+ end
133
+ 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.4.1
5
+ version: 0.4.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Javier Juarez
@@ -10,11 +10,20 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-16 00:00:00 +01:00
14
- default_executable:
15
- dependencies: []
16
-
17
- description: My tiny logger
13
+ date: 2011-04-27 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: jeweler
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *id001
26
+ description: A minimal standard Logger wrapper perfect for minimal CLI applications
18
27
  email: javier.juarez@gmail.com
19
28
  executables: []
20
29
 
@@ -25,9 +34,8 @@ extra_rdoc_files:
25
34
  files:
26
35
  - lib/mini_logger.rb
27
36
  - lib/version.rb
28
- - test/test_mini_logger.rb
37
+ - test/unit/test_mini_logger.rb
29
38
  - README.rdoc
30
- has_rdoc: true
31
39
  homepage: http://github.com/jjuarez/mini_logger
32
40
  licenses:
33
41
  - MIT
@@ -51,9 +59,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
51
59
  requirements: []
52
60
 
53
61
  rubyforge_project: http://github.com/jjuarez/mini_logger
54
- rubygems_version: 1.6.2
62
+ rubygems_version: 1.7.2
55
63
  signing_key:
56
64
  specification_version: 3
57
- summary: A real simple logger utility
65
+ summary: A tiny logger utility for small applications
58
66
  test_files:
59
- - test/test_mini_logger.rb
67
+ - test/unit/test_mini_logger.rb
@@ -1,70 +0,0 @@
1
- $:.unshift( File.join( File.dirname( __FILE__ ), %w[.. lib] ) )
2
-
3
-
4
- require 'rubygems'
5
- require 'shoulda'
6
- require 'mini_logger'
7
-
8
-
9
- class TestMiniLogger < Test::Unit::TestCase
10
-
11
- context "A Default Logger" do
12
-
13
- setup do
14
- @mini_logger_debug = MiniLogger.configure( :log_channel=>"/dev/null", :log_level=>MiniLogger::DEBUG )
15
- end
16
-
17
- should "a info log level" do
18
- assert( MiniLogger.configure.info? )
19
- end
20
-
21
- should "write a debug line" do
22
- assert( @mini_logger_debug.debug( "debug" ) )
23
- end
24
-
25
- should "write a info line" do
26
- assert( @mini_logger_debug.info( "info" ) )
27
- end
28
-
29
- should "write a warn line" do
30
- assert( @mini_logger_debug.warn( "warn" ) )
31
- end
32
-
33
- should "write a error line" do
34
- assert( @mini_logger_debug.error( "error" ) )
35
- end
36
-
37
- should "write a fatal line" do
38
- assert( @mini_logger_debug.fatal( "fatal" ) )
39
- end
40
-
41
- should "change log level to ERROR" do
42
-
43
- @mini_logger_debug.level = MiniLogger::ERROR
44
- assert( @mini_logger_debug.error? )
45
- end
46
-
47
- should "change log level to WARN" do
48
-
49
- @mini_logger_debug.level = MiniLogger::WARN
50
- assert( @mini_logger_debug.warn? )
51
- end
52
-
53
- should "change log level to INFO" do
54
-
55
- @mini_logger_debug.level = MiniLogger::INFO
56
- assert( @mini_logger_debug.info? )
57
- end
58
-
59
- should "change log level to DEBUG" do
60
-
61
- @mini_logger_debug.level = MiniLogger::DEBUG
62
- assert( @mini_logger_debug.debug? )
63
- end
64
-
65
- should "raise an ArgumentError" do
66
-
67
- assert_raise( ArgumentError ) { @mini_logger_debug.level = "bad value" }
68
- end
69
- end
70
- end