@Test
  public void testException() throws Exception {

    Logger logger = Logger.getLogger(getClass());

    logger.info(LOG_MESSAGE, new Exception("this is an exception"));
    assertEquals(1, GelfTestSender.getMessages().size());

    GelfMessage gelfMessage = GelfTestSender.getMessages().get(0);

    assertEquals(LOG_MESSAGE, gelfMessage.getFullMessage());
    assertEquals(
        AbstractGelfLogAppenderTest.class.getName(),
        gelfMessage.getField(LogMessageField.NamedLogField.SourceClassName.getFieldName()));
    assertEquals(
        "testException",
        gelfMessage.getField(LogMessageField.NamedLogField.SourceMethodName.getFieldName()));

    assertThat(
        gelfMessage.getField(MdcGelfMessageAssembler.FIELD_STACK_TRACE),
        containsString("this is an exception"));
    assertThat(
        gelfMessage.getField(MdcGelfMessageAssembler.FIELD_STACK_TRACE), containsString("skipped"));
    assertThat(
        gelfMessage.getField(MdcGelfMessageAssembler.FIELD_STACK_TRACE), containsString("skipped"));
  }
  @Test
  public void testFields() throws Exception {

    Logger logger = Logger.getLogger(getClass());
    MDC.put("mdcField1", "a value");
    MDC.remove(GelfUtil.MDC_REQUEST_START_MS);

    logger.info(LOG_MESSAGE);
    assertEquals(1, GelfTestSender.getMessages().size());

    GelfMessage gelfMessage = GelfTestSender.getMessages().get(0);

    assertEquals("fieldValue1", gelfMessage.getField("fieldName1"));
    assertEquals("fieldValue2", gelfMessage.getField("fieldName2"));
    assertEquals("a value", gelfMessage.getField("mdcField1"));
    assertNotNull(gelfMessage.getField(LogMessageField.NamedLogField.SourceLineNumber.name()));
    assertEquals(
        "testFields", gelfMessage.getField(LogMessageField.NamedLogField.SourceMethodName.name()));
    assertEquals(
        AbstractGelfLogAppenderTest.class.getName(),
        gelfMessage.getField(LogMessageField.NamedLogField.SourceClassName.name()));
    assertEquals("a value", gelfMessage.getField("mdcField1"));
    assertNull(gelfMessage.getField("mdcField2"));

    assertNull(gelfMessage.getField(GelfUtil.MDC_REQUEST_DURATION));
    assertNull(gelfMessage.getField(GelfUtil.MDC_REQUEST_END));
  }
  @Test
  public void testProfilingWrongStart() throws Exception {

    Logger logger = Logger.getLogger(getClass());
    MDC.put(GelfUtil.MDC_REQUEST_START_MS, "");

    logger.info(LOG_MESSAGE);
    assertEquals(1, GelfTestSender.getMessages().size());

    GelfMessage gelfMessage = GelfTestSender.getMessages().get(0);

    assertNull(gelfMessage.getField(GelfUtil.MDC_REQUEST_DURATION));
    assertNull(gelfMessage.getField(GelfUtil.MDC_REQUEST_END));
  }
  @Test
  public void testLongProfiling() throws Exception {

    Logger logger = Logger.getLogger(getClass());
    MDC.put(GelfUtil.MDC_REQUEST_START_MS, "" + (System.currentTimeMillis() - 2000));

    logger.info(LOG_MESSAGE);
    assertEquals(1, GelfTestSender.getMessages().size());

    GelfMessage gelfMessage = GelfTestSender.getMessages().get(0);

    assertNotNull(gelfMessage.getField(GelfUtil.MDC_REQUEST_DURATION));
    assertNotNull(gelfMessage.getField(GelfUtil.MDC_REQUEST_END));
  }
  @Before
  public void before() throws Exception {
    GelfTestSender.getMessages().clear();

    LogManager.getLogManager().reset();

    MDC.remove("mdcField1");
  }
  @Test
  public void testOriginHost() throws Exception {

    Logger logger = lc.getLogger(getClass());

    logger.info("Blubb Test");
    assertEquals(1, GelfTestSender.getMessages().size());

    GelfMessage gelfMessage = GelfTestSender.getMessages().get(0);

    String crossCheckHostName = gelfMessage.getAdditonalFields().get("crossCheckHostName");

    String json = gelfMessage.toJson();
    assertThat(
        json,
        StringContains.containsString("\"_crossCheckHostName\":\"" + crossCheckHostName + "\""));
    assertThat(json, StringContains.containsString("\"host\":\"" + crossCheckHostName + "\""));
  }
  @Test
  public void testSimpleWithStringFormatSubstitution() throws Exception {

    JBoss7GelfLogHandler handler = getJBoss7GelfLogHandler();

    Logger logger = Logger.getLogger(getClass().getName());
    logger.addHandler(handler);

    logger.log(Level.INFO, "Blubb Test %s", new String[] {"aaa"});
    assertEquals(1, GelfTestSender.getMessages().size());

    GelfMessage gelfMessage = GelfTestSender.getMessages().get(0);

    assertEquals("Blubb Test aaa", gelfMessage.getFullMessage());
    assertEquals("6", gelfMessage.getLevel());
    assertEquals("Blubb Test aaa", gelfMessage.getShortMessage());
    assertEquals(8192, gelfMessage.getMaximumMessageSize());
  }
  @Test
  public void testSimple() throws Exception {

    JBoss7GelfLogHandler handler = getJBoss7GelfLogHandler();

    Logger logger = Logger.getLogger(getClass().getName());
    logger.addHandler(handler);

    logger.info("Blubb Test");
    assertEquals(1, GelfTestSender.getMessages().size());

    GelfMessage gelfMessage = GelfTestSender.getMessages().get(0);

    assertEquals("Blubb Test", gelfMessage.getFullMessage());
    assertEquals("6", gelfMessage.getLevel());
    assertEquals("Blubb Test", gelfMessage.getShortMessage());
    assertEquals(8192, gelfMessage.getMaximumMessageSize());
  }
  @Test
  public void testFields() throws Exception {

    JBoss7GelfLogHandler handler = getJBoss7GelfLogHandler();

    Logger logger = Logger.getLogger(getClass().getName());
    logger.addHandler(handler);

    MDC.put("mdcField1", "a value");

    logger.info("Blubb Test");
    assertEquals(1, GelfTestSender.getMessages().size());

    GelfMessage gelfMessage = GelfTestSender.getMessages().get(0);

    assertEquals("fieldValue1", gelfMessage.getField("fieldName1"));
    assertEquals("fieldValue2", gelfMessage.getField("fieldName2"));
    assertEquals("a value", gelfMessage.getField("mdcField1"));
    assertNull(gelfMessage.getField("mdcField2"));
  }
  @Before
  public void before() throws Exception {
    lc = new LoggerContext();
    JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(lc);

    URL xmlConfigFile = getClass().getResource("/logback-gelf-with-dynamic-originhost.xml");

    configurator.doConfigure(xmlConfigFile);

    GelfTestSender.getMessages().clear();

    MDC.remove("mdcField1");
  }
  @Test
  public void testSimpleInfo() throws Exception {

    Logger logger = Logger.getLogger(getClass());

    NDC.clear();
    NDC.push("ndc message");
    logger.info(LOG_MESSAGE);
    NDC.clear();
    assertEquals(1, GelfTestSender.getMessages().size());

    GelfMessage gelfMessage = GelfTestSender.getMessages().get(0);

    assertEquals(EXPECTED_LOG_MESSAGE, gelfMessage.getFullMessage());
    assertEquals(EXPECTED_LOG_MESSAGE, gelfMessage.getShortMessage());
    assertEquals("ndc message", gelfMessage.getField("NDC"));
    assertNotNull(gelfMessage.getField("MyTime"));
    assertEquals("6", gelfMessage.getLevel());
    assertEquals(8192, gelfMessage.getMaximumMessageSize());
    assertEquals(GelfMessage.GELF_VERSION_1_1, gelfMessage.getVersion());

    // this is because of default-logstash-fields.properties
    assertEquals("INFO", gelfMessage.getAdditonalFields().get("MySeverity"));
  }
  @Test
  public void testLevels() throws Exception {
    Logger logger = Logger.getLogger(getClass());

    String expectedMessage = "foo bar test log message";

    logger.trace(expectedMessage);
    assertEquals(0, GelfTestSender.getMessages().size());

    logger.debug(expectedMessage);
    assertEquals("7", GelfTestSender.getMessages().get(0).getLevel());
    GelfTestSender.getMessages().clear();

    logger.info(expectedMessage);
    assertEquals("6", GelfTestSender.getMessages().get(0).getLevel());
    GelfTestSender.getMessages().clear();

    logger.warn(expectedMessage);
    assertEquals("4", GelfTestSender.getMessages().get(0).getLevel());
    GelfTestSender.getMessages().clear();

    logger.error(expectedMessage);
    assertEquals("3", GelfTestSender.getMessages().get(0).getLevel());
    GelfTestSender.getMessages().clear();

    logger.fatal(expectedMessage);
    assertEquals("2", GelfTestSender.getMessages().get(0).getLevel());
    GelfTestSender.getMessages().clear();
  }