logutils 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,5 +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_helper.rb
12
+ test/helper.rb
13
13
  test/test_logger.rb
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # logutils
2
2
 
3
+ [![Build Status](https://secure.travis-ci.org/geraldb/logutils.png?branch=master)](http://travis-ci.org/geraldb/logutils)
4
+
3
5
  Another Logger in Ruby
4
6
 
5
7
  * [github.com/geraldb/logutils](https://github.com/geraldb/logutils)
@@ -11,7 +13,7 @@ Logging levels:
11
13
  [ALL] < DEBUG < INFO < WARN < ERROR < FATAL < [OFF]
12
14
 
13
15
 
14
- Start off getting a logger e.g.:
16
+ Start by getting a logger e.g.:
15
17
 
16
18
  logger = LogUtils::Logger.new
17
19
 
@@ -90,7 +92,6 @@ To clean out all log records from the database use:
90
92
 
91
93
  ### All together now
92
94
 
93
-
94
95
  require 'logutils'
95
96
 
96
97
  include LogUtils # lets you use Logger instead of LogUtils::Logger
@@ -98,17 +99,14 @@ To clean out all log records from the database use:
98
99
  logger = Logger[ 'Test' ]
99
100
  logger.info 'hello LogUtils'
100
101
 
101
- require 'logutils/db'
102
102
 
103
- LOG_DB_PATH = './log.db'
103
+ require 'logutils/db' # nb: will also require 'active_record'
104
104
 
105
105
  LOG_DB_CONFIG = {
106
106
  :adapter => 'sqlite3',
107
- :database => LOG_DB_PATH
107
+ :database => './log.db'
108
108
  }
109
109
 
110
- require 'active_record'
111
-
112
110
  pp LOG_DB_CONFIG
113
111
  ActiveRecord::Base.establish_connection( LOG_DB_CONFIG )
114
112
 
@@ -121,19 +119,30 @@ To clean out all log records from the database use:
121
119
 
122
120
  That's it.
123
121
 
122
+
123
+ ## Real World Usage
124
+
125
+ [world.db.ruby](https://github.com/geraldb/world.db.ruby) - `world.db` Command Line Tool
126
+
127
+ [sport.db.ruby](https://github.com/geraldb/sport.db.ruby) - `sport.db` Command Line Tool
128
+
129
+ [Sportbook](http://geraldb.github.com/sportbook) - A free, open source sports betting pool
130
+ in Ruby on Rails (version 3.2 and up).
131
+
132
+
124
133
  ## Todos
125
134
 
126
135
  - [ ] Add UNKNOWN level - why? why not? check std logger
127
136
 
128
137
 
129
-
130
138
  ## Alternatives
131
139
 
140
+ * [log4r](https://github.com/colbygk/log4r) - Logging Library modeled after log4j in the Java world
141
+ * [slf4r](https://github.com/mkristian/slf4r) - Logging Library modeled after slf4j in the Java world
142
+ * [yell](https://github.com/rudionrails/yell)
143
+ * [logging](https://github.com/TwP/logging)
132
144
  * [Ruby Toolbox Logging Category](https://www.ruby-toolbox.com/categories/Logging)
133
- * [log4r]()
134
- * [slf4r](https://www.ruby-toolbox.com/projects/slf4r)
135
- * [yell]()
136
- * [logging](https://rubygems.org/gems/logging)
145
+
137
146
 
138
147
  ## License
139
148
 
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::Kernel::VERSION
7
+ self.version = LogKernel::VERSION
8
8
 
9
9
  self.summary = 'Another Logger'
10
10
  self.description = summary
@@ -25,4 +25,4 @@ Hoe.spec 'logutils' do
25
25
  :required_ruby_version => '>= 1.9.2'
26
26
  }
27
27
 
28
- end
28
+ end
@@ -20,7 +20,7 @@ require 'logutils/version'
20
20
  require 'logutils/logger'
21
21
 
22
22
 
23
- module LogUtils::Kernel
23
+ module LogKernel
24
24
  =begin # not needed for now; keep it for later
25
25
 
26
26
  def self.root
@@ -45,7 +45,7 @@ module LogUtils
45
45
  puts "depreceated API call LogUtils[] - use Logger[] / LogUtils::Logger[] instead"
46
46
 
47
47
  # for now always return single instance, that is, use standard/default logger for all
48
- LogUtils::Kernel::STDLOGGER
48
+ LogKernel::STDLOGGER
49
49
  end
50
50
 
51
51
  ###################################
@@ -58,9 +58,7 @@ module LogUtils
58
58
  # logger = Logger[ SportDb::Reader ] or
59
59
  # logger = Logger[ 'SportDb::Reader' ] etc.
60
60
 
61
- Logger = LogUtils::Kernel::Logger
62
- include LogUtils::Kernel::Level # e.g. export ALL,DEBUG,INFO,WARN,etc.
63
-
61
+ Logger = LogKernel::Logger
64
62
 
65
63
 
66
64
  #####
@@ -89,12 +87,9 @@ module LogUtils
89
87
  # end
90
88
 
91
89
  module Logging
92
- include LogUtils::Kernel::Level # e.g. lets you use ALL,DEBUG,INFO,WARN,etc.
93
-
94
90
  def logger
95
- @logger ||= LogUtils::Kernel::Logger[ self ]
91
+ @logger ||= LogKernel::Logger[ self ]
96
92
  end
97
93
  end
98
94
 
99
95
  end # module LogUtils
100
-
@@ -16,7 +16,7 @@ require 'logutils/db/deleter'
16
16
 
17
17
  module LogDb
18
18
 
19
- include LogUtils::Kernel # NB: will also include VERSION constant e.g. LogDB::VERSION == LogUtils::Kernel::VERSION
19
+ include LogKernel # 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}]"
@@ -29,22 +29,22 @@ end
29
29
 
30
30
  LogDB = LogDb
31
31
 
32
+
33
+
32
34
  ###########################
33
35
 
34
36
  module LogDb
35
37
 
36
- class DbListener
38
+ class DbHandler
37
39
  include LogDb::Models
38
40
 
39
41
  def write( ev )
40
- if( ev.fatal? || ev.error? || ev.warn? )
42
+ if( ev.fatal? || ev.error? || ev.warn? || ev.unknown? )
41
43
  ## create log entry in db table (logs)
42
44
  Log.create!( level: ev.level, msg: ev.msg, pid: ev.pid, tid: ev.tid, ts: ev.ts )
43
45
  end
44
46
  end # method write
45
- end # class DbListener
46
-
47
- STDDBLISTENER = DbListener.new # default/standard db listener
47
+ end # class DbHandler
48
48
 
49
49
  def self.create
50
50
  CreateDb.up
@@ -61,11 +61,14 @@ module LogDb
61
61
  # to be done
62
62
  end
63
63
 
64
+
65
+ STDDBHANDLER = DbHandler.new # default/standard db handler
66
+
64
67
  def self.setup # check: use different name? e.g. configure or connect ?? why or why not?
65
68
  # turn on logging to db - assumes active connection
66
- STDLOGGER.listeners << STDDBLISTENER
69
+ STDLOGGER.handlers << STDDBHANDLER
67
70
  end
68
-
71
+
69
72
 
70
73
  end # module LogDb
71
74
 
@@ -1,7 +1,9 @@
1
1
 
2
- module LogUtils::Kernel
2
+ module LogKernel
3
3
 
4
- ## private "kernel" - with include LogUtils - do NOT include everything
4
+ #####
5
+ # private "kernel" do NOT include everything into LogUtils
6
+ # only "export" public API in LogUtils
5
7
 
6
8
 
7
9
  #####
@@ -15,30 +17,43 @@ module LogUtils::Kernel
15
17
  # debug = 5 etc.
16
18
 
17
19
  module Level
18
- ALL = 0
19
- DEBUG = 1
20
- INFO = 2
21
- WARN = 3
22
- ERROR = 4
23
- FATAL = 5
24
- OFF = 6
20
+ ALL = 0
21
+ DEBUG = 1
22
+ INFO = 2
23
+ WARN = 3
24
+ ERROR = 4
25
+ FATAL = 5
26
+ UNKNOWN = 6 ## make unknown higher than off? why? why not?
27
+ OFF = 7
25
28
  end
26
29
 
30
+ LEVEL_NAME =
31
+ [
32
+ 'all', # 0
33
+ 'debug', # 1
34
+ 'info', # 2
35
+ 'warn', # 3
36
+ 'error', # 4
37
+ 'fatal', # 5
38
+ 'unknown', # 6
39
+ 'off' # 7
40
+ ]
41
+
42
+ # map symbol to levelno; if not match map to UNKNOWN
43
+ LEVEL_HASH = {
44
+ all: Level::ALL,
45
+ debug: Level::DEBUG,
46
+ info: Level::INFO,
47
+ warn: Level::WARN,
48
+ error: Level::ERROR,
49
+ fatal: Level::FATAL,
50
+ off: Level::OFF
51
+ }
52
+
27
53
 
28
54
  class Event
29
55
  include Level # lets you access Level::DEBUG with just DEBUG etc.
30
56
 
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
57
  def initialize( levelno, msg )
43
58
  @levelno = levelno # pass in integer e.g. 0,1,2,3,etc.
44
59
  @msg = msg
@@ -52,7 +67,8 @@ module LogUtils::Kernel
52
67
  def level
53
68
  LEVEL_NAME[ @levelno ]
54
69
  end
55
-
70
+
71
+ attr_reader :levelno
56
72
  attr_reader :msg
57
73
  attr_reader :pid # process_id
58
74
  attr_reader :tid # thread_id
@@ -70,6 +86,10 @@ module LogUtils::Kernel
70
86
  def warn?
71
87
  @levelno == WARN
72
88
  end
89
+
90
+ def unknown?
91
+ @levelno == UNKNOWN
92
+ end
73
93
 
74
94
 
75
95
  def to_s
@@ -80,17 +100,17 @@ module LogUtils::Kernel
80
100
  end # class Event
81
101
 
82
102
 
83
- class ConsoleListener
103
+ class ConsoleHandler
84
104
  def write( ev )
85
- if( ev.fatal? || ev.error? || ev.warn? )
105
+ if( ev.fatal? || ev.error? || ev.warn? || ev.unknown? )
86
106
  STDERR.puts ev.to_s
87
107
  else
88
108
  STDOUT.puts ev.to_s
89
109
  end
90
110
  end
91
- end # class ConsoleListener
111
+ end # class ConsoleHandler
92
112
 
93
- STDLISTENER = ConsoleListener.new # default/standard console listener
113
+ STDHANDLER = ConsoleHandler.new # default/standard log handler
94
114
 
95
115
 
96
116
  class Logger
@@ -99,14 +119,33 @@ module LogUtils::Kernel
99
119
  def self.[]( class_or_name )
100
120
  STDLOGGER
101
121
  end
102
-
122
+
123
+ def self.root
124
+ STDLOGGER
125
+ end
126
+
103
127
  def initialize
104
- @listeners = []
105
- @listeners << STDLISTENER # by default log to console
128
+ @handlers = []
129
+ @handlers << STDHANDLER # by default log to console
130
+
131
+ ## todo/check: pass levelno through to handlers?
132
+ # do we need a "global" levelno in the logger?
133
+ @levelno = ALL
106
134
  end
107
135
 
108
- attr_reader :listeners
136
+ attr_reader :handlers
137
+
138
+
139
+ #####
140
+ # NB: use :debug, :info, :warn, :error, etc as argument
141
+ #
142
+ # if invalid argument gets passed in, level gets set to UNKNOWN
109
143
 
144
+ def level=( key )
145
+ @levelno = LEVEL_HASH.fetch( key, UNKNOWN )
146
+ end
147
+
148
+
110
149
  def debug( msg )
111
150
  write( Event.new( DEBUG, msg ) )
112
151
  end
@@ -126,15 +165,21 @@ module LogUtils::Kernel
126
165
  def fatal( msg )
127
166
  write( Event.new( FATAL, msg ) )
128
167
  end
168
+
169
+ def unknown( msg )
170
+ write( Event.new( UNKNOWN, msg ) )
171
+ end
129
172
 
130
173
  private
131
174
 
132
175
  def write( ev )
133
- @listeners.each { |l| l.write( ev ) }
176
+ if ev.levelno >= @levelno
177
+ @handlers.each { |l| l.write( ev ) }
178
+ end
134
179
  end
135
180
 
136
181
  end # class Logger
137
182
 
138
183
  STDLOGGER = Logger.new # single instance - default/standard logger
139
184
 
140
- end # module LogUtils
185
+ end # module LogKernel
@@ -2,8 +2,9 @@
2
2
  module LogUtils
3
3
  # NB: keep LogUtils namespace clean
4
4
  # only include public API that gets included w/ include LogUtils in other modules
5
+ # move all non public code/apis to LogKernel (including VERSION)
5
6
  end
6
7
 
7
- module LogUtils::Kernel
8
- VERSION = '0.4.0'
8
+ module LogKernel
9
+ VERSION = '0.5.0'
9
10
  end
File without changes
@@ -4,7 +4,7 @@
4
4
  # or better
5
5
  # rake test
6
6
 
7
- require 'test_helper'
7
+ require 'helper'
8
8
 
9
9
  class TestLogger < MiniTest::Unit::TestCase
10
10
  include LogUtils # lets us use Logger instead of LogUtils::Logger
@@ -20,9 +20,9 @@ class TestLogger < MiniTest::Unit::TestCase
20
20
 
21
21
  ### nb: for now all return LogUtils::Kernel::STDLOGGER
22
22
 
23
- assert(l1 == LogUtils::Kernel::STDLOGGER )
24
- assert(l2 == LogUtils::Kernel::STDLOGGER )
25
- assert(l3 == LogUtils::Kernel::STDLOGGER )
23
+ assert(l1 == LogKernel::STDLOGGER )
24
+ assert(l2 == LogKernel::STDLOGGER )
25
+ assert(l3 == LogKernel::STDLOGGER )
26
26
  end
27
27
 
28
28
  class SampleClass
@@ -38,8 +38,8 @@ class TestLogger < MiniTest::Unit::TestCase
38
38
  obj = SampleClass.new
39
39
  obj2 = Sample2Class.new
40
40
 
41
- assert( obj.logger == LogUtils::Kernel::STDLOGGER )
42
- assert( obj2.logger == LogUtils::Kernel::STDLOGGER )
41
+ assert( obj.logger == LogKernel::STDLOGGER )
42
+ assert( obj2.logger == LogKernel::STDLOGGER )
43
43
  end
44
44
 
45
45
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logutils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-23 00:00:00.000000000 Z
12
+ date: 2013-02-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rdoc
16
- requirement: &69924980 !ruby/object:Gem::Requirement
16
+ requirement: &72260240 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.10'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *69924980
24
+ version_requirements: *72260240
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: hoe
27
- requirement: &69924650 !ruby/object:Gem::Requirement
27
+ requirement: &72259940 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '3.3'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *69924650
35
+ version_requirements: *72259940
36
36
  description: Another Logger
37
37
  email: opensport@googlegroups.com
38
38
  executables: []
@@ -51,7 +51,7 @@ files:
51
51
  - lib/logutils/db/schema.rb
52
52
  - lib/logutils/logger.rb
53
53
  - lib/logutils/version.rb
54
- - test/test_helper.rb
54
+ - test/helper.rb
55
55
  - test/test_logger.rb
56
56
  - .gemtest
57
57
  homepage: https://github.com/geraldb/logutils
@@ -82,5 +82,4 @@ signing_key:
82
82
  specification_version: 3
83
83
  summary: Another Logger
84
84
  test_files:
85
- - test/test_helper.rb
86
85
  - test/test_logger.rb