Java Code Examples for org.apache.log4j.PatternLayout#setConversionPattern()

The following examples show how to use org.apache.log4j.PatternLayout#setConversionPattern() . 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: LoggerBuilder.java    From jforgame with Apache License 2.0 7 votes vote down vote up
private static Logger build(String name) {
    Logger logger = Logger.getLogger(name);
    logger.removeAllAppenders();
    logger.setLevel(Level.INFO);
    logger.setAdditivity(false);

    RollingFileAppender appender = new RollingFileAppender();
    PatternLayout layout = new PatternLayout();
    String conversionPatten = "[%d] %p %t %c - %m%n";
    layout.setConversionPattern(conversionPatten);
    appender.setLayout(layout);
    appender.setEncoding("utf-8");
    appender.setAppend(true);

    TimeBasedRollingPolicy policy = new TimeBasedRollingPolicy();
    String fp = LoggerBuilder.LOG_PATH + name + "/" + name + ".log.%d{yyyy-MM-dd}";
    policy.setFileNamePattern(fp);
    appender.setRollingPolicy(policy);

    appender.activateOptions();
    logger.addAppender(appender);

    container.put(name,logger);
    return logger;
}
 
Example 2
Source File: TestStreamAppender.java    From samza with Apache License 2.0 6 votes vote down vote up
@Test
public void testNonDefaultSerde() {
  System.setProperty("samza.container.name", "samza-container-1");
  String streamName = StreamAppender.getStreamName("log4jTest", "1");
  Map<String, String> map = new HashMap<String, String>();
  map.put("job.name", "log4jTest");
  map.put("job.id", "1");
  map.put("serializers.registry.log4j-string.class", LoggingEventStringSerdeFactory.class.getCanonicalName());
  map.put("systems.mock.samza.factory", MockSystemFactory.class.getCanonicalName());
  map.put("systems.mock.streams." + streamName + ".samza.msg.serde", "log4j-string");
  map.put("task.log4j.system", "mock");
  MockSystemProducerAppender systemProducerAppender = new MockSystemProducerAppender(new MapConfig(map));
  PatternLayout layout = new PatternLayout();
  layout.setConversionPattern("%m");
  systemProducerAppender.setLayout(layout);
  systemProducerAppender.activateOptions();
  assertNotNull(systemProducerAppender.getSerde());
  assertEquals(LoggingEventStringSerde.class, systemProducerAppender.getSerde().getClass());
}
 
Example 3
Source File: TestStreamAppender.java    From samza with Apache License 2.0 6 votes vote down vote up
@Test
public void testSystemProducerAppenderInAM() throws InterruptedException {
  System.setProperty("samza.container.name", "samza-job-coordinator");

  MockSystemProducerAppender systemProducerAppender = new MockSystemProducerAppender();
  PatternLayout layout = new PatternLayout();
  layout.setConversionPattern("%m");
  systemProducerAppender.setLayout(layout);
  systemProducerAppender.activateOptions();
  log.addAppender(systemProducerAppender);

  log.info("no-received"); // System isn't initialized yet, so this message should be dropped

  systemProducerAppender.setupSystem();
  MockSystemProducerAppender.systemInitialized = true;

  List<String> messages = Lists.newArrayList("testing3", "testing4");
  logAndVerifyMessages(messages);
}
 
Example 4
Source File: TestStreamAppender.java    From samza with Apache License 2.0 6 votes vote down vote up
@Test
public void testStreamCreationUpSetupWhenEnabled() {
  System.setProperty("samza.container.name", "samza-container-1");

  MapConfig mapConfig = new MapConfig(ImmutableMap.of(
      "task.log4j.create.stream.enabled", "true", // Enable explicit stream creation
      "job.name", "log4jTest",
      "job.id", "1",
      "systems.mock.samza.factory", MockSystemFactory.class.getCanonicalName(),
      "task.log4j.system", "mock"));

  MockSystemProducerAppender systemProducerAppender = new MockSystemProducerAppender(mapConfig);
  PatternLayout layout = new PatternLayout();
  layout.setConversionPattern("%m");
  systemProducerAppender.setLayout(layout);
  systemProducerAppender.activateOptions(); // setupSystem() called inside here.
  log.addAppender(systemProducerAppender);

  Assert.assertEquals("__samza_log4jTest_1_logs", MockSystemAdmin.createdStreamName);
}
 
Example 5
Source File: Util.java    From celos with Apache License 2.0 6 votes vote down vote up
public static void setupLogging(File logDir) {
    System.getProperties().setProperty("log4j.defaultInitOverride", "true");

    RollingFileAppender appender = new RollingFileAppender();
    appender.setFile(new File(logDir, "celos.log").getAbsolutePath());
    appender.setAppend(true);

    TimeBasedRollingPolicy rollingPolicy = new TimeBasedRollingPolicy();
    rollingPolicy.setFileNamePattern(new File(logDir, "celos-%d{yyyy-MM-dd}.log").getAbsolutePath());
    appender.setRollingPolicy(rollingPolicy);

    PatternLayout patternLayout = new PatternLayout();
    patternLayout.setConversionPattern("[%d{YYYY-MM-dd HH:mm:ss.SSS}] %-5p: %m%n");
    appender.setLayout(patternLayout);

    appender.activateOptions();
    Logger.getRootLogger().addAppender(appender);
    Logger.getRootLogger().setLevel(Level.INFO);
}
 
Example 6
Source File: LambdaLogger.java    From lambadaframework with MIT License 5 votes vote down vote up
private static Appender getAppender() {
    if (appender == null) {
        PatternLayout patternLayout = new PatternLayout();
        patternLayout.setConversionPattern("%d{yyyy-MM-dd HH:mm:ss} <%X{AWSRequestId}> %-5p %c{1}:%L - %m%n");
        appender = new LambdaAppender();
        appender.setLayout(patternLayout);
    }
    return appender;
}
 
Example 7
Source File: TestStreamAppender.java    From samza with Apache License 2.0 5 votes vote down vote up
@Test
public void testDefaultSerde() {
  System.setProperty("samza.container.name", "samza-container-1");
  MockSystemProducerAppender systemProducerAppender = new MockSystemProducerAppender();
  PatternLayout layout = new PatternLayout();
  layout.setConversionPattern("%m");
  systemProducerAppender.setLayout(layout);
  systemProducerAppender.activateOptions();
  assertNotNull(systemProducerAppender.getSerde());
  assertEquals(LoggingEventJsonSerde.class, systemProducerAppender.getSerde().getClass());
}
 
Example 8
Source File: TestStreamAppender.java    From samza with Apache License 2.0 5 votes vote down vote up
@Test
public void testSystemProducerAppenderInContainer() throws InterruptedException {
  System.setProperty("samza.container.name", "samza-container-1");

  MockSystemProducerAppender systemProducerAppender = new MockSystemProducerAppender();
  PatternLayout layout = new PatternLayout();
  layout.setConversionPattern("%m");
  systemProducerAppender.setLayout(layout);
  systemProducerAppender.activateOptions();
  log.addAppender(systemProducerAppender);

  List<String> messages = Lists.newArrayList("testing1", "testing2");
  logAndVerifyMessages(messages);
}
 
Example 9
Source File: TestStreamAppender.java    From samza with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoStreamCreationUponSetupByDefault() {
  System.setProperty("samza.container.name", "samza-container-1");

  MockSystemProducerAppender systemProducerAppender = new MockSystemProducerAppender();
  PatternLayout layout = new PatternLayout();
  layout.setConversionPattern("%m");
  systemProducerAppender.setLayout(layout);
  systemProducerAppender.activateOptions(); // setupSystem() called inside here.
  log.addAppender(systemProducerAppender);

  Assert.assertEquals("", MockSystemAdmin.createdStreamName);
}
 
Example 10
Source File: TestStreamAppender.java    From samza with Apache License 2.0 5 votes vote down vote up
@Test
public void testExceptionsDoNotKillTransferThread() throws InterruptedException {
  System.setProperty("samza.container.name", "samza-container-1");

  MockSystemProducerAppender systemProducerAppender = new MockSystemProducerAppender();
  PatternLayout layout = new PatternLayout();
  layout.setConversionPattern("%m");
  systemProducerAppender.setLayout(layout);
  systemProducerAppender.activateOptions();
  log.addAppender(systemProducerAppender);

  List<String> messages = Lists.newArrayList("testing5", "testing6", "testing7");

  // Set up latch
  final CountDownLatch allMessagesSent = new CountDownLatch(messages.size());
  MockSystemProducer.listeners.add((source, envelope) -> {
    allMessagesSent.countDown();
    if (allMessagesSent.getCount() == messages.size() - 1) {
      throw new RuntimeException(); // Throw on the first message
    }
  });

  // Log the messages
  messages.forEach((message) -> log.info(message));

  // Wait for messages
  assertTrue("Thread did not send all messages. Count: " + allMessagesSent.getCount(),
      allMessagesSent.await(60, TimeUnit.SECONDS));
}
 
Example 11
Source File: PatternParserTestCase.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
  Test case for MDC conversion pattern. */
public void mdcPattern() throws Exception {
  
  String mdcMsgPattern1 = "%m : %X%n";
  String mdcMsgPattern2 = "%m : %X{key1}%n";
  String mdcMsgPattern3 = "%m : %X{key2}%n";
  String mdcMsgPattern4 = "%m : %X{key3}%n";
  String mdcMsgPattern5 = "%m : %X{key1},%X{key2},%X{key3}%n";
  
  // set up appender
  PatternLayout layout = new PatternLayout(msgPattern);
  Appender appender = new FileAppender(layout, OUTPUT_FILE+"_mdc", false);
          
  // set appender on root and set level to debug
  root.addAppender(appender);
  root.setLevel(Level.DEBUG);
  
  // output starting message
  root.debug("starting mdc pattern test");
 
  layout.setConversionPattern(mdcMsgPattern1);
  root.debug("empty mdc, no key specified in pattern");
  
  layout.setConversionPattern(mdcMsgPattern2);
  root.debug("empty mdc, key1 in pattern");
  
  layout.setConversionPattern(mdcMsgPattern3);
  root.debug("empty mdc, key2 in pattern");
  
  layout.setConversionPattern(mdcMsgPattern4);
  root.debug("empty mdc, key3 in pattern");
  
  layout.setConversionPattern(mdcMsgPattern5);
  root.debug("empty mdc, key1, key2, and key3 in pattern");

  MDC.put("key1", "value1");
  MDC.put("key2", "value2");

  layout.setConversionPattern(mdcMsgPattern1);
  root.debug("filled mdc, no key specified in pattern");
  
  layout.setConversionPattern(mdcMsgPattern2);
  root.debug("filled mdc, key1 in pattern");
  
  layout.setConversionPattern(mdcMsgPattern3);
  root.debug("filled mdc, key2 in pattern");
  
  layout.setConversionPattern(mdcMsgPattern4);
  root.debug("filled mdc, key3 in pattern");
  
  layout.setConversionPattern(mdcMsgPattern5);
  root.debug("filled mdc, key1, key2, and key3 in pattern");

  MDC.remove("key1");
  MDC.remove("key2");

  layout.setConversionPattern(msgPattern);
  root.debug("finished mdc pattern test");

  assertTrue(Compare.compare(OUTPUT_FILE+"_mdc", WITNESS_FILE+"_mdc"));
}
 
Example 12
Source File: TestStreamAppender.java    From samza with Apache License 2.0 4 votes vote down vote up
@Test
public void testQueueTimeout() throws InterruptedException {
  System.setProperty("samza.container.name", "samza-container-1");

  MockSystemProducerAppender systemProducerAppender = new MockSystemProducerAppender();
  systemProducerAppender.queueTimeoutS = 1;
  PatternLayout layout = new PatternLayout();
  layout.setConversionPattern("%m");
  systemProducerAppender.setLayout(layout);
  systemProducerAppender.activateOptions();
  log.addAppender(systemProducerAppender);

  int extraMessageCount = 5;
  int expectedMessagesSent = extraMessageCount - 1; // -1 because when the queue is drained there is one additional message that couldn't be added
  List<String> messages = new ArrayList<>(StreamAppender.DEFAULT_QUEUE_SIZE + extraMessageCount);
  for (int i = 0; i < StreamAppender.DEFAULT_QUEUE_SIZE + extraMessageCount; i++) {
    messages.add(String.valueOf(i));
  }

  // Set up latch
  final CountDownLatch allMessagesSent = new CountDownLatch(expectedMessagesSent); // We expect to drop all but the extra messages
  final CountDownLatch waitForTimeout = new CountDownLatch(1);
  MockSystemProducer.listeners.add((source, envelope) -> {
    allMessagesSent.countDown();
    try {
      waitForTimeout.await();
    } catch (InterruptedException e) {
      fail("Test could not run properly because of a thread interrupt.");
    }
  });

  // Log the messages. This is where the timeout will happen!
  messages.forEach((message) -> log.info(message));

  assertEquals(messages.size() - expectedMessagesSent, systemProducerAppender.metrics.logMessagesDropped.getCount());

  // Allow all the rest of the messages to send.
  waitForTimeout.countDown();

  // Wait for messages
  assertTrue("Thread did not send all messages. Count: " + allMessagesSent.getCount(),
      allMessagesSent.await(60, TimeUnit.SECONDS));
  assertEquals(expectedMessagesSent, MockSystemProducer.messagesReceived.size());
}