From 9af91033952c756d0cbe44fafc00d399cf0226e9 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Thu, 5 May 2022 21:24:00 +0900 Subject: [PATCH 1/2] Prefer Kernel#sprintf over String#% for formatting Strings String#% takes the arguments as an Array object, which requires us to create an extra object. --- lib/logger/formatter.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/logger/formatter.rb b/lib/logger/formatter.rb index 34e07bc..e60f5cf 100644 --- a/lib/logger/formatter.rb +++ b/lib/logger/formatter.rb @@ -13,8 +13,7 @@ def initialize end def call(severity, time, progname, msg) - Format % [severity[0, 1], format_datetime(time), Process.pid, severity, progname, - msg2str(msg)] + sprintf(Format, severity[0, 1], format_datetime(time), Process.pid, severity, progname, msg2str(msg)) end private From bd2865694a46832d8e74b4d0519f680cfc5156e8 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Fri, 6 May 2022 08:30:00 +0900 Subject: [PATCH 2/2] Cut off one more String allocation Co-authored-by: Nobuyoshi Nakada --- lib/logger/formatter.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/logger/formatter.rb b/lib/logger/formatter.rb index e60f5cf..c634dbf 100644 --- a/lib/logger/formatter.rb +++ b/lib/logger/formatter.rb @@ -3,7 +3,7 @@ class Logger # Default formatter for log messages. class Formatter - Format = "%s, [%s #%d] %5s -- %s: %s\n" + Format = "%.1s, [%s #%d] %5s -- %s: %s\n" DatetimeFormat = "%Y-%m-%dT%H:%M:%S.%6N" attr_accessor :datetime_format @@ -13,7 +13,7 @@ def initialize end def call(severity, time, progname, msg) - sprintf(Format, severity[0, 1], format_datetime(time), Process.pid, severity, progname, msg2str(msg)) + sprintf(Format, severity, format_datetime(time), Process.pid, severity, progname, msg2str(msg)) end private