From 87b49f402a7fe6b9badf729e9f2c867f50d483ac Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Fri, 23 Nov 2018 13:01:04 +0100 Subject: [PATCH] Improve reasoning of log lines in threaded processes. --- lib/core_ext/logger/formatter.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 lib/core_ext/logger/formatter.rb diff --git a/lib/core_ext/logger/formatter.rb b/lib/core_ext/logger/formatter.rb new file mode 100644 index 000000000..fdc88c5b8 --- /dev/null +++ b/lib/core_ext/logger/formatter.rb @@ -0,0 +1,20 @@ +# This customization adds the id of the current Thread to all log lines. +# It was introduced to make it more easy to follow the execution of tasks in the log in threaded processes. +# +# before: +# D, [2018-11-20T16:35:03.483547 #72102] DEBUG -- : (0.5ms) SELECT COUNT(*) FROM "delayed_jobs" +# +# after: +# D, [2018-11-20T16:35:03.483547 #72102-23423534] DEBUG -- : (0.5ms) SELECT COUNT(*) FROM "delayed_jobs" + +class Logger + class Formatter + + # original: Format = "%s, [%s#%d] %5s -- %s: %s\n".freeze + FORMAT_WITH_THREAD_ID = "%s, [%s#%d-%d] %5s -- %s: %s\n".freeze + + def call(severity, time, progname, msg) + format(FORMAT_WITH_THREAD_ID, severity[0..0], format_datetime(time), Process.pid, Thread.current.object_id, severity, progname, msg2str(msg)) + end + end +end