hatchet 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -20,9 +20,9 @@ require_relative 'hatchet/version'
20
20
  #
21
21
  module Hatchet
22
22
 
23
- # Public: Returns a Logger for the object.
23
+ # Public: Returns a HatchetLogger for the object.
24
24
  #
25
- # The returned logger has 5 methods. Those are, in decreasing order of
25
+ # The logger has 5 logging methods. Those are, in decreasing order of
26
26
  # severity:
27
27
  #
28
28
  # * fatal
@@ -31,8 +31,8 @@ module Hatchet
31
31
  # * info
32
32
  # * debug
33
33
  #
34
- # All the methods have the same signature. You can either provide a message as
35
- # a direct string, or as a block to the method is lazily evaluated (this is
34
+ # All these methods have the same signature. You can either provide a message
35
+ # as a direct string, or as a block to the method is lazily evaluated (this is
36
36
  # the recommended option).
37
37
  #
38
38
  # Examples
@@ -40,18 +40,31 @@ module Hatchet
40
40
  # logger.info "Informational message"
41
41
  # logger.info { "Informational message #{potentially_expensive}" }
42
42
  #
43
- # Log messages are sent to appender where they will be filtered and invoked as
44
- # configured.
43
+ # Log messages are sent to each appender where they will be filtered and
44
+ # invoked as configured.
45
45
  #
46
- # Returns a Logger for the object.
46
+ # The logger also has 5 inspection methods. Those are, in decreasing order of
47
+ # severity:
48
+ #
49
+ # * fatal?
50
+ # * error?
51
+ # * warn?
52
+ # * info?
53
+ # * debug?
54
+ #
55
+ # All these methods take no arguments and return true if any of the loggers'
56
+ # appenders will log a message at that level for the current context,
57
+ # otherwise they will return false.
58
+ #
59
+ # Returns a HatchetLogger for the object.
47
60
  #
48
61
  def logger
49
62
  @_hatchet_logger ||= HatchetLogger.new self, Hatchet.appenders
50
63
  end
51
64
 
52
- # Public: Returns a logger for the object.
65
+ # Public: Returns a HatchetLogger for the object.
53
66
  #
54
- # The returned logger has 5 methods. Those are, in decreasing order of
67
+ # The logger has 5 logging methods. Those are, in decreasing order of
55
68
  # severity:
56
69
  #
57
70
  # * fatal
@@ -60,8 +73,8 @@ module Hatchet
60
73
  # * info
61
74
  # * debug
62
75
  #
63
- # All the methods have the same signature. You can either provide a message as
64
- # a direct string, or as a block to the method is lazily evaluated (this is
76
+ # All these methods have the same signature. You can either provide a message
77
+ # as a direct string, or as a block to the method is lazily evaluated (this is
65
78
  # the recommended option).
66
79
  #
67
80
  # Examples
@@ -69,10 +82,23 @@ module Hatchet
69
82
  # log.info "Informational message"
70
83
  # log.info { "Informational message #{potentially_expensive}" }
71
84
  #
72
- # Log messages are sent to appender where they will be filtered and invoked as
73
- # configured.
85
+ # Log messages are sent to each appender where they will be filtered and
86
+ # invoked as configured.
87
+ #
88
+ # The logger also has 5 inspection methods. Those are, in decreasing order of
89
+ # severity:
90
+ #
91
+ # * fatal?
92
+ # * error?
93
+ # * warn?
94
+ # * info?
95
+ # * debug?
96
+ #
97
+ # All these methods take no arguments and return true if any of the loggers'
98
+ # appenders will log a message at that level for the current context,
99
+ # otherwise they will return false.
74
100
  #
75
- # Returns a Logger for the object.
101
+ # Returns a HatchetLogger for the object.
76
102
  #
77
103
  alias_method :log, :logger
78
104
 
@@ -106,8 +132,9 @@ module Hatchet
106
132
  def self.configure
107
133
  @@config = Configuration.new
108
134
  yield @@config
135
+ default_formatter = StandardFormatter.new
109
136
  @@config.appenders.each do |appender|
110
- appender.formatter ||= StandardFormatter.new
137
+ appender.formatter ||= default_formatter
111
138
  appender.levels = @@config.levels if appender.levels.empty?
112
139
  end
113
140
  end
@@ -2,10 +2,11 @@
2
2
 
3
3
  module Hatchet
4
4
 
5
- # Internal: Class that handles logging calls and distributes them to all its
5
+ # Public: Class that handles logging calls and distributes them to all its
6
6
  # appenders.
7
7
  #
8
- # Each logger has 5 methods. Those are, in decreasing order of severity:
8
+ # Each logger has 5 logging methods. Those are, in decreasing order of
9
+ # severity:
9
10
  #
10
11
  # * fatal
11
12
  # * error
@@ -13,8 +14,8 @@ module Hatchet
13
14
  # * info
14
15
  # * debug
15
16
  #
16
- # All the methods have the same signature. You can either provide a message as
17
- # a direct string, or as a block to the method is lazily evaluated (this is
17
+ # All these methods have the same signature. You can either provide a message
18
+ # as a direct string, or as a block to the method is lazily evaluated (this is
18
19
  # the recommended option).
19
20
  #
20
21
  # Examples
@@ -25,6 +26,19 @@ module Hatchet
25
26
  # Log messages are sent to each appender where they will be filtered and
26
27
  # invoked as configured.
27
28
  #
29
+ # Each logger also has 5 inspection methods. Those are, in decreasing order of
30
+ # severity:
31
+ #
32
+ # * fatal?
33
+ # * error?
34
+ # * warn?
35
+ # * info?
36
+ # * debug?
37
+ #
38
+ # All these methods take no arguments and return true if any of the loggers'
39
+ # appenders will log a message at that level for the current context,
40
+ # otherwise they will return false.
41
+ #
28
42
  class HatchetLogger
29
43
 
30
44
  # Internal: Creates a new logger.
@@ -45,7 +45,7 @@ module Hatchet
45
45
  yield self if block_given?
46
46
 
47
47
  @logger.level = ::Logger::DEBUG
48
- return unless @logger.instance_of? Logger
48
+ return unless @logger.respond_to? :formatter
49
49
 
50
50
  # Wipe the format of the core Logger. The Rails.logger doesn't have this
51
51
  # method for example.
@@ -6,6 +6,10 @@ module Hatchet
6
6
  #
7
7
  class StandardFormatter
8
8
 
9
+ def initialize
10
+ @secs = 0
11
+ end
12
+
9
13
  # Public: Returns the formatted message.
10
14
  #
11
15
  # level - The severity of the log message.
@@ -15,6 +19,7 @@ module Hatchet
15
19
  # Returns the context and message separated by a hypen.
16
20
  #
17
21
  def format(level, context, message)
22
+ message = message.to_s.strip
18
23
  "#{timestamp} [#{thread_name}] #{format_level level} #{context} - #{message}"
19
24
  end
20
25
 
@@ -23,7 +28,20 @@ module Hatchet
23
28
  # Private: Returns the current time as a String.
24
29
  #
25
30
  def timestamp
26
- Time.now.strftime('%Y-%m-%d %H:%M:%S.%L')
31
+ time = Time.now
32
+
33
+ secs = time.to_i
34
+ millis = time.nsec/1000000
35
+
36
+ return @last if @millis == millis && @secs == secs
37
+
38
+ unless secs == @secs
39
+ @secs = secs
40
+ @date = time.strftime('%Y-%m-%d %H:%M:%S.')
41
+ end
42
+
43
+ @millis = millis
44
+ @last = @date + "00#{millis}"[1..3]
27
45
  end
28
46
 
29
47
  # Private: Returns the name of the current thread from the processes pid and
@@ -4,6 +4,6 @@ module Hatchet
4
4
 
5
5
  # Public: The version of Hatchet.
6
6
  #
7
- VERSION = "0.0.5"
7
+ VERSION = '0.0.6'
8
8
 
9
9
  end
@@ -54,6 +54,20 @@ describe StandardFormatter do
54
54
  end
55
55
  end
56
56
 
57
+ describe 'benchmarks' do
58
+ it 'is reasonably quick' do
59
+ start = Time.now
60
+
61
+ 50_000.times do
62
+ subject.format(:info, @context, @message)
63
+ end
64
+
65
+ took = Time.now - start
66
+ limit = 0.4
67
+ assert took < limit, "Expected messages to take less than #{limit} but took #{took}"
68
+ end
69
+ end
70
+
57
71
  def format_as_string(time)
58
72
  time.strftime TIME_FORMAT
59
73
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hatchet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-08 00:00:00.000000000 Z
12
+ date: 2012-07-10 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Logging library that provides the ability to add class/module specific
15
15
  filters
@@ -58,7 +58,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
58
58
  version: '0'
59
59
  requirements: []
60
60
  rubyforge_project:
61
- rubygems_version: 1.8.17
61
+ rubygems_version: 1.8.24
62
62
  signing_key:
63
63
  specification_version: 3
64
64
  summary: Logging library that provides the ability to add class/module specific filters