Permalink
Browse files

Ability to add thread context in SimpleFormatter

Changed the SimpleFormatter to be able to include the thread context if its
thread_context attribute is set to true.

This was added so that it would be possible to differentiate between forked
processes when using a wrapper like Foreman that includes timestamps and
high-level worker information already.
  • Loading branch information...
1 parent cdcf699 commit eb832e33f759a20c95419554ba7f0f73afa00c74 @gshutler committed Nov 11, 2012
View
@@ -4,6 +4,7 @@
require_relative 'hatchet/level_manager'
require_relative 'hatchet/backtrace_formatter'
+require_relative 'hatchet/thread_name_formatter'
require_relative 'hatchet/configuration'
require_relative 'hatchet/delegating_formatter'
require_relative 'hatchet/hatchet_logger'
@@ -7,6 +7,12 @@ module Hatchet
#
class SimpleFormatter
include BacktraceFormatter
+ include ThreadNameFormatter
+
+ # Public: Gets or sets whether the context of the thread (pid and thread ID)
+ # should be included into the output messages.
+ #
+ attr_accessor :thread_context
# Public: Returns the formatted message.
#
@@ -16,14 +22,21 @@ class SimpleFormatter
#
# Returns messages in the format:
#
- # LEVEL - CONTEXT - MESSAGE
+ # [THREAD] - LEVEL - CONTEXT - MESSAGE
# BACKTRACE
#
- # The backtrace is only present if the message contains an error.
+ # The backtrace is only present if the message contains an error and the
+ # presence of the context of the thread context is managed via the
+ # #thread_context attribute.
#
def format(level, context, message)
msg = message.to_s.strip
- msg = "#{level.to_s.upcase} - #{context} - #{msg}"
+
+ if thread_context
+ msg = "[#{thread_name}] - #{level.to_s.upcase} - #{context} - #{msg}"
+ else
+ msg = "#{level.to_s.upcase} - #{context} - #{msg}"
+ end
with_backtrace(message, msg)
end
@@ -6,6 +6,7 @@ module Hatchet
#
class StandardFormatter
include BacktraceFormatter
+ include ThreadNameFormatter
# Public: Creates a new instance.
#
@@ -29,6 +30,7 @@ def initialize
def format(level, context, message)
msg = message.to_s.strip
msg = "#{timestamp} [#{thread_name}] #{format_level level} #{context} - #{msg}"
+
with_backtrace(message, msg)
end
@@ -53,17 +55,6 @@ def timestamp
@last = @date + "00#{millis}"[-3..-1]
end
- # Private: Returns the name of the current thread from the processes pid and
- # the threads object_id when it is not the main thread for the process.
- #
- def thread_name
- if Thread.current == Thread.main
- Process.pid
- else
- "#{Process.pid}##{Thread.current.object_id}"
- end
- end
-
# Private: Returns the level formatted for log output as a String.
#
def format_level(level)
@@ -0,0 +1,25 @@
+# -*- encoding: utf-8 -*-
+
+module Hatchet
+
+ # Internal: Module for standardizing the formatting of thread contexts.
+ #
+ module ThreadNameFormatter
+
+ private
+
+ # Private: Returns the name of the current thread from the processes pid and
+ # the threads object_id when it is not the main thread for the process.
+ #
+ def thread_name
+ if Thread.current == Thread.main
+ Process.pid
+ else
+ "#{Process.pid}##{Thread.current.object_id}"
+ end
+ end
+
+ end
+
+end
+
View
@@ -4,6 +4,6 @@ module Hatchet
# Public: The version of Hatchet.
#
- VERSION = '0.0.19'
+ VERSION = '0.0.20'
end
@@ -52,6 +52,22 @@
end
end
+
+ describe 'with thread context' do
+
+ before do
+ subject.thread_context = true
+ @message = Message.new(' Hello, World ')
+ end
+
+ it 'outputs the message in the [THREAD] - LEVEL - CONTEXT - MESSAGE format' do
+ message = subject.format(:info, 'Custom::Context', @message)
+ assert "[#{Process.pid}] - INFO - Custom::Context - Hello, World" == message, "got #{message}"
+ end
+
+ end
+
end
+
end

0 comments on commit eb832e3

Please sign in to comment.