@Test
  public void testNanoPatternShortChangesConfigurationNanoClock() {
    final Configuration config = new NullConfiguration();
    assertTrue(config.getNanoClock() instanceof DummyNanoClock);

    final PatternParser pp = new PatternParser(config, KEY, null);
    assertTrue(config.getNanoClock() instanceof DummyNanoClock);

    pp.parse("%m");
    assertTrue(config.getNanoClock() instanceof DummyNanoClock);

    pp.parse("%nano"); // this changes the config clock
    assertTrue(config.getNanoClock() instanceof SystemNanoClock);
  }
  @Test
  public void testNanoPatternLongChangesNanoClockFactoryMode() {
    final Configuration config = new NullConfiguration();
    assertTrue(config.getNanoClock() instanceof DummyNanoClock);

    final PatternParser pp = new PatternParser(config, KEY, null);
    assertTrue(config.getNanoClock() instanceof DummyNanoClock);

    pp.parse("%m");
    assertTrue(config.getNanoClock() instanceof DummyNanoClock);

    pp.parse("%N");
    assertTrue(config.getNanoClock() instanceof SystemNanoClock);
  }
 /** Test the custom pattern */
 @Test
 public void testCustomPattern() {
   final List<PatternFormatter> formatters = parser.parse(customPattern);
   assertNotNull(formatters);
   final Map<String, String> mdc = new HashMap<>();
   mdc.put("loginId", "Fred");
   final Throwable t = new Throwable();
   final StackTraceElement[] elements = t.getStackTrace();
   final Log4jLogEvent event =
       Log4jLogEvent.newBuilder() //
           .setLoggerName("org.apache.logging.log4j.PatternParserTest") //
           .setMarker(MarkerManager.getMarker("TEST")) //
           .setLoggerFqcn(Logger.class.getName()) //
           .setLevel(Level.INFO) //
           .setMessage(new SimpleMessage("Hello, world")) //
           .setContextMap(mdc) //
           .setThreadName("Thread1") //
           .setSource(elements[0])
           .setTimeMillis(System.currentTimeMillis())
           .build();
   final StringBuilder buf = new StringBuilder();
   for (final PatternFormatter formatter : formatters) {
     formatter.format(event, buf);
   }
   final String str = buf.toString();
   final String expected =
       "INFO  [PatternParserTest        :100 ] - Hello, world" + Strings.LINE_SEPARATOR;
   assertTrue("Expected to end with: " + expected + ". Actual: " + str, str.endsWith(expected));
 }
 @Test
 public void testNanoPatternLong() {
   final List<PatternFormatter> formatters = parser.parse("%nano");
   assertNotNull(formatters);
   assertEquals(1, formatters.size());
   assertTrue(formatters.get(0).getConverter() instanceof NanoTimePatternConverter);
 }
 private void testFirstConverter(final String pattern, final Class<?> checkClass) {
   final List<PatternFormatter> formatters = parser.parse(pattern);
   assertNotNull(formatters);
   final String msg = formatters.toString();
   assertEquals(msg, 1, formatters.size());
   assertTrue(msg, checkClass.isInstance(formatters.get(0).getConverter()));
 }
 private void testNestedPatternHighlight(final Level level, final String expectedStart) {
   final List<PatternFormatter> formatters = parser.parse(nestedPatternHighlight);
   assertNotNull(formatters);
   final Throwable t = new Throwable();
   t.getStackTrace();
   final LogEvent event =
       Log4jLogEvent.newBuilder() //
           .setLoggerName("org.apache.logging.log4j.PatternParserTest") //
           .setMarker(MarkerManager.getMarker("TEST")) //
           .setLoggerFqcn(Logger.class.getName()) //
           .setLevel(level) //
           .setMessage(new SimpleMessage("Hello, world")) //
           .setThreadName("Thread1") //
           .setSource(/*stackTraceElement[0]*/ null) //
           .setTimeMillis(System.currentTimeMillis()) //
           .build();
   final StringBuilder buf = new StringBuilder();
   for (final PatternFormatter formatter : formatters) {
     formatter.format(event, buf);
   }
   final String str = buf.toString();
   final String expectedEnd =
       String.format("] %-5s: Hello, world%s\u001B[m", level, Strings.LINE_SEPARATOR);
   assertTrue(
       "Expected to start with: " + expectedStart + ". Actual: " + str,
       str.startsWith(expectedStart));
   assertTrue(
       "Expected to end with: \"" + expectedEnd + "\". Actual: \"" + str,
       str.endsWith(expectedEnd));
 }
  @Test
  public void testBadPattern() {
    final Calendar cal = Calendar.getInstance();
    cal.set(2001, Calendar.FEBRUARY, 3, 4, 5, 6);
    cal.set(Calendar.MILLISECOND, 789);
    final long timestamp = cal.getTimeInMillis();

    final List<PatternFormatter> formatters = parser.parse(badPattern);
    assertNotNull(formatters);
    final Throwable t = new Throwable();
    final StackTraceElement[] elements = t.getStackTrace();
    final LogEvent event =
        Log4jLogEvent.newBuilder() //
            .setLoggerName("a.b.c") //
            .setLoggerFqcn(Logger.class.getName()) //
            .setLevel(Level.INFO) //
            .setMessage(new SimpleMessage("Hello, world")) //
            .setThreadName("Thread1") //
            .setSource(elements[0]) //
            .setTimeMillis(timestamp) //
            .build();
    final StringBuilder buf = new StringBuilder();
    for (final PatternFormatter formatter : formatters) {
      formatter.format(event, buf);
    }
    final String str = buf.toString();

    // eats all characters until the closing '}' character
    final String expected = "[2001-02-03 04:05:06,789] - Hello, world";
    assertTrue(
        "Expected to start with: " + expected + ". Actual: " + str, str.startsWith(expected));
  }
 /** Test the default pattern */
 @Test
 public void defaultPattern() {
   final List<PatternFormatter> formatters = parser.parse(msgPattern);
   assertNotNull(formatters);
   assertTrue(formatters.size() == 2);
   validateConverter(formatters, 0, "Message");
   validateConverter(formatters, 1, "Line Sep");
 }
 public static RegexReplacementConverter newInstance(Configuration config, String[] options) {
   if (options.length != 3) {
     LOGGER.error("Incorrect number of options on replace. Expected 3 received " + options.length);
     return null;
   }
   if (options[0] == null) {
     LOGGER.error("No pattern supplied on replace");
     return null;
   }
   if (options[1] == null) {
     LOGGER.error("No regular expression supplied on replace");
     return null;
   }
   if (options[2] == null) {
     LOGGER.error("No substitution supplied on replace");
     return null;
   }
   Pattern p = Pattern.compile(options[1]);
   PatternParser parser = PatternLayout.createPatternParser(config);
   List<PatternFormatter> formatters = parser.parse(options[0]);
   return new RegexReplacementConverter(formatters, p, options[2]);
 }
 @Test
 public void testPatternTruncateFromEnd() {
   final List<PatternFormatter> formatters = parser.parse(patternTruncateFromEnd);
   assertNotNull(formatters);
   final LogEvent event =
       Log4jLogEvent.newBuilder() //
           .setLoggerName("org.apache.logging.log4j.PatternParserTest") //
           .setLoggerFqcn(Logger.class.getName()) //
           .setLevel(Level.INFO) //
           .setMessage(new SimpleMessage("Hello, world")) //
           .setThreadName("Thread1") //
           .setTimeMillis(System.currentTimeMillis()) //
           .build();
   final StringBuilder buf = new StringBuilder();
   for (final PatternFormatter formatter : formatters) {
     formatter.format(event, buf);
   }
   final String str = buf.toString();
   final String expected = "INFO  org.a Hello, world" + Strings.LINE_SEPARATOR;
   assertTrue("Expected to end with: " + expected + ". Actual: " + str, str.endsWith(expected));
 }
示例#11
0
  private void parseField(MagpieParser parser, boolean isMutable, Map<String, Field> fields) {
    String name = parser.consume(TokenType.NAME).getString();

    // Parse the pattern if there is one.
    Pattern pattern;
    if (parser.lookAhead(TokenType.EQUALS) || parser.lookAhead(TokenType.LINE)) {
      pattern = Pattern.wildcard();
    } else {
      pattern = PatternParser.parse(parser);
    }

    // Parse the initializer if there is one.
    Expr initializer;
    if (parser.match(TokenType.EQUALS)) {
      initializer = parser.parseExpressionOrBlock();
    } else {
      initializer = null;
    }

    fields.put(name, new Field(isMutable, initializer, pattern));
  }