logutils 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.md ADDED
@@ -0,0 +1,3 @@
1
+ ### 0.1.0 / 2013-02-13
2
+
3
+ * Everything is new. First release
data/Manifest.txt ADDED
@@ -0,0 +1,11 @@
1
+ History.md
2
+ Manifest.txt
3
+ NOTES.md
4
+ README.md
5
+ Rakefile
6
+ lib/logutils.rb
7
+ lib/logutils/db/deleter.rb
8
+ lib/logutils/db/models.rb
9
+ lib/logutils/db/schema.rb
10
+ lib/logutils/logger.rb
11
+ lib/logutils/version.rb
data/NOTES.md ADDED
@@ -0,0 +1,81 @@
1
+ # Dev Notes
2
+
3
+ ## todo add name, level
4
+
5
+ LogUtils[ name ] or
6
+ LogUtils.logger( name ) e.g. LogUtils[ self ]
7
+
8
+ add logger to logger repo
9
+
10
+ ## todo support
11
+
12
+ logger.debug?
13
+ logger.warn?
14
+ etc.
15
+
16
+ ## todo support blocks for conditional evaluation
17
+
18
+ logger.debug { ruby code here }
19
+
20
+
21
+ ### How to get process id (pid)?
22
+
23
+ * Yell uses Process.pid
24
+ * Logging ???
25
+ * Log4r
26
+
27
+ ### How to get thread id (tid)?
28
+
29
+ * Yell uses Thread.current.object_id
30
+ * Logging ???
31
+ * Log4r ???
32
+
33
+ ### How to get timestamp (ts)?
34
+
35
+ * Yell uses Time.now
36
+
37
+ ### How to get filename, line, method?
38
+
39
+ use caller ?
40
+
41
+
42
+ ### Notes on logging levels/serverity
43
+
44
+ todo/check: use int constants or strings?
45
+
46
+ There are two things you should know about log levels/severity:
47
+ - syslog defines levels from 0 (Emergency) to 7 (Debug).
48
+ 0 (Emergency) and 1 (Alert) levels are reserved for OS kernel.
49
+ - Ruby default Logger defines levels from 0 (DEBUG) to 4 (FATAL) and 5 (UNKNOWN).
50
+ Note that order is inverted.
51
+ For compatibility we define our constants as Ruby Logger, and convert values before
52
+ generating GELF message, using defined mapping.
53
+
54
+ module Levels
55
+ DEBUG = 0
56
+ INFO = 1
57
+ WARN = 2
58
+ ERROR = 3
59
+ FATAL = 4
60
+ UNKNOWN = 5
61
+ end
62
+
63
+ include Levels
64
+
65
+ Maps Ruby Logger levels to syslog levels as SyslogLogger and syslogger gems. This one is default.
66
+ LOGGER_MAPPING = {DEBUG => 7, # Debug
67
+ INFO => 6, # Info
68
+ WARN => 5, # Notice
69
+ ERROR => 4, # Warning
70
+ FATAL => 3, # Error
71
+ UNKNOWN => 1} # Alert � shouldn't be used
72
+
73
+ Maps Ruby Logger levels to syslog levels as is.
74
+ DIRECT_MAPPING = {DEBUG => 7, # Debug
75
+ INFO => 6, # Info
76
+ # skip 5 Notice
77
+ WARN => 4, # Warning
78
+ ERROR => 3, # Error
79
+ FATAL => 2, # Critical
80
+ UNKNOWN => 1} # Alert � shouldn't be used
81
+ end
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # logutils
2
+
3
+ Another Logger in Ruby
4
+
5
+ * [github.com/geraldb/logutils](https://github.com/geraldb/logutils)
6
+
7
+ ## Usage
8
+
9
+ Logging levels:
10
+
11
+ DEBUG < INFO < WARN < ERROR < FATAL
12
+
13
+ use methods e.g.
14
+
15
+ logger = LoggerUtils::Logger.new
16
+
17
+ logger.debug "msg"
18
+ logger.info "another msg"
19
+ logger.warn "another msg"
20
+ logger.error "another msg"
21
+ logger.fatal "another msg"
22
+
23
+
24
+ ## Alternatives
25
+
26
+ * [Ruby Toolbox Logging Category](https://www.ruby-toolbox.com/categories/Logging)
27
+ * [log4r]()
28
+ * [slf4r](https://www.ruby-toolbox.com/projects/slf4r)
29
+ * [yell]()
30
+ * [logging](https://rubygems.org/gems/logging)
31
+
32
+ ## License
33
+
34
+ The `logutils` scripts are dedicated to the public domain.
35
+ Use it as you please with no restrictions whatsoever.
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ require 'hoe'
2
+ require './lib/logutils/version.rb'
3
+
4
+
5
+ Hoe.spec 'logutils' do
6
+
7
+ self.version = LogUtils::VERSION
8
+
9
+ self.summary = 'Another Logger'
10
+ self.description = summary
11
+
12
+ self.urls = ['https://github.com/geraldb/logutils']
13
+
14
+ self.author = 'Gerald Bauer'
15
+ self.email = 'opensport@googlegroups.com'
16
+
17
+ # switch extension to .markdown for gihub formatting
18
+ # -- NB: auto-changed when included in manifest
19
+ self.readme_file = 'README.md'
20
+ self.history_file = 'History.md'
21
+
22
+ self.licenses = ['Public Domain']
23
+
24
+ self.spec_extras = {
25
+ :required_ruby_version => '>= 1.9.2'
26
+ }
27
+
28
+ end
data/lib/logutils.rb ADDED
@@ -0,0 +1,70 @@
1
+ # encoding: utf-8
2
+
3
+ ###
4
+ # NB: for local testing run like:
5
+ #
6
+ # 1.9.x: ruby -Ilib lib/logutils.rb
7
+
8
+ # core and stlibs
9
+
10
+ require 'yaml'
11
+ require 'pp'
12
+ require 'logger'
13
+ require 'fileutils'
14
+
15
+
16
+ # rubygems / 3rd party libs
17
+
18
+ require 'active_record' ## todo: add sqlite3? etc.
19
+
20
+
21
+ # our own code
22
+
23
+ require 'logutils/version'
24
+
25
+ require 'logutils/db/models'
26
+ require 'logutils/db/schema'
27
+ require 'logutils/db/deleter'
28
+
29
+ require 'logutils/logger'
30
+
31
+
32
+ module LogUtils
33
+
34
+ end # module LogUtils
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
42
+
43
+ def self.root
44
+ "#{File.expand_path( File.dirname(File.dirname(__FILE__)) )}"
45
+ end
46
+
47
+ def self.config_path
48
+ "#{root}/config"
49
+ end
50
+
51
+ def self.create
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!
60
+
61
+
62
+ def self.stats
63
+ # to be done
64
+ end
65
+
66
+
67
+ end # module LogDB
68
+
69
+ ## say hello
70
+ puts LogDB.banner
@@ -0,0 +1,15 @@
1
+
2
+ module LogDB
3
+
4
+ class Deleter
5
+ include LogDB::Models
6
+
7
+ def run( args=[] )
8
+ # for now delete all tables
9
+
10
+ Log.delete_all
11
+ end
12
+
13
+ end # class Deleter
14
+
15
+ end # module LogDB
@@ -0,0 +1,13 @@
1
+ # encoding: utf-8
2
+
3
+ module LogDB
4
+ module Models
5
+
6
+
7
+ class Log < ActiveRecord::Base
8
+
9
+ end # class Log
10
+
11
+
12
+ end # module Models
13
+ end # module LogDB
@@ -0,0 +1,44 @@
1
+
2
+ module LogDB
3
+
4
+ class CreateDB ## fix/todo: change to ActiveRecord::Migration why? why not?
5
+
6
+ def self.up
7
+
8
+ ActiveRecord::Schema.define do
9
+
10
+ create_table :logs do |t|
11
+ t.string :msg, :null => false
12
+ t.string :level, :null => false # e.g. fatal, error, warn, info, debug
13
+ t.string :app
14
+ t.string :tag # e.g. class name w/ namespace e.g. SportDB.Reader etc. # NB: change to name?
15
+ t.integer :pid
16
+ t.string :tid # NB: thread_id - change to integer ??
17
+ t.string :ts # timestamp - change to datetime?
18
+ # add filename, line, method ??
19
+ t.timestamps
20
+ end
21
+
22
+ #####################################
23
+ # todo: add more fields ?? e.g.
24
+ #
25
+ # %m : The message to be logged
26
+ # %d : The ISO8601 Timestamp
27
+ # %L : The log level, e.g INFO, WARN
28
+ # %l : The log level (short), e.g. I, W
29
+ # %p : The PID of the process from where the log event occured
30
+ # %t : The Thread ID from where the log event occured
31
+ # %h : The hostname of the machine from where the log event occured
32
+ # %f : The filename from where the log event occured
33
+ # %n : The line number of the file from where the log event occured
34
+ # %F : The filename with path from where the log event occured
35
+ # %M : The method where the log event occured
36
+
37
+
38
+ end # block Schema.define
39
+
40
+ end # method up
41
+
42
+ end # class CreateDB
43
+
44
+ end # module LogDB
@@ -0,0 +1,74 @@
1
+
2
+ module LogUtils
3
+
4
+ module Level
5
+ DEBUG = 'debug'
6
+ INFO = 'info'
7
+ WARN = 'warn'
8
+ ERROR = 'error'
9
+ FATAL = 'fatal'
10
+ end
11
+
12
+ class Event
13
+ def initialize( level, msg )
14
+ @level = level
15
+ @msg = msg
16
+
17
+ @pid = Process.pid
18
+ @tid = Thread.current.object_id
19
+
20
+ @ts = Time.now
21
+ end
22
+
23
+ attr_reader :level
24
+ attr_reader :msg
25
+ attr_reader :pid # process_id
26
+ attr_reader :tid # thread_id
27
+ attr_reader :ts # timestamp
28
+
29
+
30
+ def to_s()
31
+ "[#{level}-#{pid}.#{tid}] #{msg}"
32
+ end
33
+
34
+ end # class Event
35
+
36
+
37
+ class Logger
38
+ include LogDB::Models
39
+ include Level
40
+
41
+ def debug( msg )
42
+ write( Event.new( DEBUG, msg ) )
43
+ end
44
+
45
+ def info( msg )
46
+ write( Event.new( INFO, msg ) )
47
+ end
48
+
49
+ def warn( msg )
50
+ write( Event.new( WARN, msg ) )
51
+ end
52
+
53
+ def error( msg )
54
+ write( Event.new( ERROR, msg ) )
55
+ end
56
+
57
+ def fatal( msg )
58
+ write( Event.new( FATAL, msg ) )
59
+ end
60
+
61
+ private
62
+
63
+ def write( ev )
64
+ puts ev.to_s
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
70
+ end
71
+
72
+ end # class Logger
73
+
74
+ end # module LogUtils
@@ -0,0 +1,5 @@
1
+
2
+ module LogUtils
3
+ VERSION = '0.1.0'
4
+ end # module LogUtils
5
+
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logutils
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Gerald Bauer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rdoc
16
+ requirement: &21073536 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.10'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *21073536
25
+ - !ruby/object:Gem::Dependency
26
+ name: hoe
27
+ requirement: &21072828 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '3.5'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *21072828
36
+ description: Another Logger
37
+ email: opensport@googlegroups.com
38
+ executables: []
39
+ extensions: []
40
+ extra_rdoc_files:
41
+ - Manifest.txt
42
+ files:
43
+ - History.md
44
+ - Manifest.txt
45
+ - NOTES.md
46
+ - README.md
47
+ - Rakefile
48
+ - lib/logutils.rb
49
+ - lib/logutils/db/deleter.rb
50
+ - lib/logutils/db/models.rb
51
+ - lib/logutils/db/schema.rb
52
+ - lib/logutils/logger.rb
53
+ - lib/logutils/version.rb
54
+ homepage: https://github.com/geraldb/logutils
55
+ licenses:
56
+ - Public Domain
57
+ post_install_message:
58
+ rdoc_options:
59
+ - --main
60
+ - README.md
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: 1.9.2
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project: logutils
77
+ rubygems_version: 1.8.16
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Another Logger
81
+ test_files: []