Example #1
0
  String getLogDir(Configuration conf) {
    String logDir = conf.get("hive.log.dir");
    if (logDir == null) {
      logDir = System.getProperty("hive.log.dir");
    }
    if (logDir != null) {
      return logDir;
    }

    LoggerContext context = (LoggerContext) LogManager.getContext(false);
    for (Logger logger : context.getLoggers()) {
      for (Appender appender : logger.getAppenders().values()) {
        if (appender instanceof AbstractOutputStreamAppender) {
          OutputStreamManager manager = ((AbstractOutputStreamAppender<?>) appender).getManager();
          if (manager instanceof FileManager) {
            String fileName = ((FileManager) manager).getFileName();
            if (fileName != null) {
              return fileName.substring(0, fileName.lastIndexOf('/'));
            }
          }
        }
      }
    }
    return null;
  }
 @AfterClass
 public static void cleanupClass() {
   System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
   final LoggerContext ctx = LoggerContext.getContext();
   ctx.reconfigure();
   StatusLogger.getLogger().reset();
 }
 /**
  * Not a real test, just make sure we can compile access to the typed manager.
  *
  * @throws IOException
  */
 @Test
 public void testAccessManagerWithStrings() throws IOException {
   try (final LoggerContext ctx = LoggerContext.getContext(false)) {
     final Configuration config = ctx.getConfiguration();
     final File file = File.createTempFile("RollingFileAppenderAccessTest", ".tmp");
     file.deleteOnExit();
     final RollingFileAppender appender =
         RollingFileAppender.createAppender(
             file.getCanonicalPath(),
             "FilePattern",
             null,
             "Name",
             null,
             null,
             null,
             OnStartupTriggeringPolicy.createPolicy(1),
             null,
             null,
             null,
             null,
             null,
             null,
             config);
     final RollingFileManager manager = appender.getManager();
     // Since the RolloverStrategy and TriggeringPolicy are immutable, we could also use generics
     // to type their
     // access.
     Assert.assertNotNull(manager.getRolloverStrategy());
     Assert.assertNotNull(manager.getTriggeringPolicy());
   }
 }
Example #4
0
 @BeforeClass
 public void configure() {
   LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
   Configuration config = ctx.getConfiguration();
   config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME).setLevel(Level.INFO);
   ctx.updateLoggers(); // This causes all Loggers to refetch information from their LoggerConfig.
   dao = new ScriptDao();
 }
  private void addInstrumentedAppender(final MetricRegistry metrics, final Level level) {
    final InstrumentedAppender appender = new InstrumentedAppender(metrics, null, null, false);
    appender.start();

    final LoggerContext context = (LoggerContext) LogManager.getContext(false);
    final org.apache.logging.log4j.core.config.Configuration config = context.getConfiguration();
    config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME).addAppender(appender, level, null);
    context.updateLoggers(config);
  }
  private void initializeLogging(final Level logLevel) {
    final LoggerContext context = (LoggerContext) LogManager.getContext(false);
    final org.apache.logging.log4j.core.config.Configuration config = context.getConfiguration();

    config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME).setLevel(logLevel);
    config.getLoggerConfig(Main.class.getPackage().getName()).setLevel(logLevel);

    context.updateLoggers(config);
  }
Example #7
0
 private void initOperationLogCapture(String loggingMode) {
   // Register another Appender (with the same layout) that talks to us.
   Appender ap = LogDivertAppender.createInstance(this, OperationLog.getLoggingLevel(loggingMode));
   LoggerContext context = (LoggerContext) LogManager.getContext(false);
   Configuration configuration = context.getConfiguration();
   LoggerConfig loggerConfig =
       configuration.getLoggerConfig(LoggerFactory.getLogger(getClass()).getName());
   loggerConfig.addAppender(ap, null, null);
   context.updateLoggers();
   ap.start();
 }
 protected void loadConfiguration(String location, LogFile logFile) {
   Assert.notNull(location, "Location must not be null");
   try {
     LoggerContext ctx = getLoggerContext();
     URL url = ResourceUtils.getURL(location);
     ConfigurationSource source = getConfigurationSource(url);
     ctx.start(ConfigurationFactory.getInstance().getConfiguration(source));
   } catch (Exception ex) {
     throw new IllegalStateException("Could not initialize Log4J2 logging from " + location, ex);
   }
 }
  @Test
  public void test() throws Exception {

    // To ensure our custom plugin is NOT included in the log4j plugin metadata file,
    // we make sure the class does not exist until after the build is finished.
    // So we don't create the custom plugin class until this test is run.
    final File orig = new File("target/test-classes/customplugin/FixedStringLayout.java.source");
    final File f = new File(orig.getParentFile(), "FixedStringLayout.java");
    assertTrue("renamed source file OK", orig.renameTo(f));
    compile(f);
    assertTrue("reverted source file OK", f.renameTo(orig));

    // load the compiled class
    Class.forName("customplugin.FixedStringLayout");

    // now that the custom plugin class exists, we load the config
    // with the packages element pointing to our custom plugin
    ctx = Configurator.initialize("Test1", "customplugin/log4j2-741.xml");
    config = ctx.getConfiguration();
    listAppender = (ListAppender) config.getAppender("List");

    final Logger logger = LogManager.getLogger(PluginManagerPackagesTest.class);
    logger.info("this message is ignored");

    final List<String> messages = listAppender.getMessages();
    assertEquals(messages.toString(), 1, messages.size());
    assertEquals("abc123XYZ", messages.get(0));
  }
Example #10
0
 @Test
 @Ignore
 public void testOnJms() throws Exception {
   EventTopicHome eventTopicHome = new JmsEventTopicHome();
   // EventListener loggerTopic =
   // eventTopicHome.getEventListener(LoggerService.DEFAULT_DESTINATION_NAME);
   Logger logger = loggerContext.getLogger(this.getClass().getCanonicalName());
   logger.debug("This is a test message");
 }
Example #11
0
  @Test
  public void testOnLocal() throws Exception {
    EventTopicHome eventTopicHome = new LocalEventTopicHome();
    // EventListener loggerTopic =
    // eventTopicHome.getEventListener(LoggerService.DEFAULT_DESTINATION_NAME);
    EventSource logEventSource =
        eventTopicHome.getEventSource(LoggerService.DEFAULT_DESTINATION_NAME);

    LoggerService loggerService = new LoggerService();
    logEventSource.wireTo(loggerService);
    loggerService.setConfigLocation(centralConfigLocation);
    loggerService.init();
    loggerService.start();
    synchronized (this) {
      wait(1000);
    }
    Logger logger = loggerContext.getLogger(this.getClass().getCanonicalName());
    logger.debug("This is a test message");
  }
Example #12
0
 /**
  * Constructor used by Logger to specify a LoggerContext.
  *
  * @param context The LoggerContext.
  * @param name The name of the Logger.
  */
 protected Category(final LoggerContext context, final String name) {
   this.logger = context.getLogger(name);
 }
public class PatternParserTest {

  static String OUTPUT_FILE = "output/PatternParser";
  static String WITNESS_FILE = "witness/PatternParser";
  LoggerContext ctx = LoggerContext.getContext();
  Logger root = ctx.getRootLogger();

  private static String msgPattern = "%m%n";
  private final String mdcMsgPattern1 = "%m : %X%n";
  private final String mdcMsgPattern2 = "%m : %X{key1}%n";
  private final String mdcMsgPattern3 = "%m : %X{key2}%n";
  private final String mdcMsgPattern4 = "%m : %X{key3}%n";
  private final String mdcMsgPattern5 = "%m : %X{key1},%X{key2},%X{key3}%n";

  private static String badPattern = "[%d{yyyyMMdd HH:mm:ss,SSS] %-5p [%c{10}] - %m%n";
  private static String customPattern =
      "[%d{yyyyMMdd HH:mm:ss,SSS}] %-5p [%-25.25c{1}:%-4L] - %m%n";
  private static String patternTruncateFromEnd = "%d; %-5p %5.-5c %m%n";
  private static String patternTruncateFromBeginning = "%d; %-5p %5.5c %m%n";
  private static String nestedPatternHighlight =
      "%highlight{%d{dd MMM yyyy HH:mm:ss,SSS}{GMT+0} [%t] %-5level: %msg%n%throwable}";

  private static final String KEY = "Converter";
  private PatternParser parser;

  @Before
  public void setup() {
    parser = new PatternParser(KEY);
  }

  private void validateConverter(
      final List<PatternFormatter> formatter, final int index, final String name) {
    final PatternConverter pc = formatter.get(index).getConverter();
    assertEquals(
        "Incorrect converter " + pc.getName() + " at index " + index + " expected " + name,
        pc.getName(),
        name);
  }

  /** 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");
  }

  /** 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 testPatternTruncateFromBeginning() {
    final List<PatternFormatter> formatters = parser.parse(patternTruncateFromBeginning);
    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  rTest Hello, world" + Strings.LINE_SEPARATOR;
    assertTrue("Expected to end with: " + expected + ". Actual: " + str, str.endsWith(expected));
  }

  @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));
  }

  @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
  public void testNestedPatternHighlight() {
    testNestedPatternHighlight(Level.TRACE, "\u001B[30m");
    testNestedPatternHighlight(Level.DEBUG, "\u001B[36m");
    testNestedPatternHighlight(Level.INFO, "\u001B[32m");
    testNestedPatternHighlight(Level.WARN, "\u001B[33m");
    testNestedPatternHighlight(Level.ERROR, "\u001B[1;31m");
    testNestedPatternHighlight(Level.FATAL, "\u001B[1;31m");
  }

  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 testNanoPatternShort() {
    final List<PatternFormatter> formatters = parser.parse("%N");
    assertNotNull(formatters);
    assertEquals(1, formatters.size());
    assertTrue(formatters.get(0).getConverter() instanceof NanoTimePatternConverter);
  }

  @Test
  public void testNanoPatternLong() {
    final List<PatternFormatter> formatters = parser.parse("%nano");
    assertNotNull(formatters);
    assertEquals(1, formatters.size());
    assertTrue(formatters.get(0).getConverter() instanceof NanoTimePatternConverter);
  }

  @Test
  public void testThreadNamePattern() {
    testThreadNamePattern("%thread");
  }

  @Test
  public void testThreadNameFullPattern() {
    testThreadNamePattern("%threadName");
  }

  @Test
  public void testThreadIdFullPattern() {
    testThreadIdPattern("%threadId");
  }

  @Test
  public void testThreadIdShortPattern1() {
    testThreadIdPattern("%tid");
  }

  @Test
  public void testThreadIdShortPattern2() {
    testThreadIdPattern("%T");
  }

  @Test
  public void testThreadPriorityShortPattern() {
    testThreadPriorityPattern("%tp");
  }

  @Test
  public void testThreadPriorityFullPattern() {
    testThreadPriorityPattern("%threadPriority");
  }

  private void testThreadIdPattern(final String pattern) {
    testFirstConverter(pattern, ThreadIdPatternConverter.class);
  }

  private void testThreadNamePattern(final String pattern) {
    testFirstConverter(pattern, ThreadNamePatternConverter.class);
  }

  private void testThreadPriorityPattern(final String pattern) {
    testFirstConverter(pattern, ThreadPriorityPatternConverter.class);
  }

  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()));
  }

  @Test
  public void testThreadNameShortPattern() {
    testThreadNamePattern("%t");
  }

  @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);
  }
}
Example #14
0
 public void init() throws URISyntaxException {
   this.loggerContext = new LoggerContext(this.getClass().getName());
   loggerContext.setConfigLocation(new URI(configLocation));
 }