hatchet 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +22 -0
- data/lib/hatchet.rb +145 -0
- data/lib/hatchet/version.rb +3 -0
- data/test/experiment.rb +47 -0
- metadata +58 -0
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Garry Shutler
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/lib/hatchet.rb
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
require_relative 'hatchet/version'
|
2
|
+
|
3
|
+
module Hatchet
|
4
|
+
|
5
|
+
def logger
|
6
|
+
@_hatchet_logger ||= Logger.new self, Hatchet.appenders
|
7
|
+
end
|
8
|
+
|
9
|
+
alias_method :log, :logger
|
10
|
+
|
11
|
+
def self.configure
|
12
|
+
@@config = Configuration.new
|
13
|
+
yield @@config
|
14
|
+
@@config.appenders.each do |appender|
|
15
|
+
appender.formatter ||= StandardFormatter.new
|
16
|
+
appender.levels ||= @@config.levels
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.appenders
|
21
|
+
if @@config and @@config.appenders
|
22
|
+
@@config.appenders
|
23
|
+
else
|
24
|
+
[]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class LoggerAppender
|
29
|
+
|
30
|
+
LEVELS = [:trace, :debug, :info, :warn, :error, :fatal, :off]
|
31
|
+
|
32
|
+
attr_accessor :levels
|
33
|
+
|
34
|
+
attr_accessor :logger
|
35
|
+
|
36
|
+
attr_accessor :formatter
|
37
|
+
|
38
|
+
def initialize(args = {})
|
39
|
+
@logger = args[:logger]
|
40
|
+
@formatter = args[:formatter]
|
41
|
+
yield self
|
42
|
+
@logger.formatter = proc do |severity, datetime, progname, msg|
|
43
|
+
"#{timestamp} [#{thread_name}] #{severity.ljust 5} #{msg}\n"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def add(level, context, msg)
|
48
|
+
return unless enabled? context, level
|
49
|
+
@logger.send level, @formatter.format(context, msg)
|
50
|
+
end
|
51
|
+
|
52
|
+
def enabled?(context, level)
|
53
|
+
unless self.levels.key? context
|
54
|
+
lvl = self.levels[nil]
|
55
|
+
root = []
|
56
|
+
context.to_s.split('::').each do |part|
|
57
|
+
root << part
|
58
|
+
path = root.join '::'
|
59
|
+
lvl = self.levels[path] if self.levels.key? path
|
60
|
+
end
|
61
|
+
self.levels[context] = lvl
|
62
|
+
end
|
63
|
+
LEVELS.index(level) >= LEVELS.index(self.levels[context])
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def timestamp
|
69
|
+
Time.now.strftime('%Y-%m-%d %H:%M:%S.%L')
|
70
|
+
end
|
71
|
+
|
72
|
+
def thread_name
|
73
|
+
if Thread.current == Thread.main
|
74
|
+
Process.pid
|
75
|
+
else
|
76
|
+
"#{Process.pid}##{Thread.current.object_id}"
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
class StandardFormatter
|
83
|
+
|
84
|
+
def format(context, msg)
|
85
|
+
"#{context} - #{msg.call}"
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
class Configuration
|
91
|
+
|
92
|
+
attr_reader :levels
|
93
|
+
|
94
|
+
attr_reader :appenders
|
95
|
+
|
96
|
+
def initialize
|
97
|
+
@levels = {}
|
98
|
+
@levels[nil] = :info
|
99
|
+
@appenders = []
|
100
|
+
yield self if block_given?
|
101
|
+
end
|
102
|
+
|
103
|
+
def level(level, context = nil)
|
104
|
+
context = context.to_s unless context.nil?
|
105
|
+
@levels[context] = level
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
109
|
+
|
110
|
+
class Logger
|
111
|
+
|
112
|
+
def initialize(host, appenders)
|
113
|
+
@context = context host
|
114
|
+
@appenders = appenders
|
115
|
+
end
|
116
|
+
|
117
|
+
[:trace, :debug, :info, :warn, :error, :fatal].reverse.each do |level|
|
118
|
+
define_method level do |*args, &block|
|
119
|
+
msg = args[0]
|
120
|
+
block = Proc.new { msg } unless msg.nil? or block
|
121
|
+
return if block.nil?
|
122
|
+
add level, block
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
private
|
127
|
+
|
128
|
+
def add(level, msg)
|
129
|
+
@appenders.each { |appender| appender.add(level, @context, msg) }
|
130
|
+
end
|
131
|
+
|
132
|
+
def context(host)
|
133
|
+
if host.inspect == 'main'
|
134
|
+
'main'
|
135
|
+
elsif host.class == Module
|
136
|
+
host
|
137
|
+
else
|
138
|
+
host.class
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
end
|
143
|
+
|
144
|
+
end
|
145
|
+
|
data/test/experiment.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require_relative '../lib/hatchet'
|
2
|
+
require 'logger'
|
3
|
+
|
4
|
+
module Namespace
|
5
|
+
class Foo
|
6
|
+
include Hatchet
|
7
|
+
|
8
|
+
def work
|
9
|
+
log.fatal { "Fatal message will be shown" }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module Something
|
14
|
+
extend Hatchet
|
15
|
+
|
16
|
+
def self.work
|
17
|
+
log.info { "Info message will be shown" }
|
18
|
+
logger.debug { "Debug message won't be shown" }
|
19
|
+
end
|
20
|
+
|
21
|
+
class Nested
|
22
|
+
include Hatchet
|
23
|
+
|
24
|
+
def work
|
25
|
+
log.debug { "Debug message will be shown due to override" }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
Hatchet.configure do |config|
|
32
|
+
config.level :info
|
33
|
+
config.level :debug, Namespace::Something::Nested
|
34
|
+
|
35
|
+
config.appenders << Hatchet::LoggerAppender.new do |appender|
|
36
|
+
appender.logger = Logger.new('log/test.log')
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
include Hatchet
|
41
|
+
|
42
|
+
log.warn 'Warn message will be shown'
|
43
|
+
thread = Thread.new { Namespace::Foo.new.work }
|
44
|
+
Namespace::Something.work
|
45
|
+
Namespace::Something::Nested.new.work
|
46
|
+
|
47
|
+
thread.join
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hatchet
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Garry Shutler
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-07 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Ruby logging library that provides the ability to add class/module specific
|
15
|
+
filters
|
16
|
+
email:
|
17
|
+
- garry@robustsoftware.co.uk
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- lib/hatchet/version.rb
|
23
|
+
- lib/hatchet.rb
|
24
|
+
- test/experiment.rb
|
25
|
+
- LICENSE
|
26
|
+
homepage: https://github.com/gshutler/hatchet
|
27
|
+
licenses: []
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
segments:
|
39
|
+
- 0
|
40
|
+
hash: 2996924827001664983
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
segments:
|
48
|
+
- 0
|
49
|
+
hash: 2996924827001664983
|
50
|
+
requirements: []
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.8.24
|
53
|
+
signing_key:
|
54
|
+
specification_version: 3
|
55
|
+
summary: Ruby logging library that provides the ability to add class/module specific
|
56
|
+
filters
|
57
|
+
test_files:
|
58
|
+
- test/experiment.rb
|