Java Code Examples for org.apache.logging.log4j.core.lookup.StrSubstitutor#replace()

The following examples show how to use org.apache.logging.log4j.core.lookup.StrSubstitutor#replace() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WebLookupTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testLookup2() throws Exception {
    ContextAnchor.THREAD_CONTEXT.remove();
    final ServletContext servletContext = new MockServletContext();
    ((MockServletContext) servletContext).setContextPath("/");
    servletContext.setAttribute("TestAttr", "AttrValue");
    servletContext.setInitParameter("myapp.logdir", "target");
    servletContext.setAttribute("Name1", "Ben");
    servletContext.setInitParameter("Name2", "Jerry");
    servletContext.setInitParameter("log4jConfiguration", "WEB-INF/classes/log4j-webvar.xml");
    final Log4jWebLifeCycle initializer = WebLoggerContextUtils.getWebLifeCycle(servletContext);
    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 Map<String, Appender> appenders = config.getAppenders();
    for (final Map.Entry<String, Appender> entry : appenders.entrySet()) {
        if (entry.getKey().equals("file")) {
            final FileAppender fa = (FileAppender) entry.getValue();
            assertEquals("target/myapp.log", fa.getFileName());
        }
    }
    final StrSubstitutor substitutor = config.getStrSubstitutor();
    String value = substitutor.replace("${web:contextPathName:-default}");
    assertNotNull("No value for context name", value);
    assertEquals("Incorrect value for context name", "default", value);
    initializer.stop();
    ContextAnchor.THREAD_CONTEXT.remove();
}
 
Example 2
Source File: PatternProcessor.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
/**
   * Formats file name.
   * @param subst The StrSubstitutor.
   * @param buf string buffer to which formatted file name is appended, may not be null.
   * @param obj object to be evaluated in formatting, may not be null.
   */
  public final void formatFileName(final StrSubstitutor subst, final StringBuilder buf, final boolean useCurrentTime,
                                   final Object obj) {
      // LOG4J2-628: we deliberately use System time, not the log4j.Clock time
      // for creating the file name of rolled-over files.
LOGGER.debug("Formatting file name. useCurrentTime={}, currentFileTime={}, prevFileTime={}",
	useCurrentTime, currentFileTime, prevFileTime);
final long time = useCurrentTime ? currentFileTime != 0 ? currentFileTime : System.currentTimeMillis() :
              prevFileTime != 0 ? prevFileTime : System.currentTimeMillis();
      formatFileName(buf, new Date(time), obj);
      final LogEvent event = new Log4jLogEvent.Builder().setTimeMillis(time).build();
      final String fileName = subst.replace(event, buf);
      buf.setLength(0);
      buf.append(fileName);
  }
 
Example 3
Source File: WebLookupTest.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Test
public void testLookup() throws Exception {
    ContextAnchor.THREAD_CONTEXT.remove();
    final ServletContext servletContext = new MockServletContext();
    ((MockServletContext) servletContext).setContextPath("/WebApp");
    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);
        value = substitutor.replace("${web:contextPathName}");
        assertNotNull("No value for context name", value);
        assertEquals("Incorrect value for context name", "WebApp", value);
    } catch (final IllegalStateException e) {
        fail("Failed to initialize Log4j properly." + e.getMessage());
    }
    initializer.stop();
    ContextAnchor.THREAD_CONTEXT.remove();
}