hatchet 0.0.19 → 0.0.20

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/RELEASE.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Release notes
2
2
 
3
+ ## 0.0.20
4
+
5
+ * Added a `#thread_context` attribute to the `SimpleFormatter` that is `false`
6
+ by default, but when set to `true` will output the context of the thread
7
+ within messages in the same style as the `StandardFormatter`
8
+
3
9
  ## 0.0.19
4
10
 
5
11
  * Changed core formatters to output an indented backtrace after a message when
@@ -0,0 +1,52 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module Hatchet
4
+
5
+ # Internal: Module for handling the common code for conditionally including
6
+ # the backtrace within a formatted message.
7
+ #
8
+ module BacktraceFormatter
9
+
10
+ # Public: Gets whether backtraces are enabled.
11
+ #
12
+ # Defaults to true if it has not been set.
13
+ #
14
+ def backtrace
15
+ @backtrace.nil? ? true : @backtrace
16
+ end
17
+
18
+ # Public: Sets whether backtraces should be enabled.
19
+ #
20
+ # value - True if backtraces should be enabled, otherwise false.
21
+ #
22
+ def backtrace=(value)
23
+ @backtrace = value
24
+ end
25
+
26
+ private
27
+
28
+ # Private: Method that takes an already formatted message and appends the
29
+ # backtrace to it if there is one and backtraces are enabled.
30
+ #
31
+ # message - The Message that is being formatted.
32
+ # message_without_backtrace - The formatted message before a backtrace may
33
+ # be added.
34
+ #
35
+ # Returns a message with a backtrace optionally appended to it.
36
+ #
37
+ def with_backtrace(message, message_without_backtrace)
38
+ msg = message_without_backtrace
39
+ error = message.error
40
+
41
+ if self.backtrace && error && error.respond_to?(:backtrace)
42
+ indented_backtrace = error.backtrace.map { |line| " #{line}" }.to_a
43
+ msg = ([msg] + indented_backtrace).join("\n")
44
+ end
45
+
46
+ msg
47
+ end
48
+
49
+ end
50
+
51
+ end
52
+
@@ -8,17 +8,7 @@ module Hatchet
8
8
  # be human readable rather than useful for grepping, etc.
9
9
  #
10
10
  class PlainFormatter
11
-
12
- # Public: Gets or sets whether backtraces should be output when messages
13
- # contain an error with one.
14
- #
15
- attr_accessor :backtrace
16
-
17
- # Public: Creates a new instance.
18
- #
19
- def initialize
20
- self.backtrace = true
21
- end
11
+ include BacktraceFormatter
22
12
 
23
13
  # Public: Returns the formatted message.
24
14
  #
@@ -34,12 +24,7 @@ module Hatchet
34
24
  # The backtrace is only present if the message contains an error.
35
25
  #
36
26
  def format(level, context, message)
37
- unless self.backtrace && message.error && message.error.respond_to?(:backtrace)
38
- message.to_s
39
- else
40
- indented_backtrace = message.error.backtrace.map { |line| " #{line}" }.to_a
41
- ([message.to_s] + indented_backtrace).join "\n"
42
- end
27
+ with_backtrace(message, message.to_s)
43
28
  end
44
29
 
45
30
  end
@@ -6,17 +6,13 @@ module Hatchet
6
6
  # and message.
7
7
  #
8
8
  class SimpleFormatter
9
+ include BacktraceFormatter
10
+ include ThreadNameFormatter
9
11
 
10
- # Public: Gets or sets whether backtraces should be output when messages
11
- # contain an error with one.
12
+ # Public: Gets or sets whether the context of the thread (pid and thread ID)
13
+ # should be included into the output messages.
12
14
  #
13
- attr_accessor :backtrace
14
-
15
- # Public: Creates a new instance.
16
- #
17
- def initialize
18
- self.backtrace = true
19
- end
15
+ attr_accessor :thread_context
20
16
 
21
17
  # Public: Returns the formatted message.
22
18
  #
@@ -26,18 +22,23 @@ module Hatchet
26
22
  #
27
23
  # Returns messages in the format:
28
24
  #
29
- # LEVEL - CONTEXT - MESSAGE
25
+ # [THREAD] - LEVEL - CONTEXT - MESSAGE
30
26
  # BACKTRACE
31
27
  #
32
- # The backtrace is only present if the message contains an error.
28
+ # The backtrace is only present if the message contains an error and the
29
+ # presence of the context of the thread context is managed via the
30
+ # #thread_context attribute.
33
31
  #
34
32
  def format(level, context, message)
35
33
  msg = message.to_s.strip
36
- if self.backtrace && message.error && message.error.respond_to?(:backtrace)
37
- indented_backtrace = message.error.backtrace.map { |line| " #{line}" }.to_a
38
- msg = ([msg] + indented_backtrace).join("\n")
34
+
35
+ if thread_context
36
+ msg = "[#{thread_name}] - #{level.to_s.upcase} - #{context} - #{msg}"
37
+ else
38
+ msg = "#{level.to_s.upcase} - #{context} - #{msg}"
39
39
  end
40
- "#{level.to_s.upcase} - #{context} - #{msg}"
40
+
41
+ with_backtrace(message, msg)
41
42
  end
42
43
 
43
44
  end
@@ -5,17 +5,13 @@ module Hatchet
5
5
  # Public: Standard formatter class. Outputs messages in the TTCC of log4j.
6
6
  #
7
7
  class StandardFormatter
8
-
9
- # Public: Gets or sets whether backtraces should be output when messages
10
- # contain an error with one.
11
- #
12
- attr_accessor :backtrace
8
+ include BacktraceFormatter
9
+ include ThreadNameFormatter
13
10
 
14
11
  # Public: Creates a new instance.
15
12
  #
16
13
  def initialize
17
14
  @secs = 0
18
- self.backtrace = true
19
15
  end
20
16
 
21
17
  # Public: Returns the formatted message.
@@ -33,11 +29,9 @@ module Hatchet
33
29
  #
34
30
  def format(level, context, message)
35
31
  msg = message.to_s.strip
36
- if self.backtrace && message.error && message.error.respond_to?(:backtrace)
37
- indented_backtrace = message.error.backtrace.map { |line| " #{line}" }.to_a
38
- msg = ([msg] + indented_backtrace).join("\n")
39
- end
40
- "#{timestamp} [#{thread_name}] #{format_level level} #{context} - #{msg}"
32
+ msg = "#{timestamp} [#{thread_name}] #{format_level level} #{context} - #{msg}"
33
+
34
+ with_backtrace(message, msg)
41
35
  end
42
36
 
43
37
  private
@@ -61,17 +55,6 @@ module Hatchet
61
55
  @last = @date + "00#{millis}"[-3..-1]
62
56
  end
63
57
 
64
- # Private: Returns the name of the current thread from the processes pid and
65
- # the threads object_id when it is not the main thread for the process.
66
- #
67
- def thread_name
68
- if Thread.current == Thread.main
69
- Process.pid
70
- else
71
- "#{Process.pid}##{Thread.current.object_id}"
72
- end
73
- end
74
-
75
58
  # Private: Returns the level formatted for log output as a String.
76
59
  #
77
60
  def format_level(level)
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module Hatchet
4
+
5
+ # Internal: Module for standardizing the formatting of thread contexts.
6
+ #
7
+ module ThreadNameFormatter
8
+
9
+ private
10
+
11
+ # Private: Returns the name of the current thread from the processes pid and
12
+ # the threads object_id when it is not the main thread for the process.
13
+ #
14
+ def thread_name
15
+ if Thread.current == Thread.main
16
+ Process.pid
17
+ else
18
+ "#{Process.pid}##{Thread.current.object_id}"
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ end
25
+
@@ -4,6 +4,6 @@ module Hatchet
4
4
 
5
5
  # Public: The version of Hatchet.
6
6
  #
7
- VERSION = '0.0.19'
7
+ VERSION = '0.0.20'
8
8
 
9
9
  end
data/lib/hatchet.rb CHANGED
@@ -3,6 +3,8 @@
3
3
  require 'logger'
4
4
 
5
5
  require_relative 'hatchet/level_manager'
6
+ require_relative 'hatchet/backtrace_formatter'
7
+ require_relative 'hatchet/thread_name_formatter'
6
8
  require_relative 'hatchet/configuration'
7
9
  require_relative 'hatchet/delegating_formatter'
8
10
  require_relative 'hatchet/hatchet_logger'
@@ -52,6 +52,22 @@ describe SimpleFormatter do
52
52
  end
53
53
 
54
54
  end
55
+
56
+ describe 'with thread context' do
57
+
58
+ before do
59
+ subject.thread_context = true
60
+ @message = Message.new(' Hello, World ')
61
+ end
62
+
63
+ it 'outputs the message in the [THREAD] - LEVEL - CONTEXT - MESSAGE format' do
64
+ message = subject.format(:info, 'Custom::Context', @message)
65
+ assert "[#{Process.pid}] - INFO - Custom::Context - Hello, World" == message, "got #{message}"
66
+ end
67
+
68
+ end
69
+
55
70
  end
71
+
56
72
  end
57
73
 
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.19
4
+ version: 0.0.20
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-11-07 00:00:00.000000000 Z
12
+ date: 2012-11-11 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
@@ -19,6 +19,7 @@ executables: []
19
19
  extensions: []
20
20
  extra_rdoc_files: []
21
21
  files:
22
+ - lib/hatchet/backtrace_formatter.rb
22
23
  - lib/hatchet/configuration.rb
23
24
  - lib/hatchet/delegating_formatter.rb
24
25
  - lib/hatchet/hatchet_logger.rb
@@ -29,6 +30,7 @@ files:
29
30
  - lib/hatchet/railtie.rb
30
31
  - lib/hatchet/simple_formatter.rb
31
32
  - lib/hatchet/standard_formatter.rb
33
+ - lib/hatchet/thread_name_formatter.rb
32
34
  - lib/hatchet/version.rb
33
35
  - lib/hatchet.rb
34
36
  - spec/configuration_spec.rb