logutils 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gemtest +0 -0
- data/Manifest.txt +2 -0
- data/README.md +58 -0
- data/lib/logutils.rb +11 -33
- data/lib/logutils/db.rb +74 -0
- data/lib/logutils/db/deleter.rb +5 -5
- data/lib/logutils/db/models.rb +2 -2
- data/lib/logutils/db/schema.rb +4 -4
- data/lib/logutils/logger.rb +44 -11
- data/lib/logutils/version.rb +1 -1
- data/test/test.rb +33 -0
- metadata +7 -4
data/.gemtest
ADDED
File without changes
|
data/Manifest.txt
CHANGED
data/README.md
CHANGED
@@ -20,6 +20,64 @@ use methods e.g.
|
|
20
20
|
logger.error "another msg"
|
21
21
|
logger.fatal "another msg"
|
22
22
|
|
23
|
+
To get a Logger use
|
24
|
+
|
25
|
+
logger = LogUtils[ self ]
|
26
|
+
|
27
|
+
or
|
28
|
+
|
29
|
+
logger = LogUtils[ 'SportDb::Reader' ]
|
30
|
+
|
31
|
+
### Log to the database using `LogDb`
|
32
|
+
|
33
|
+
NB: To use the `LogDb` machinery require the module, that is, issue:
|
34
|
+
|
35
|
+
require 'logutils/db'
|
36
|
+
|
37
|
+
To create the database tables use:
|
38
|
+
|
39
|
+
LogDb.create
|
40
|
+
|
41
|
+
To start logging to the database (established connection required) use:
|
42
|
+
|
43
|
+
LogDb.setup
|
44
|
+
|
45
|
+
To clean out all log records from the database use:
|
46
|
+
|
47
|
+
LogDb.delete!
|
48
|
+
|
49
|
+
|
50
|
+
### All together now
|
51
|
+
|
52
|
+
|
53
|
+
require 'logutils'
|
54
|
+
|
55
|
+
logger = LogUtils[ 'Test' ]
|
56
|
+
logger.info 'hello LogUtils'
|
57
|
+
|
58
|
+
require 'logutils/db'
|
59
|
+
|
60
|
+
LOG_DB_PATH = './log.db'
|
61
|
+
|
62
|
+
LOG_DB_CONFIG = {
|
63
|
+
:adapter => 'sqlite3',
|
64
|
+
:database => LOG_DB_PATH
|
65
|
+
}
|
66
|
+
|
67
|
+
require 'active_record'
|
68
|
+
|
69
|
+
pp LOG_DB_CONFIG
|
70
|
+
ActiveRecord::Base.establish_connection( LOG_DB_CONFIG )
|
71
|
+
|
72
|
+
LogDb.create
|
73
|
+
LogDb.setup
|
74
|
+
|
75
|
+
logger.info 'hola LogUtils'
|
76
|
+
logger.warn 'servus LogUtils'
|
77
|
+
|
78
|
+
|
79
|
+
That's it.
|
80
|
+
|
23
81
|
|
24
82
|
## Alternatives
|
25
83
|
|
data/lib/logutils.rb
CHANGED
@@ -5,6 +5,7 @@
|
|
5
5
|
#
|
6
6
|
# 1.9.x: ruby -Ilib lib/logutils.rb
|
7
7
|
|
8
|
+
|
8
9
|
# core and stlibs
|
9
10
|
|
10
11
|
require 'yaml'
|
@@ -13,32 +14,16 @@ require 'logger'
|
|
13
14
|
require 'fileutils'
|
14
15
|
|
15
16
|
|
16
|
-
# rubygems / 3rd party libs
|
17
|
-
|
18
|
-
require 'active_record' ## todo: add sqlite3? etc.
|
19
|
-
|
20
|
-
|
21
17
|
# our own code
|
22
18
|
|
23
19
|
require 'logutils/version'
|
24
20
|
|
25
|
-
require 'logutils/db/models'
|
26
|
-
require 'logutils/db/schema'
|
27
|
-
require 'logutils/db/deleter'
|
28
|
-
|
29
21
|
require 'logutils/logger'
|
30
22
|
|
31
23
|
|
32
24
|
module LogUtils
|
33
25
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
module LogDB
|
38
|
-
|
39
|
-
def self.banner
|
40
|
-
"logdb #{LogUtils::VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
|
41
|
-
end
|
26
|
+
=begin # not needed for now; keep it for later
|
42
27
|
|
43
28
|
def self.root
|
44
29
|
"#{File.expand_path( File.dirname(File.dirname(__FILE__)) )}"
|
@@ -48,23 +33,16 @@ module LogDB
|
|
48
33
|
"#{root}/config"
|
49
34
|
end
|
50
35
|
|
51
|
-
|
52
|
-
CreateDB.up
|
53
|
-
end
|
54
|
-
|
55
|
-
# delete ALL records (use with care!)
|
56
|
-
def self.delete!
|
57
|
-
puts '*** deleting log table records/data...'
|
58
|
-
Deleter.new.run
|
59
|
-
end # method delete!
|
36
|
+
=end
|
60
37
|
|
38
|
+
###########
|
39
|
+
# use it like:
|
40
|
+
# logger = LogUtils[ self ] or
|
41
|
+
# logger = LogUtils[ 'SportDb::Reader' ] etc.
|
61
42
|
|
62
|
-
def self.
|
63
|
-
#
|
43
|
+
def self.[]( class_or_name )
|
44
|
+
# for now always return single instance, that is, use standard/default logger for all
|
45
|
+
STDLOGGER
|
64
46
|
end
|
65
47
|
|
66
|
-
|
67
|
-
end # module LogDB
|
68
|
-
|
69
|
-
## say hello
|
70
|
-
puts LogDB.banner
|
48
|
+
end # module LogUtils
|
data/lib/logutils/db.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
|
2
|
+
#########################################
|
3
|
+
# NB: only load on demand
|
4
|
+
# we do NOT want to pull in activerecord gem/dep for simple scripts
|
5
|
+
|
6
|
+
# rubygems / 3rd party libs
|
7
|
+
|
8
|
+
require 'active_record' ## todo: add sqlite3? etc.
|
9
|
+
|
10
|
+
# our own code
|
11
|
+
|
12
|
+
require 'logutils/db/models'
|
13
|
+
require 'logutils/db/schema'
|
14
|
+
require 'logutils/db/deleter'
|
15
|
+
|
16
|
+
|
17
|
+
module LogDb
|
18
|
+
|
19
|
+
VERSION = LogUtils::VERSION
|
20
|
+
|
21
|
+
def self.banner
|
22
|
+
"logdb #{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
###########################################3
|
28
|
+
## fix: remove old alias for LogDb
|
29
|
+
|
30
|
+
LogDB = LogDb
|
31
|
+
|
32
|
+
###########################
|
33
|
+
|
34
|
+
module LogDb
|
35
|
+
|
36
|
+
class DbListener
|
37
|
+
include LogDB::Models
|
38
|
+
|
39
|
+
def write( ev )
|
40
|
+
if( ev.fatal? || ev.error? || ev.warn? )
|
41
|
+
## create log entry in db table (logs)
|
42
|
+
Log.create!( level: ev.level, msg: ev.msg, pid: ev.pid, tid: ev.tid, ts: ev.ts )
|
43
|
+
end
|
44
|
+
end # method write
|
45
|
+
end # class DbListener
|
46
|
+
|
47
|
+
STDDBLISTENER = DbListener.new # default/standard db listener
|
48
|
+
|
49
|
+
def self.create
|
50
|
+
CreateDb.up
|
51
|
+
end
|
52
|
+
|
53
|
+
# delete ALL records (use with care!)
|
54
|
+
def self.delete!
|
55
|
+
puts '*** deleting log table records/data...'
|
56
|
+
Deleter.new.run
|
57
|
+
end # method delete!
|
58
|
+
|
59
|
+
|
60
|
+
def self.stats
|
61
|
+
# to be done
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.setup # check: use different name? e.g. configure or connect ?? why or why not?
|
65
|
+
# turn on logging to db - assumes active connection
|
66
|
+
LogUtils::STDLOGGER.listeners << STDDBLISTENER
|
67
|
+
end
|
68
|
+
|
69
|
+
|
70
|
+
end # module LogDb
|
71
|
+
|
72
|
+
|
73
|
+
# say hello
|
74
|
+
puts LogDb.banner
|
data/lib/logutils/db/deleter.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
|
2
|
-
module
|
2
|
+
module LogDb
|
3
3
|
|
4
|
-
class Deleter
|
5
|
-
include
|
4
|
+
class Deleter
|
5
|
+
include LogDb::Models
|
6
6
|
|
7
|
-
def run
|
7
|
+
def run
|
8
8
|
# for now delete all tables
|
9
9
|
|
10
10
|
Log.delete_all
|
@@ -12,4 +12,4 @@ module LogDB
|
|
12
12
|
|
13
13
|
end # class Deleter
|
14
14
|
|
15
|
-
end # module
|
15
|
+
end # module LogDb
|
data/lib/logutils/db/models.rb
CHANGED
data/lib/logutils/db/schema.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
|
2
|
-
module
|
2
|
+
module LogDb
|
3
3
|
|
4
|
-
class
|
4
|
+
class CreateDb ## fix/todo: change to ActiveRecord::Migration why? why not?
|
5
5
|
|
6
6
|
def self.up
|
7
7
|
|
@@ -39,6 +39,6 @@ end
|
|
39
39
|
|
40
40
|
end # method up
|
41
41
|
|
42
|
-
end # class
|
42
|
+
end # class CreateDb
|
43
43
|
|
44
|
-
end # module
|
44
|
+
end # module LogDb
|
data/lib/logutils/logger.rb
CHANGED
@@ -8,8 +8,11 @@ module LogUtils
|
|
8
8
|
ERROR = 'error'
|
9
9
|
FATAL = 'fatal'
|
10
10
|
end
|
11
|
-
|
11
|
+
|
12
|
+
|
12
13
|
class Event
|
14
|
+
include Level
|
15
|
+
|
13
16
|
def initialize( level, msg )
|
14
17
|
@level = level
|
15
18
|
@msg = msg
|
@@ -27,17 +30,50 @@ module LogUtils
|
|
27
30
|
attr_reader :ts # timestamp
|
28
31
|
|
29
32
|
|
30
|
-
def
|
31
|
-
|
33
|
+
def fatal?
|
34
|
+
@level == FATAL
|
35
|
+
end
|
36
|
+
|
37
|
+
def error?
|
38
|
+
@level == ERROR
|
39
|
+
end
|
40
|
+
|
41
|
+
def warn?
|
42
|
+
@level == WARN
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
def to_s
|
47
|
+
# "[#{level}-#{pid}.#{tid}] #{msg}"
|
48
|
+
"[#{level}] #{msg}"
|
32
49
|
end
|
33
50
|
|
34
51
|
end # class Event
|
52
|
+
|
53
|
+
|
54
|
+
class ConsoleListener
|
55
|
+
def write( ev )
|
56
|
+
if( ev.fatal? || ev.error? || ev.warn? )
|
57
|
+
STDERR.puts ev.to_s
|
58
|
+
else
|
59
|
+
STDOUT.puts ev.to_s
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end # class ConsoleListener
|
35
63
|
|
36
|
-
|
64
|
+
STDLISTENER = ConsoleListener.new # default/standard console listener
|
65
|
+
|
66
|
+
|
37
67
|
class Logger
|
38
|
-
include LogDB::Models
|
39
68
|
include Level
|
40
69
|
|
70
|
+
def initialize
|
71
|
+
@listeners = []
|
72
|
+
@listeners << STDLISTENER # by default log to console
|
73
|
+
end
|
74
|
+
|
75
|
+
attr_reader :listeners
|
76
|
+
|
41
77
|
def debug( msg )
|
42
78
|
write( Event.new( DEBUG, msg ) )
|
43
79
|
end
|
@@ -61,14 +97,11 @@ module LogUtils
|
|
61
97
|
private
|
62
98
|
|
63
99
|
def write( ev )
|
64
|
-
|
65
|
-
|
66
|
-
if( [FATAL, ERROR, WARN].include?( ev.level ) )
|
67
|
-
## create log entry in db table (logs)
|
68
|
-
Log.create!( level: ev.level, msg: ev.msg, pid: ev.pid, tid: ev.tid, ts: ev.ts )
|
69
|
-
end
|
100
|
+
@listeners.each { |l| l.write( ev ) }
|
70
101
|
end
|
71
102
|
|
72
103
|
end # class Logger
|
73
104
|
|
105
|
+
STDLOGGER = Logger.new # single instance - default/standard logger
|
106
|
+
|
74
107
|
end # module LogUtils
|
data/lib/logutils/version.rb
CHANGED
data/test/test.rb
ADDED
@@ -0,0 +1,33 @@
|
|
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
|
+
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: logutils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 3
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.3.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Gerald Bauer
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2013-02-
|
18
|
+
date: 2013-02-21 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: rdoc
|
@@ -61,11 +61,14 @@ files:
|
|
61
61
|
- README.md
|
62
62
|
- Rakefile
|
63
63
|
- lib/logutils.rb
|
64
|
+
- lib/logutils/db.rb
|
64
65
|
- lib/logutils/db/deleter.rb
|
65
66
|
- lib/logutils/db/models.rb
|
66
67
|
- lib/logutils/db/schema.rb
|
67
68
|
- lib/logutils/logger.rb
|
68
69
|
- lib/logutils/version.rb
|
70
|
+
- test/test.rb
|
71
|
+
- .gemtest
|
69
72
|
homepage: https://github.com/geraldb/logutils
|
70
73
|
licenses:
|
71
74
|
- Public Domain
|