This is an automated email from the ASF dual-hosted git repository. pkarwasz pushed a commit to branch 2.x in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit b6673e79053649a85d68111b5ec2a5bbea1484a1 Author: Piotr P. Karwasz <[email protected]> AuthorDate: Mon Nov 20 21:13:31 2023 +0100 Use parameterized logging everywhere Our code should be an example to users: we should never use string concatenation in log messages. This was performed using OpenRewrite and is related to #1706. --- .../java/org/apache/logging/log4j/LogManager.java | 28 +++++++++++----------- .../logging/log4j/message/LocalizedMessage.java | 4 ++-- .../log4j/message/MessageFormatMessage.java | 2 +- .../log4j/message/StringFormattedMessage.java | 2 +- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/LogManager.java b/log4j-api/src/main/java/org/apache/logging/log4j/LogManager.java index 7f155a2e5a..02e03235d9 100644 --- a/log4j-api/src/main/java/org/apache/logging/log4j/LogManager.java +++ b/log4j-api/src/main/java/org/apache/logging/log4j/LogManager.java @@ -103,8 +103,8 @@ public class LogManager { } if (factories.isEmpty()) { - LOGGER.error("Log4j2 could not find a logging implementation. " - + "Please add log4j-core to the classpath. Using SimpleLogger to log to the console..."); + LOGGER.error( + "Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console..."); factory = SimpleLoggerContextFactory.INSTANCE; } else if (factories.size() == 1) { factory = factories.get(factories.lastKey()); @@ -120,8 +120,8 @@ public class LogManager { LOGGER.warn(sb.toString()); } } else { - LOGGER.error("Log4j2 could not find a logging implementation. " - + "Please add log4j-core to the classpath. Using SimpleLogger to log to the console..."); + LOGGER.error( + "Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console..."); factory = SimpleLoggerContextFactory.INSTANCE; } } @@ -157,7 +157,7 @@ public class LogManager { try { return factory.getContext(FQCN, null, null, true); } catch (final IllegalStateException ex) { - LOGGER.warn(ex.getMessage() + " Using SimpleLogger"); + LOGGER.warn("{} Using SimpleLogger", ex.getMessage()); return SimpleLoggerContextFactory.INSTANCE.getContext(FQCN, null, null, true); } } @@ -176,7 +176,7 @@ public class LogManager { try { return factory.getContext(FQCN, null, null, currentContext, null, null); } catch (final IllegalStateException ex) { - LOGGER.warn(ex.getMessage() + " Using SimpleLogger"); + LOGGER.warn("{} Using SimpleLogger", ex.getMessage()); return SimpleLoggerContextFactory.INSTANCE.getContext(FQCN, null, null, currentContext, null, null); } } @@ -196,7 +196,7 @@ public class LogManager { try { return factory.getContext(FQCN, loader, null, currentContext); } catch (final IllegalStateException ex) { - LOGGER.warn(ex.getMessage() + " Using SimpleLogger"); + LOGGER.warn("{} Using SimpleLogger", ex.getMessage()); return SimpleLoggerContextFactory.INSTANCE.getContext(FQCN, loader, null, currentContext); } } @@ -218,7 +218,7 @@ public class LogManager { try { return factory.getContext(FQCN, loader, externalContext, currentContext); } catch (final IllegalStateException ex) { - LOGGER.warn(ex.getMessage() + " Using SimpleLogger"); + LOGGER.warn("{} Using SimpleLogger", ex.getMessage()); return SimpleLoggerContextFactory.INSTANCE.getContext(FQCN, loader, externalContext, currentContext); } } @@ -240,7 +240,7 @@ public class LogManager { try { return factory.getContext(FQCN, loader, null, currentContext, configLocation, null); } catch (final IllegalStateException ex) { - LOGGER.warn(ex.getMessage() + " Using SimpleLogger"); + LOGGER.warn("{} Using SimpleLogger", ex.getMessage()); return SimpleLoggerContextFactory.INSTANCE.getContext( FQCN, loader, null, currentContext, configLocation, null); } @@ -267,7 +267,7 @@ public class LogManager { try { return factory.getContext(FQCN, loader, externalContext, currentContext, configLocation, null); } catch (final IllegalStateException ex) { - LOGGER.warn(ex.getMessage() + " Using SimpleLogger"); + LOGGER.warn("{} Using SimpleLogger", ex.getMessage()); return SimpleLoggerContextFactory.INSTANCE.getContext( FQCN, loader, externalContext, currentContext, configLocation, null); } @@ -296,7 +296,7 @@ public class LogManager { try { return factory.getContext(FQCN, loader, externalContext, currentContext, configLocation, name); } catch (final IllegalStateException ex) { - LOGGER.warn(ex.getMessage() + " Using SimpleLogger"); + LOGGER.warn("{} Using SimpleLogger", ex.getMessage()); return SimpleLoggerContextFactory.INSTANCE.getContext( FQCN, loader, externalContext, currentContext, configLocation, name); } @@ -316,7 +316,7 @@ public class LogManager { try { return factory.getContext(fqcn, null, null, currentContext); } catch (final IllegalStateException ex) { - LOGGER.warn(ex.getMessage() + " Using SimpleLogger"); + LOGGER.warn("{} Using SimpleLogger", ex.getMessage()); return SimpleLoggerContextFactory.INSTANCE.getContext(fqcn, null, null, currentContext); } } @@ -338,7 +338,7 @@ public class LogManager { try { return factory.getContext(fqcn, loader, null, currentContext); } catch (final IllegalStateException ex) { - LOGGER.warn(ex.getMessage() + " Using SimpleLogger"); + LOGGER.warn("{} Using SimpleLogger", ex.getMessage()); return SimpleLoggerContextFactory.INSTANCE.getContext(fqcn, loader, null, currentContext); } } @@ -366,7 +366,7 @@ public class LogManager { try { return factory.getContext(fqcn, loader, null, currentContext, configLocation, name); } catch (final IllegalStateException ex) { - LOGGER.warn(ex.getMessage() + " Using SimpleLogger"); + LOGGER.warn("{} Using SimpleLogger", ex.getMessage()); return SimpleLoggerContextFactory.INSTANCE.getContext(fqcn, loader, null, currentContext); } } diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/message/LocalizedMessage.java b/log4j-api/src/main/java/org/apache/logging/log4j/message/LocalizedMessage.java index 172164c41b..c3152224d9 100644 --- a/log4j-api/src/main/java/org/apache/logging/log4j/message/LocalizedMessage.java +++ b/log4j-api/src/main/java/org/apache/logging/log4j/message/LocalizedMessage.java @@ -239,7 +239,7 @@ public class LocalizedMessage implements Message, LoggerNameAwareMessage { } } catch (final MissingResourceException ex) { if (!loop) { - logger.debug("Unable to locate ResourceBundle " + rbBaseName); + logger.debug("Unable to locate ResourceBundle {}", rbBaseName); return null; } } @@ -255,7 +255,7 @@ public class LocalizedMessage implements Message, LoggerNameAwareMessage { rb = ResourceBundle.getBundle(substr); } } catch (final MissingResourceException ex) { - logger.debug("Unable to locate ResourceBundle " + substr); + logger.debug("Unable to locate ResourceBundle {}", substr); } } return rb; diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/message/MessageFormatMessage.java b/log4j-api/src/main/java/org/apache/logging/log4j/message/MessageFormatMessage.java index af7ee8e68f..609bf77f4e 100644 --- a/log4j-api/src/main/java/org/apache/logging/log4j/message/MessageFormatMessage.java +++ b/log4j-api/src/main/java/org/apache/logging/log4j/message/MessageFormatMessage.java @@ -114,7 +114,7 @@ public class MessageFormatMessage implements Message { final MessageFormat temp = new MessageFormat(msgPattern, locale); return temp.format(args); } catch (final IllegalFormatException ife) { - LOGGER.error("Unable to format msg: " + msgPattern, ife); + LOGGER.error("Unable to format msg: {}", msgPattern, ife); return msgPattern; } } diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/message/StringFormattedMessage.java b/log4j-api/src/main/java/org/apache/logging/log4j/message/StringFormattedMessage.java index 05006250af..d1eba763d1 100644 --- a/log4j-api/src/main/java/org/apache/logging/log4j/message/StringFormattedMessage.java +++ b/log4j-api/src/main/java/org/apache/logging/log4j/message/StringFormattedMessage.java @@ -120,7 +120,7 @@ public class StringFormattedMessage implements Message { try { return String.format(locale, msgPattern, args); } catch (final IllegalFormatException ife) { - LOGGER.error("Unable to format msg: " + msgPattern, ife); + LOGGER.error("Unable to format msg: {}", msgPattern, ife); return msgPattern; } }
