From b44f511765bc734e8a2a001663ef51a01850108e Mon Sep 17 00:00:00 2001 From: Sergio Bobillier Date: Tue, 27 Jul 2021 12:28:52 +0200 Subject: [PATCH] Add the R conversion character to layouts The 'R' character is very similar to the 'r' character except that it will output the number of seconds (with milliseconds) elapsed between the construction of the Layout and the creation of the log event formatted like #.#### (always 4 decimal places) --- lib/logging/layouts/pattern.rb | 5 +++++ test/layouts/test_pattern.rb | 3 +++ 2 files changed, 8 insertions(+) diff --git a/lib/logging/layouts/pattern.rb b/lib/logging/layouts/pattern.rb index 45bb39e..581c414 100644 --- a/lib/logging/layouts/pattern.rb +++ b/lib/logging/layouts/pattern.rb @@ -61,6 +61,9 @@ def self.pattern( *args ) # [p] Used to output the process ID of the currently running program. # [r] Used to output the number of milliseconds elapsed from the # construction of the Layout until creation of the log event. + # [R] Used to output the number of seconds (with milliseconds) elapsed + # between the construction of the Layout and the creation of the log + # event formatted like #.#### (always 4 decimal places). # [t] Used to output the object ID of the thread that generated the # log event. # [T] Used to output the name of the thread that generated the log event. @@ -311,6 +314,7 @@ class FormatMethodBuilder 'h' => "'#{Socket.gethostname}'".freeze, 'p' => 'Process.pid'.freeze, 'r' => 'Integer((event.time-@created_at)*1000).to_s'.freeze, + 'R' => 'Kernel.format("%.4f", event.time-@created_at)'.freeze, 't' => 'Thread.current.object_id.to_s'.freeze, 'T' => 'Thread.current[:name]'.freeze, 'X' => :placeholder, @@ -326,6 +330,7 @@ class FormatMethodBuilder 'h' => :hostname, 'p' => :pid, 'r' => :time, + 'R' => :time, 'T' => :thread, 't' => :thread_id, 'F' => :file, diff --git a/test/layouts/test_pattern.rb b/test/layouts/test_pattern.rb index 1e22cfd..439e6be 100644 --- a/test/layouts/test_pattern.rb +++ b/test/layouts/test_pattern.rb @@ -134,6 +134,9 @@ def test_pattern_all @layout.pattern = '%r' assert_match %r/\A\d+\z/, @layout.format(event) + @layout.pattern = '%R' + assert_match %r/\A\d+\.\d{4}\z/, @layout.format(event) + @layout.pattern = '%t' assert_match %r/\A-?\d+\z/, @layout.format(event)