use of org.apache.logging.log4j.core.lookup.StrSubstitutor in project geode by apache.
the class LogService method isUsingGemFireDefaultConfig.
public static boolean isUsingGemFireDefaultConfig() {
final Configuration config = ((org.apache.logging.log4j.core.Logger) LogManager.getLogger(ROOT_LOGGER_NAME, GemFireParameterizedMessageFactory.INSTANCE)).getContext().getConfiguration();
final StrSubstitutor sub = config.getStrSubstitutor();
final StrLookup resolver = sub.getVariableResolver();
final String value = resolver.lookup(GEMFIRE_DEFAULT_PROPERTY);
return "true".equals(value);
}
use of org.apache.logging.log4j.core.lookup.StrSubstitutor in project logging-log4j2 by apache.
the class Log4j1ConfigurationParser method buildConfigurationBuilder.
/**
* Parses a Log4j 1.2 properties configuration file in ISO 8859-1 encoding into a ConfigurationBuilder.
*
* @param input
* InputStream to read from is assumed to be ISO 8859-1, and will not be closed.
* @return the populated ConfigurationBuilder, never {@literal null}
* @throws IOException
* if unable to read the input
* @throws ConfigurationException
* if the input does not contain a valid configuration
*/
public ConfigurationBuilder<BuiltConfiguration> buildConfigurationBuilder(final InputStream input) throws IOException {
try {
properties.load(input);
strSubstitutorProperties = new StrSubstitutor(properties);
strSubstitutorSystem = new StrSubstitutor(System.getProperties());
final String rootCategoryValue = getLog4jValue(ROOTCATEGORY);
final String rootLoggerValue = getLog4jValue(ROOTLOGGER);
if (rootCategoryValue == null && rootLoggerValue == null) {
// This is not a Log4j 1 properties configuration file.
warn("Missing " + ROOTCATEGORY + " or " + ROOTLOGGER + " in " + input);
// throw new ConfigurationException(
// "Missing " + ROOTCATEGORY + " or " + ROOTLOGGER + " in " + input);
}
builder.setConfigurationName("Log4j1");
// DEBUG
final String debugValue = getLog4jValue("debug");
if (Boolean.valueOf(debugValue)) {
builder.setStatusLevel(Level.DEBUG);
}
// Root
buildRootLogger(getLog4jValue(ROOTCATEGORY));
buildRootLogger(getLog4jValue(ROOTLOGGER));
// Appenders
final Map<String, String> appenderNameToClassName = buildClassToPropertyPrefixMap();
for (final Map.Entry<String, String> entry : appenderNameToClassName.entrySet()) {
final String appenderName = entry.getKey();
final String appenderClass = entry.getValue();
buildAppender(appenderName, appenderClass);
}
// Loggers
buildLoggers("log4j.category.");
buildLoggers("log4j.logger.");
buildProperties();
return builder;
} catch (final IllegalArgumentException e) {
throw new ConfigurationException(e);
}
}
use of org.apache.logging.log4j.core.lookup.StrSubstitutor in project logging-log4j2 by apache.
the class GelfLayout method toText.
private StringBuilder toText(final LogEvent event, final StringBuilder builder, final boolean gcFree) {
builder.append('{');
builder.append("\"version\":\"1.1\",");
builder.append("\"host\":\"");
JsonUtils.quoteAsString(toNullSafeString(host), builder);
builder.append(QC);
builder.append("\"timestamp\":").append(formatTimestamp(event.getTimeMillis())).append(C);
builder.append("\"level\":").append(formatLevel(event.getLevel())).append(C);
if (event.getThreadName() != null) {
builder.append("\"_thread\":\"");
JsonUtils.quoteAsString(event.getThreadName(), builder);
builder.append(QC);
}
if (event.getLoggerName() != null) {
builder.append("\"_logger\":\"");
JsonUtils.quoteAsString(event.getLoggerName(), builder);
builder.append(QC);
}
if (additionalFields.length > 0) {
final StrSubstitutor strSubstitutor = getConfiguration().getStrSubstitutor();
for (final KeyValuePair additionalField : additionalFields) {
builder.append(QU);
JsonUtils.quoteAsString(additionalField.getKey(), builder);
builder.append("\":\"");
final String value = valueNeedsLookup(additionalField.getValue()) ? strSubstitutor.replace(event, additionalField.getValue()) : additionalField.getValue();
JsonUtils.quoteAsString(toNullSafeString(value), builder);
builder.append(QC);
}
}
if (includeThreadContext) {
event.getContextData().forEach(WRITE_KEY_VALUES_INTO, builder);
}
if (event.getThrown() != null) {
builder.append("\"full_message\":\"");
if (includeStacktrace) {
JsonUtils.quoteAsString(formatThrowable(event.getThrown()), builder);
} else {
JsonUtils.quoteAsString(event.getThrown().toString(), builder);
}
builder.append(QC);
}
builder.append("\"short_message\":\"");
final Message message = event.getMessage();
if (message instanceof CharSequence) {
JsonUtils.quoteAsString(((CharSequence) message), builder);
} else if (gcFree && message instanceof StringBuilderFormattable) {
final StringBuilder messageBuffer = getMessageStringBuilder();
try {
((StringBuilderFormattable) message).formatTo(messageBuffer);
JsonUtils.quoteAsString(messageBuffer, builder);
} finally {
trimToMaxSize(messageBuffer);
}
} else {
JsonUtils.quoteAsString(toNullSafeString(message.getFormattedMessage()), builder);
}
builder.append(Q);
builder.append('}');
if (includeNullDelimiter) {
builder.append('\0');
}
return builder;
}
use of org.apache.logging.log4j.core.lookup.StrSubstitutor in project logging-log4j2 by apache.
the class WebLookupTest method testLookup.
@Test
public void testLookup() throws Exception {
ContextAnchor.THREAD_CONTEXT.remove();
final ServletContext servletContext = new MockServletContext();
servletContext.setAttribute("TestAttr", "AttrValue");
servletContext.setInitParameter("TestParam", "ParamValue");
servletContext.setAttribute("Name1", "Ben");
servletContext.setInitParameter("Name2", "Jerry");
final Log4jWebLifeCycle initializer = WebLoggerContextUtils.getWebLifeCycle(servletContext);
try {
initializer.start();
initializer.setLoggerContext();
final LoggerContext ctx = ContextAnchor.THREAD_CONTEXT.get();
assertNotNull("No LoggerContext", ctx);
assertNotNull("No ServletContext", ctx.getExternalContext());
final Configuration config = ctx.getConfiguration();
assertNotNull("No Configuration", config);
final StrSubstitutor substitutor = config.getStrSubstitutor();
assertNotNull("No Interpolator", substitutor);
String value = substitutor.replace("${web:initParam.TestParam}");
assertNotNull("No value for TestParam", value);
assertEquals("Incorrect value for TestParam: " + value, "ParamValue", value);
value = substitutor.replace("${web:attr.TestAttr}");
assertNotNull("No value for TestAttr", value);
assertEquals("Incorrect value for TestAttr: " + value, "AttrValue", value);
value = substitutor.replace("${web:Name1}");
assertNotNull("No value for Name1", value);
assertEquals("Incorrect value for Name1: " + value, "Ben", value);
value = substitutor.replace("${web:Name2}");
assertNotNull("No value for Name2", value);
assertEquals("Incorrect value for Name2: " + value, "Jerry", value);
} catch (final IllegalStateException e) {
fail("Failed to initialize Log4j properly." + e.getMessage());
}
initializer.stop();
ContextAnchor.THREAD_CONTEXT.remove();
}
Aggregations