Search in sources :

Example 86 with Logger

use of org.apache.logging.log4j.Logger in project tutorials by eugenp.

the class CustomLoggingIntegrationTest method givenLoggerWithConsoleConfig_whenFilterByMarker_thanOK.

@Test
public void givenLoggerWithConsoleConfig_whenFilterByMarker_thanOK() throws Exception {
    Logger logger = LogManager.getLogger("CONSOLE_PATTERN_APPENDER_MARKER");
    Marker appError = MarkerManager.getMarker("APP_ERROR");
    Marker connectionTrace = MarkerManager.getMarker("CONN_TRACE");
    logger.error(appError, "This marker message at ERROR level should be hidden.");
    logger.trace(connectionTrace, "This is a marker message at TRACE level.");
}
Also used : Marker(org.apache.logging.log4j.Marker) Logger(org.apache.logging.log4j.Logger) Test(org.junit.Test)

Example 87 with Logger

use of org.apache.logging.log4j.Logger in project tutorials by eugenp.

the class CustomLoggingIntegrationTest method givenLoggerWithJdbcConfig_whenLogToDataSource_thanOK.

@Test
public void givenLoggerWithJdbcConfig_whenLogToDataSource_thanOK() throws Exception {
    Logger logger = LogManager.getLogger("JDBC_APPENDER");
    final int count = 88;
    for (int i = 0; i < count; i++) {
        logger.info("This is JDBC message #{} at INFO level.", count);
    }
    Connection connection = ConnectionFactory.getConnection();
    ResultSet resultSet = connection.createStatement().executeQuery("SELECT COUNT(*) AS ROW_COUNT FROM logs");
    int logCount = 0;
    if (resultSet.next()) {
        logCount = resultSet.getInt("ROW_COUNT");
    }
    assertTrue(logCount == count);
}
Also used : Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Logger(org.apache.logging.log4j.Logger) Test(org.junit.Test)

Example 88 with Logger

use of org.apache.logging.log4j.Logger in project tutorials by eugenp.

the class CustomLoggingIntegrationTest method givenLoggerWithDefaultConfig_whenLogToConsole_thanOK.

@Test
public void givenLoggerWithDefaultConfig_whenLogToConsole_thanOK() throws Exception {
    Logger logger = LogManager.getLogger(getClass());
    Exception e = new RuntimeException("This is only a test!");
    logger.info("This is a simple message at INFO level. " + "It will be hidden.");
    logger.error("This is a simple message at ERROR level. " + "This is the minimum visible level.", e);
}
Also used : Logger(org.apache.logging.log4j.Logger) Test(org.junit.Test)

Example 89 with Logger

use of org.apache.logging.log4j.Logger in project tutorials by eugenp.

the class CustomLoggingIntegrationTest method givenLoggerWithConsoleConfig_whenLogToConsoleInColors_thanOK.

@Test
public void givenLoggerWithConsoleConfig_whenLogToConsoleInColors_thanOK() throws Exception {
    Logger logger = LogManager.getLogger("CONSOLE_PATTERN_APPENDER_MARKER");
    Exception e = new RuntimeException("This is only a test!");
    logger.trace("This is a colored message at TRACE level.");
    logger.debug("This is a colored message at DEBUG level. " + "This is the minimum visible level.");
    logger.info("This is a colored message at INFO level.");
    logger.warn("This is a colored message at WARN level.");
    logger.error("This is a colored message at ERROR level.", e);
    logger.fatal("This is a colored message at FATAL level.");
}
Also used : Logger(org.apache.logging.log4j.Logger) Test(org.junit.Test)

Example 90 with Logger

use of org.apache.logging.log4j.Logger in project tutorials by eugenp.

the class CustomLoggingIntegrationTest method givenLoggerWithAsyncConfig_whenLogToJsonFile_thanOK.

@Test
public void givenLoggerWithAsyncConfig_whenLogToJsonFile_thanOK() throws Exception {
    Logger logger = LogManager.getLogger("ASYNC_JSON_FILE_APPENDER");
    final int count = 88;
    for (int i = 0; i < count; i++) {
        logger.info("This is async JSON message #{} at INFO level.", count);
    }
    long logEventsCount = Files.lines(Paths.get("target/logfile.json")).count();
    assertTrue(logEventsCount > 0 && logEventsCount <= count);
}
Also used : Logger(org.apache.logging.log4j.Logger) Test(org.junit.Test)

Aggregations

Logger (org.apache.logging.log4j.Logger)491 Test (org.junit.Test)226 File (java.io.File)80 Test (org.junit.jupiter.api.Test)69 IOException (java.io.IOException)34 LoggerContext (org.apache.logging.log4j.core.LoggerContext)33 Appender (org.apache.logging.log4j.core.Appender)32 Collectors (java.util.stream.Collectors)30 StatusLogger (org.apache.logging.log4j.status.StatusLogger)30 BufferedReader (java.io.BufferedReader)29 Level (org.apache.logging.log4j.Level)27 FileReader (java.io.FileReader)26 Path (java.nio.file.Path)26 CountDownLatch (java.util.concurrent.CountDownLatch)23 Map (java.util.Map)21 IntStream (java.util.stream.IntStream)20 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)20 java.util (java.util)18 HashMap (java.util.HashMap)18 Configuration (org.apache.logging.log4j.core.config.Configuration)18