mini_logger 0.4.2 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/mini_logger.rb CHANGED
@@ -5,32 +5,39 @@ require 'logger'
5
5
  module MiniLogger
6
6
  extend self
7
7
 
8
- LLM = {
9
- :debug =>::Logger::DEBUG,
10
- :info =>::Logger::INFO,
11
- :warn =>::Logger::WARN,
12
- :error =>::Logger::ERROR,
13
- :faltal =>::Logger::FATAL
14
- }
15
-
16
8
  DEBUG = :debug
17
9
  INFO = :info
18
10
  WARN = :warn
19
11
  ERROR = :error
20
12
  FATAL = :fatal
13
+
14
+ LLM = {
15
+ :debug =>::Logger::DEBUG,
16
+ :info =>::Logger::INFO,
17
+ :warn =>::Logger::WARN,
18
+ :error =>::Logger::ERROR,
19
+ :fatal =>::Logger::FATAL
20
+ }
21
+
22
+ RLLM = {
23
+ ::Logger::DEBUG => DEBUG,
24
+ ::Logger::INFO => INFO,
25
+ ::Logger::WARN => WARN,
26
+ ::Logger::ERROR => ERROR,
27
+ ::Logger::FATAL => FATAL
28
+ }
21
29
 
22
30
  DEFAULT_LOG_CHANNEL = STDERR
23
31
  DEFAULT_LOG_LEVEL = DEBUG
24
- VALID_METHODS = [ :debug, :info, :warn, :error, :fatal, :debug?, :info?, :warn?, :error? ]
32
+ VALID_METHODS = [ :debug, :info, :warn, :error, :fatal, :debug?, :info?, :warn?, :error?, :fatal? ]
33
+
25
34
 
26
- private
27
35
  def validate_log_level?( log_level )
28
36
 
29
37
  LLM.has_key?( log_level )
30
38
  end
31
39
 
32
-
33
- public
40
+
34
41
  def configure( attributes = { } )
35
42
 
36
43
  log_channel = attributes[:log_channel] ? attributes[:log_channel] : DEFAULT_LOG_CHANNEL
@@ -40,16 +47,18 @@ module MiniLogger
40
47
 
41
48
  @logger = Logger.new( log_channel )
42
49
  @logger.level = LLM[log_level]
43
-
50
+
44
51
  self
45
52
  end
46
53
 
54
+
47
55
  def level( )
48
56
 
49
57
  @logger || configure
50
58
 
51
- @logger.level
59
+ RLLM[@logger.level]
52
60
  end
61
+
53
62
 
54
63
  def level=( new_log_level )
55
64
 
data/lib/version.rb CHANGED
@@ -2,7 +2,7 @@ module Version
2
2
  INFO = {
3
3
  :major =>0,
4
4
  :minor =>4,
5
- :patch =>2
5
+ :patch =>3
6
6
  }
7
7
 
8
8
  NAME = 'mini_logger'
@@ -0,0 +1,24 @@
1
+ require 'rubygems'
2
+ require 'file/tail'
3
+
4
+
5
+ class TailFileHelper
6
+
7
+ DEFAULT_BACKWARD = 1
8
+
9
+ def initialize( log_file_name, backward=DEFAULT_BACKWARD )
10
+
11
+ @log_file = File.new( log_file_name )
12
+
13
+ @log_file.extend( File::Tail )
14
+ @log_file.backward( backward )
15
+
16
+ # Skilp Logger file header
17
+ @log_file.tail( 1 )
18
+ end
19
+
20
+ def get_log_line( )
21
+
22
+ @log_file.tail( 1 )[0]
23
+ end
24
+ end
@@ -1,133 +1,186 @@
1
- $:.unshift( File.join( File.dirname( __FILE__ ), %w[.. lib] ) )
2
-
3
-
4
1
  require 'rubygems'
5
2
  require 'shoulda'
6
- require 'config_context'
7
3
  require 'file/tail'
8
4
  require 'mini_logger'
5
+ require File.join( File.dirname( __FILE__ ), %w[.. helpers tail_file_helper.rb] )
9
6
 
10
7
 
11
8
  class TestMiniLogger < Test::Unit::TestCase
12
9
 
13
- context "A default logger" do
10
+ context "A default configured logger" do
14
11
 
15
12
  setup do
16
- @dl = MiniLogger.configure()
13
+ @dl = MiniLogger.configure
17
14
  end
18
15
 
19
16
  should "create a logger in debug level" do
20
17
  assert( @dl.debug? )
21
18
  end
19
+
20
+ should "validate diferent log levels" do
21
+
22
+ MiniLogger::LLM.keys.each do |log_level|
23
+
24
+ assert( @dl.validate_log_level?( log_level ) )
25
+ end
26
+
27
+ [:debug, :info, :warn, :error, :fatal].each do |log_level|
28
+
29
+ assert( @dl.validate_log_level?( log_level ) )
30
+ end
31
+ end
32
+
33
+ should "not validate diferent log levels" do
34
+
35
+ ["foo", "bar", "bazz", :foo, :bar, :bazz].each do |log_level|
36
+
37
+ assert( ! @dl.validate_log_level?( log_level ) )
38
+ end
39
+ end
22
40
  end
23
41
 
24
-
25
42
  context "A bad configured logger" do
26
43
 
27
44
  should "raise and ArgumentError" do
28
45
 
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
46
  assert_raises( ArgumentError ) { MiniLogger.configure( :log_level=>"bad_log_level" ) }
33
47
  end
34
48
  end
35
49
 
36
-
37
50
  context "A well configured logger" do
38
51
 
52
+ TEST_DEBUG_MESSAGE = "debug message"
53
+ TEST_INFO_MESSAGE = "info message"
54
+ TEST_WARN_MESSAGE = "warn message"
55
+ TEST_ERROR_MESSAGE = "error message"
56
+ TEST_FATAL_MESSAGE = "fatal message"
57
+
58
+ TEST_LOG_FILE_NAME = '/tmp/mini_logger_test.log'
59
+ TEST_LOG_LEVEL = 'debug'
60
+
39
61
  setup do
40
62
 
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
63
+ @mld = MiniLogger.configure( :log_channel=>TEST_LOG_FILE_NAME, :log_level=>TEST_LOG_LEVEL )
64
+ @log_file = TailFileHelper.new( TEST_LOG_FILE_NAME )
54
65
  end
55
66
 
56
67
  teardown do
57
- File.delete( ConfigContext.log_channel ) if File.exist?( ConfigContext.log_channel )
68
+
69
+ File.delete( TEST_LOG_FILE_NAME ) if File.exist?( TEST_LOG_FILE_NAME )
58
70
  end
59
71
 
60
72
  should "write >= debug message" do
61
73
 
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/ )
74
+ @mld.debug( TEST_DEBUG_MESSAGE )
75
+ assert( @log_file.get_log_line =~ /^D\, \[\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6} #\d{5,6}\] DEBUG \-\- : #{TEST_DEBUG_MESSAGE}/ )
76
+
77
+ @mld.info( TEST_INFO_MESSAGE )
78
+ assert( @log_file.get_log_line =~ /^I\, \[\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6} #\d{5,6}\] INFO \-\- : #{TEST_INFO_MESSAGE}/ )
79
+
80
+ @mld.warn( TEST_WARN_MESSAGE )
81
+ assert( @log_file.get_log_line =~ /^W\, \[\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6} #\d{5,6}\] WARN \-\- : #{TEST_WARN_MESSAGE}/ )
82
+
83
+ @mld.error( TEST_ERROR_MESSAGE )
84
+ assert( @log_file.get_log_line =~ /^E\, \[\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6} #\d{5,6}\] ERROR \-\- : #{TEST_ERROR_MESSAGE}/ )
85
+
86
+ @mld.fatal( TEST_FATAL_MESSAGE )
87
+ assert( @log_file.get_log_line =~ /^F\, \[\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6} #\d{5,6}\] FATAL \-\- : #{TEST_FATAL_MESSAGE}/ )
72
88
  end
73
89
 
74
90
  should "write >= info message" do
75
91
 
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/ )
92
+ @mld.info( TEST_INFO_MESSAGE )
93
+ assert( @log_file.get_log_line =~ /^I\, \[\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6} #\d{5,6}\] INFO \-\- : #{TEST_INFO_MESSAGE}/ )
94
+
95
+ @mld.warn( TEST_WARN_MESSAGE )
96
+ assert( @log_file.get_log_line =~ /^W\, \[\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6} #\d{5,6}\] WARN \-\- : #{TEST_WARN_MESSAGE}/ )
97
+
98
+ @mld.error( TEST_ERROR_MESSAGE )
99
+ assert( @log_file.get_log_line =~ /^E\, \[\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6} #\d{5,6}\] ERROR \-\- : #{TEST_ERROR_MESSAGE}/ )
100
+
101
+ @mld.fatal( TEST_FATAL_MESSAGE )
102
+ assert( @log_file.get_log_line =~ /^F\, \[\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6} #\d{5,6}\] FATAL \-\- : #{TEST_FATAL_MESSAGE}/ )
84
103
  end
85
104
 
86
105
  should "write >= warn message" do
87
106
 
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/ )
107
+ @mld.warn( TEST_WARN_MESSAGE )
108
+ assert( @log_file.get_log_line =~ /^W\, \[\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6} #\d{5,6}\] WARN \-\- : #{TEST_WARN_MESSAGE}/ )
109
+
110
+ @mld.error( TEST_ERROR_MESSAGE )
111
+ assert( @log_file.get_log_line =~ /^E\, \[\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6} #\d{5,6}\] ERROR \-\- : #{TEST_ERROR_MESSAGE}/ )
112
+
113
+ @mld.fatal( TEST_FATAL_MESSAGE )
114
+ assert( @log_file.get_log_line =~ /^F\, \[\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6} #\d{5,6}\] FATAL \-\- : #{TEST_FATAL_MESSAGE}/ )
94
115
  end
95
116
 
96
117
  should "write >= error message" do
97
118
 
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/ )
119
+ @mld.error( TEST_ERROR_MESSAGE )
120
+ assert( @log_file.get_log_line =~ /^E\, \[\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6} #\d{5,6}\] ERROR \-\- : #{TEST_ERROR_MESSAGE}/ )
121
+
122
+ @mld.fatal( TEST_FATAL_MESSAGE )
123
+ assert( @log_file.get_log_line =~ /^F\, \[\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6} #\d{5,6}\] FATAL \-\- : #{TEST_FATAL_MESSAGE}/ )
102
124
  end
103
125
 
104
126
  should "write a fatal message" do
105
127
 
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/ )
128
+ @mld.fatal( TEST_FATAL_MESSAGE )
129
+ assert( @log_file.get_log_line =~ /^F\, \[\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6} #\d{5,6}\] FATAL \-\- : #{TEST_FATAL_MESSAGE}/ )
108
130
  end
109
131
 
132
+
110
133
  should "be in DEBUG level" do
111
134
 
112
135
  assert( @mld.debug? )
136
+ assert_equal( @mld.level, MiniLogger::DEBUG )
113
137
  end
114
138
 
115
139
  should "change log level to INFO" do
116
140
 
117
141
  @mld.level = MiniLogger::INFO
142
+
118
143
  assert( @mld.info? )
144
+ assert_equal( @mld.level, MiniLogger::INFO )
119
145
  end
120
146
 
121
147
  should "change log level to WARN" do
122
148
 
123
149
  @mld.level = MiniLogger::WARN
150
+
124
151
  assert( @mld.warn? )
152
+ assert_equal( @mld.level, MiniLogger::WARN )
125
153
  end
126
154
 
127
155
  should "change log level to ERROR" do
128
156
 
129
157
  @mld.level = MiniLogger::ERROR
158
+
130
159
  assert( @mld.error? )
160
+ assert_equal( @mld.level, MiniLogger::ERROR )
161
+ end
162
+
163
+ should "change log level to FATAL" do
164
+
165
+ @mld.level = MiniLogger::FATAL
166
+
167
+ assert( @mld.fatal? )
168
+ assert_equal( @mld.level, MiniLogger::FATAL )
169
+ end
170
+
171
+ should "not change to invalid log level" do
172
+
173
+ @mld.level = MiniLogger::DEBUG
174
+
175
+ assert( @mld.debug? )
176
+ assert_equal( @mld.level, MiniLogger::DEBUG )
177
+
178
+ [:this, :is, :not, :valid, :log, :levels].each do |invalid_log_level|
179
+
180
+ assert_raises( ArgumentError ) { @mld.level = invalid_log_level }
181
+ assert( @mld.debug? )
182
+ assert_equal( @mld.level, MiniLogger::DEBUG )
183
+ end
131
184
  end
132
185
  end
133
186
  end
metadata CHANGED
@@ -1,8 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mini_logger
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 9
4
5
  prerelease:
5
- version: 0.4.2
6
+ segments:
7
+ - 0
8
+ - 4
9
+ - 3
10
+ version: 0.4.3
6
11
  platform: ruby
7
12
  authors:
8
13
  - Javier Juarez
@@ -10,20 +15,24 @@ autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
17
 
13
- date: 2011-04-27 00:00:00 Z
18
+ date: 2011-04-28 00:00:00 +02:00
19
+ default_executable:
14
20
  dependencies:
15
21
  - !ruby/object:Gem::Dependency
16
- name: jeweler
22
+ type: :development
17
23
  requirement: &id001 !ruby/object:Gem::Requirement
18
24
  none: false
19
25
  requirements:
20
26
  - - ">="
21
27
  - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
22
31
  version: "0"
23
- type: :runtime
24
- prerelease: false
32
+ name: jeweler
25
33
  version_requirements: *id001
26
- description: A minimal standard Logger wrapper perfect for minimal CLI applications
34
+ prerelease: false
35
+ description: A minimal standard Logger wrapper perfect for CLI applications
27
36
  email: javier.juarez@gmail.com
28
37
  executables: []
29
38
 
@@ -34,8 +43,10 @@ extra_rdoc_files:
34
43
  files:
35
44
  - lib/mini_logger.rb
36
45
  - lib/version.rb
46
+ - test/helpers/tail_file_helper.rb
37
47
  - test/unit/test_mini_logger.rb
38
48
  - README.rdoc
49
+ has_rdoc: true
39
50
  homepage: http://github.com/jjuarez/mini_logger
40
51
  licenses:
41
52
  - MIT
@@ -49,19 +60,26 @@ required_ruby_version: !ruby/object:Gem::Requirement
49
60
  requirements:
50
61
  - - ">="
51
62
  - !ruby/object:Gem::Version
63
+ hash: 3
64
+ segments:
65
+ - 0
52
66
  version: "0"
53
67
  required_rubygems_version: !ruby/object:Gem::Requirement
54
68
  none: false
55
69
  requirements:
56
70
  - - ">="
57
71
  - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
58
75
  version: "0"
59
76
  requirements: []
60
77
 
61
78
  rubyforge_project: http://github.com/jjuarez/mini_logger
62
- rubygems_version: 1.7.2
79
+ rubygems_version: 1.6.2
63
80
  signing_key:
64
81
  specification_version: 3
65
82
  summary: A tiny logger utility for small applications
66
83
  test_files:
84
+ - test/helpers/tail_file_helper.rb
67
85
  - test/unit/test_mini_logger.rb