Skip to content

Commit

Permalink
Use parameterized logging everywhere
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ppkarwasz committed Nov 21, 2023
1 parent 72152f7 commit b6673e7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
28 changes: 14 additions & 14 deletions log4j-api/src/main/java/org/apache/logging/log4j/LogManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -157,7 +157,7 @@ public static LoggerContext getContext() {
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);
}
}
Expand All @@ -176,7 +176,7 @@ public static LoggerContext getContext(final boolean currentContext) {
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);
}
}
Expand All @@ -196,7 +196,7 @@ public static LoggerContext getContext(final ClassLoader loader, final boolean c
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);
}
}
Expand All @@ -218,7 +218,7 @@ public static LoggerContext getContext(
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);
}
}
Expand All @@ -240,7 +240,7 @@ public static LoggerContext getContext(
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);
}
Expand All @@ -267,7 +267,7 @@ public static LoggerContext getContext(
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);
}
Expand Down Expand Up @@ -296,7 +296,7 @@ public static LoggerContext getContext(
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);
}
Expand All @@ -316,7 +316,7 @@ protected static LoggerContext getContext(final String fqcn, final boolean curre
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);
}
}
Expand All @@ -338,7 +338,7 @@ protected static LoggerContext getContext(
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);
}
}
Expand Down Expand Up @@ -366,7 +366,7 @@ protected static LoggerContext getContext(
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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ protected ResourceBundle getResourceBundle(
}
} catch (final MissingResourceException ex) {
if (!loop) {
logger.debug("Unable to locate ResourceBundle " + rbBaseName);
logger.debug("Unable to locate ResourceBundle {}", rbBaseName);
return null;
}
}
Expand All @@ -255,7 +255,7 @@ protected ResourceBundle getResourceBundle(
rb = ResourceBundle.getBundle(substr);
}
} catch (final MissingResourceException ex) {
logger.debug("Unable to locate ResourceBundle " + substr);
logger.debug("Unable to locate ResourceBundle {}", substr);
}
}
return rb;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ protected String formatMessage(final String msgPattern, final Object... args) {
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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ protected String formatMessage(final String msgPattern, final Object... args) {
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;
}
}
Expand Down

0 comments on commit b6673e7

Please sign in to comment.