logutils 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,4 +9,5 @@ lib/logutils/db/models.rb
9
9
  lib/logutils/db/schema.rb
10
10
  lib/logutils/logger.rb
11
11
  lib/logutils/version.rb
12
- test/test.rb
12
+ test/test_helper.rb
13
+ test/test_logger.rb
data/README.md CHANGED
@@ -8,25 +8,66 @@ Another Logger in Ruby
8
8
 
9
9
  Logging levels:
10
10
 
11
- DEBUG < INFO < WARN < ERROR < FATAL
11
+ [ALL] < DEBUG < INFO < WARN < ERROR < FATAL < [OFF]
12
12
 
13
- use methods e.g.
14
13
 
15
- logger = LoggerUtils::Logger.new
16
-
14
+ Start off getting a logger e.g.:
15
+
16
+ logger = LogUtils::Logger.new
17
+
18
+ or
19
+
20
+ include LogUtils
21
+
22
+ logger = Logger.new
23
+
24
+ now you're ready to log using the methods `#debug`, `#info`, `#warn`, etc.
25
+
17
26
  logger.debug "msg"
18
27
  logger.info "another msg"
19
28
  logger.warn "another msg"
20
29
  logger.error "another msg"
21
30
  logger.fatal "another msg"
22
31
 
32
+
23
33
  To get a Logger use
24
34
 
25
- logger = LogUtils[ self ]
35
+ logger = Logger[ self ] # pass in object reference
36
+
37
+ or
38
+
39
+ logger = Logger[ SportDb::Reader ] # pass in class reference
26
40
 
27
41
  or
28
42
 
29
- logger = LogUtils[ 'SportDb::Reader' ]
43
+ logger = Logger[ 'SportDb::Reader' ] # pass in class name (as string)
44
+
45
+
46
+ ### `Logging` Mixin
47
+
48
+ Note: In a class for convenience you can include the logging machinery
49
+ with a single line using the Logging mixin e.g.
50
+
51
+ include LogUtils::Logging
52
+
53
+ This will add/mixin the logger attribute reader e.g.
54
+
55
+ def logger
56
+ @logger ||= Logger[ self ]
57
+ end
58
+
59
+ plus the constants for all logging levels, that is, FATAL, ERROR, WARN, etc.
60
+
61
+ Example:
62
+
63
+ class SampleClass
64
+ include Logging
65
+
66
+ def initialize
67
+ logger.info 'hello SampleClass'
68
+ end
69
+ end
70
+
30
71
 
31
72
  ### Log to the database using `LogDb`
32
73
 
@@ -52,7 +93,9 @@ To clean out all log records from the database use:
52
93
 
53
94
  require 'logutils'
54
95
 
55
- logger = LogUtils[ 'Test' ]
96
+ include LogUtils # lets you use Logger instead of LogUtils::Logger
97
+
98
+ logger = Logger[ 'Test' ]
56
99
  logger.info 'hello LogUtils'
57
100
 
58
101
  require 'logutils/db'
@@ -78,6 +121,11 @@ To clean out all log records from the database use:
78
121
 
79
122
  That's it.
80
123
 
124
+ ## Todos
125
+
126
+ - [ ] Add UNKNOWN level - why? why not? check std logger
127
+
128
+
81
129
 
82
130
  ## Alternatives
83
131
 
data/Rakefile CHANGED
@@ -4,7 +4,7 @@ require './lib/logutils/version.rb'
4
4
 
5
5
  Hoe.spec 'logutils' do
6
6
 
7
- self.version = LogUtils::VERSION
7
+ self.version = LogUtils::Kernel::VERSION
8
8
 
9
9
  self.summary = 'Another Logger'
10
10
  self.description = summary
@@ -10,7 +10,6 @@
10
10
 
11
11
  require 'yaml'
12
12
  require 'pp'
13
- require 'logger'
14
13
  require 'fileutils'
15
14
 
16
15
 
@@ -21,8 +20,7 @@ require 'logutils/version'
21
20
  require 'logutils/logger'
22
21
 
23
22
 
24
- module LogUtils
25
-
23
+ module LogUtils::Kernel
26
24
  =begin # not needed for now; keep it for later
27
25
 
28
26
  def self.root
@@ -34,15 +32,69 @@ module LogUtils
34
32
  end
35
33
 
36
34
  =end
35
+ end
36
+
37
+
38
+ module LogUtils
37
39
 
38
40
  ###########
39
- # use it like:
40
- # logger = LogUtils[ self ] or
41
- # logger = LogUtils[ 'SportDb::Reader' ] etc.
41
+ # deprecated - remove old api!!!
42
42
 
43
43
  def self.[]( class_or_name )
44
+
45
+ puts "depreceated API call LogUtils[] - use Logger[] / LogUtils::Logger[] instead"
46
+
44
47
  # for now always return single instance, that is, use standard/default logger for all
45
- STDLOGGER
48
+ LogUtils::Kernel::STDLOGGER
49
+ end
50
+
51
+ ###################################
52
+ # export public api from kernel
53
+
54
+
55
+ ##############################
56
+ # use it like:
57
+ # logger = Logger[ self ] or
58
+ # logger = Logger[ SportDb::Reader ] or
59
+ # logger = Logger[ 'SportDb::Reader' ] etc.
60
+
61
+ Logger = LogUtils::Kernel::Logger
62
+ include LogUtils::Kernel::Level # e.g. export ALL,DEBUG,INFO,WARN,etc.
63
+
64
+
65
+
66
+ #####
67
+ # use it like
68
+ #
69
+ # class SampleClass
70
+ # include LogUtils::Logging
71
+ # ...
72
+ # end
73
+ #
74
+ # or
75
+ #
76
+ # include LogUtils
77
+ #
78
+ # class SampleClass
79
+ # include Logging
80
+ # ...
81
+ # end
82
+ #
83
+ # class AnotherSampleClass
84
+ # include Logging
85
+ # ...
86
+ # def sample_method
87
+ # logger.debug "calling sample_method"
88
+ # end
89
+ # end
90
+
91
+ module Logging
92
+ include LogUtils::Kernel::Level # e.g. lets you use ALL,DEBUG,INFO,WARN,etc.
93
+
94
+ def logger
95
+ @logger ||= LogUtils::Kernel::Logger[ self ]
96
+ end
46
97
  end
47
98
 
48
99
  end # module LogUtils
100
+
@@ -16,7 +16,7 @@ require 'logutils/db/deleter'
16
16
 
17
17
  module LogDb
18
18
 
19
- VERSION = LogUtils::VERSION
19
+ include LogUtils::Kernel # NB: will also include VERSION constant e.g. LogDB::VERSION == LogUtils::Kernel::VERSION
20
20
 
21
21
  def self.banner
22
22
  "logdb #{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
@@ -34,7 +34,7 @@ LogDB = LogDb
34
34
  module LogDb
35
35
 
36
36
  class DbListener
37
- include LogDB::Models
37
+ include LogDb::Models
38
38
 
39
39
  def write( ev )
40
40
  if( ev.fatal? || ev.error? || ev.warn? )
@@ -63,7 +63,7 @@ module LogDb
63
63
 
64
64
  def self.setup # check: use different name? e.g. configure or connect ?? why or why not?
65
65
  # turn on logging to db - assumes active connection
66
- LogUtils::STDLOGGER.listeners << STDDBLISTENER
66
+ STDLOGGER.listeners << STDDBLISTENER
67
67
  end
68
68
 
69
69
 
@@ -1,21 +1,47 @@
1
1
 
2
- module LogUtils
2
+ module LogUtils::Kernel
3
3
 
4
+ ## private "kernel" - with include LogUtils - do NOT include everything
5
+
6
+
7
+ #####
8
+ # use like:
9
+ # LEVEL_NAME[ DEBUG ] will return 'debug'
10
+ # LEVEL_NAME[ ERROR ] will return 'error' etc.
11
+
12
+ ### todo: reverse level integers ??? why? why not??
13
+ # more logical no output = 0 (off)
14
+ # everything = 6 (all)
15
+ # debug = 5 etc.
16
+
4
17
  module Level
5
- DEBUG = 'debug'
6
- INFO = 'info'
7
- WARN = 'warn'
8
- ERROR = 'error'
9
- FATAL = 'fatal'
18
+ ALL = 0
19
+ DEBUG = 1
20
+ INFO = 2
21
+ WARN = 3
22
+ ERROR = 4
23
+ FATAL = 5
24
+ OFF = 6
10
25
  end
11
26
 
12
27
 
13
28
  class Event
14
- include Level
15
-
16
- def initialize( level, msg )
17
- @level = level
18
- @msg = msg
29
+ include Level # lets you access Level::DEBUG with just DEBUG etc.
30
+
31
+ LEVEL_NAME =
32
+ [
33
+ 'all', # 0
34
+ 'debug', # 1
35
+ 'info', # 2
36
+ 'warn', # 3
37
+ 'error', # 4
38
+ 'fatal', # 5
39
+ 'off' # 6
40
+ ]
41
+
42
+ def initialize( levelno, msg )
43
+ @levelno = levelno # pass in integer e.g. 0,1,2,3,etc.
44
+ @msg = msg
19
45
 
20
46
  @pid = Process.pid
21
47
  @tid = Thread.current.object_id
@@ -23,7 +49,10 @@ module LogUtils
23
49
  @ts = Time.now
24
50
  end
25
51
 
26
- attr_reader :level
52
+ def level
53
+ LEVEL_NAME[ @levelno ]
54
+ end
55
+
27
56
  attr_reader :msg
28
57
  attr_reader :pid # process_id
29
58
  attr_reader :tid # thread_id
@@ -31,15 +60,15 @@ module LogUtils
31
60
 
32
61
 
33
62
  def fatal?
34
- @level == FATAL
63
+ @levelno == FATAL
35
64
  end
36
65
 
37
66
  def error?
38
- @level == ERROR
67
+ @levelno == ERROR
39
68
  end
40
69
 
41
70
  def warn?
42
- @level == WARN
71
+ @levelno == WARN
43
72
  end
44
73
 
45
74
 
@@ -66,7 +95,11 @@ module LogUtils
66
95
 
67
96
  class Logger
68
97
  include Level
69
-
98
+
99
+ def self.[]( class_or_name )
100
+ STDLOGGER
101
+ end
102
+
70
103
  def initialize
71
104
  @listeners = []
72
105
  @listeners << STDLISTENER # by default log to console
@@ -1,5 +1,9 @@
1
1
 
2
2
  module LogUtils
3
- VERSION = '0.3.0'
4
- end # module LogUtils
3
+ # NB: keep LogUtils namespace clean
4
+ # only include public API that gets included w/ include LogUtils in other modules
5
+ end
5
6
 
7
+ module LogUtils::Kernel
8
+ VERSION = '0.4.0'
9
+ end
@@ -0,0 +1,15 @@
1
+
2
+ ## $:.unshift(File.dirname(__FILE__))
3
+
4
+
5
+ ## minitest setup
6
+
7
+ # require 'minitest/unit'
8
+ require 'minitest/autorun'
9
+
10
+ # include MiniTest::Unit # lets us use TestCase instead of MiniTest::Unit::TestCase
11
+
12
+
13
+ ## our own code
14
+
15
+ require 'logutils'
@@ -0,0 +1,45 @@
1
+ ###
2
+ # to run use
3
+ # ruby -I ./lib -I ./test test/test_logger.rb
4
+ # or better
5
+ # rake test
6
+
7
+ require 'test_helper'
8
+
9
+ class TestLogger < MiniTest::Unit::TestCase
10
+ include LogUtils # lets us use Logger instead of LogUtils::Logger
11
+
12
+ ####
13
+ # just for testing testing machinery
14
+ # simple tests
15
+
16
+ def test_root
17
+ l1 = Logger[ self ]
18
+ l2 = Logger[ 'Test' ]
19
+ l3 = Logger[ TestLogger ]
20
+
21
+ ### nb: for now all return LogUtils::Kernel::STDLOGGER
22
+
23
+ assert(l1 == LogUtils::Kernel::STDLOGGER )
24
+ assert(l2 == LogUtils::Kernel::STDLOGGER )
25
+ assert(l3 == LogUtils::Kernel::STDLOGGER )
26
+ end
27
+
28
+ class SampleClass
29
+ include LogUtils # NB: constant from containing class not available (only if class is nested in module)
30
+ include Logging
31
+ end
32
+
33
+ class Sample2Class
34
+ include LogUtils::Logging
35
+ end
36
+
37
+ def test_class
38
+ obj = SampleClass.new
39
+ obj2 = Sample2Class.new
40
+
41
+ assert( obj.logger == LogUtils::Kernel::STDLOGGER )
42
+ assert( obj2.logger == LogUtils::Kernel::STDLOGGER )
43
+ end
44
+
45
+ end
metadata CHANGED
@@ -1,61 +1,45 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: logutils
3
- version: !ruby/object:Gem::Version
4
- hash: 19
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 3
9
- - 0
10
- version: 0.3.0
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Gerald Bauer
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2013-02-21 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2013-02-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: rdoc
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &69924980 !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
18
+ requirements:
26
19
  - - ~>
27
- - !ruby/object:Gem::Version
28
- hash: 19
29
- segments:
30
- - 3
31
- - 10
32
- version: "3.10"
20
+ - !ruby/object:Gem::Version
21
+ version: '3.10'
33
22
  type: :development
34
- version_requirements: *id001
35
- - !ruby/object:Gem::Dependency
36
- name: hoe
37
23
  prerelease: false
38
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: *69924980
25
+ - !ruby/object:Gem::Dependency
26
+ name: hoe
27
+ requirement: &69924650 !ruby/object:Gem::Requirement
39
28
  none: false
40
- requirements:
29
+ requirements:
41
30
  - - ~>
42
- - !ruby/object:Gem::Version
43
- hash: 1
44
- segments:
45
- - 3
46
- - 3
47
- version: "3.3"
31
+ - !ruby/object:Gem::Version
32
+ version: '3.3'
48
33
  type: :development
49
- version_requirements: *id002
34
+ prerelease: false
35
+ version_requirements: *69924650
50
36
  description: Another Logger
51
37
  email: opensport@googlegroups.com
52
38
  executables: []
53
-
54
39
  extensions: []
55
-
56
- extra_rdoc_files:
40
+ extra_rdoc_files:
57
41
  - Manifest.txt
58
- files:
42
+ files:
59
43
  - History.md
60
44
  - Manifest.txt
61
45
  - README.md
@@ -67,43 +51,36 @@ files:
67
51
  - lib/logutils/db/schema.rb
68
52
  - lib/logutils/logger.rb
69
53
  - lib/logutils/version.rb
70
- - test/test.rb
54
+ - test/test_helper.rb
55
+ - test/test_logger.rb
71
56
  - .gemtest
72
57
  homepage: https://github.com/geraldb/logutils
73
- licenses:
58
+ licenses:
74
59
  - Public Domain
75
60
  post_install_message:
76
- rdoc_options:
61
+ rdoc_options:
77
62
  - --main
78
63
  - README.md
79
- require_paths:
64
+ require_paths:
80
65
  - lib
81
- required_ruby_version: !ruby/object:Gem::Requirement
66
+ required_ruby_version: !ruby/object:Gem::Requirement
82
67
  none: false
83
- requirements:
84
- - - ">="
85
- - !ruby/object:Gem::Version
86
- hash: 55
87
- segments:
88
- - 1
89
- - 9
90
- - 2
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
91
71
  version: 1.9.2
92
- required_rubygems_version: !ruby/object:Gem::Requirement
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
73
  none: false
94
- requirements:
95
- - - ">="
96
- - !ruby/object:Gem::Version
97
- hash: 3
98
- segments:
99
- - 0
100
- version: "0"
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
101
78
  requirements: []
102
-
103
79
  rubyforge_project: logutils
104
- rubygems_version: 1.8.24
80
+ rubygems_version: 1.8.17
105
81
  signing_key:
106
82
  specification_version: 3
107
83
  summary: Another Logger
108
- test_files: []
109
-
84
+ test_files:
85
+ - test/test_helper.rb
86
+ - test/test_logger.rb
@@ -1,33 +0,0 @@
1
-
2
- puts "before require 'logutils'"
3
- require 'logutils'
4
-
5
- puts "LogUtils::VERSION #{LogUtils::VERSION}"
6
-
7
- logger = LogUtils[ 'Test' ]
8
- logger.info 'hello LogUtils'
9
-
10
- puts "before require 'logutils/db'"
11
- require 'logutils/db'
12
-
13
-
14
- LOG_DB_PATH = './log.db'
15
-
16
- LOG_DB_CONFIG = {
17
- :adapter => 'sqlite3',
18
- :database => LOG_DB_PATH
19
- }
20
-
21
- require 'active_record'
22
-
23
- pp LOG_DB_CONFIG
24
- ActiveRecord::Base.establish_connection( LOG_DB_CONFIG )
25
-
26
-
27
- LogDb.create
28
-
29
- LogDb.setup
30
-
31
- logger.info 'hola LogUtils'
32
- logger.warn 'servus LogUtils'
33
-