Java Code Examples for org.apache.logging.log4j.Level#FATAL

The following examples show how to use org.apache.logging.log4j.Level#FATAL . 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: LogTypeConverter.java    From teku with Apache License 2.0 6 votes vote down vote up
@Override
public Level convert(String value) {
  switch (value.toUpperCase()) {
    case "OFF":
      return Level.OFF;
    case "FATAL":
      return Level.FATAL;
    case "WARN":
      return Level.WARN;
    case "INFO":
      return Level.INFO;
    case "DEBUG":
      return Level.DEBUG;
    case "TRACE":
      return Level.TRACE;
    case "ALL":
      return Level.ALL;
  }
  throw (new CommandLine.TypeConversionException(
      "'"
          + value
          + "' is not a valid log level. Supported values are [OFF|FATAL|WARN|INFO|DEBUG|TRACE|ALL]"));
}
 
Example 2
Source File: DefaultLogger.java    From ymate-platform-v2 with Apache License 2.0 6 votes vote down vote up
public static Level __parseLogLevel(LogLevel level) {
    switch (level.getLevel()) {
        case 600:
            return Level.TRACE;
        case 500:
            return Level.DEBUG;
        case 400:
            return Level.INFO;
        case 300:
            return Level.WARN;
        case 200:
            return Level.ERROR;
        case 100:
            return Level.FATAL;
        case 0:
            return Level.OFF;
        default:
            return Level.ALL;
    }
}
 
Example 3
Source File: LevelRangeFilterBuilder.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
private Filter createFilter(String levelMax, String levelMin, boolean acceptOnMatch) {
    Level max = Level.FATAL;
    Level min = Level.TRACE;
    if (levelMax != null) {
        max = Level.toLevel(levelMax, Level.FATAL);
    }
    if (levelMin != null) {
        min = Level.toLevel(levelMin, Level.DEBUG);
    }
    org.apache.logging.log4j.core.Filter.Result onMatch = acceptOnMatch
            ? org.apache.logging.log4j.core.Filter.Result.ACCEPT
            : org.apache.logging.log4j.core.Filter.Result.NEUTRAL;

    return new FilterWrapper(LevelRangeFilter.createFilter(min, max, onMatch,
            org.apache.logging.log4j.core.Filter.Result.DENY));
}
 
Example 4
Source File: LevelRangeFilterBuilder.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public Filter parseFilter(Element filterElement, XmlConfigurationFactory factory) {
    final Holder<String> levelMax = new Holder<>();
    final Holder<String> levelMin = new Holder<>();
    final Holder<Boolean> acceptOnMatch = new BooleanHolder();
    forEachElement(filterElement.getElementsByTagName("param"), (currentElement) -> {
        if (currentElement.getTagName().equals("param")) {
            switch (currentElement.getAttribute(NAME_ATTR).toLowerCase()) {
                case LEVEL_MAX:
                    levelMax.set(currentElement.getAttribute(VALUE_ATTR));
                    break;
                case LEVEL_MIN:
                    levelMax.set(currentElement.getAttribute(VALUE_ATTR));
                    break;
                case ACCEPT_ON_MATCH:
                    acceptOnMatch.set(Boolean.parseBoolean(currentElement.getAttribute(VALUE_ATTR)));
                    break;
            }
        }
    });
    Level max = Level.FATAL;
    Level min = Level.TRACE;
    if (levelMax.get() != null) {
        max = Level.toLevel(levelMax.get(), Level.FATAL);
    }
    if (levelMin.get() != null) {
        min = Level.toLevel(levelMin.get(), Level.DEBUG);
    }
    org.apache.logging.log4j.core.Filter.Result onMatch = acceptOnMatch.get() != null && acceptOnMatch.get()
            ? org.apache.logging.log4j.core.Filter.Result.ACCEPT
            : org.apache.logging.log4j.core.Filter.Result.NEUTRAL;

    return new FilterWrapper(LevelRangeFilter.createFilter(min, max, onMatch,
            org.apache.logging.log4j.core.Filter.Result.DENY));
}
 
Example 5
Source File: JsonTemplateLayoutTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void test_PatternResolver() {

    // Create the event template.
    final String eventTemplate = writeJson(Map(
            "message", Map(
                    "$resolver", "pattern",
                    "pattern", "%p:%m")));

    // Create the layout.
    final JsonTemplateLayout layout = JsonTemplateLayout
            .newBuilder()
            .setConfiguration(CONFIGURATION)
            .setEventTemplate(eventTemplate)
            .build();

    // Create the log event.
    final SimpleMessage message = new SimpleMessage("foo");
    final Level level = Level.FATAL;
    final LogEvent logEvent = Log4jLogEvent
            .newBuilder()
            .setLoggerName(LOGGER_NAME)
            .setMessage(message)
            .setLevel(level)
            .build();

    // Check the serialized event.
    usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
        final String expectedMessage = String.format(
                "%s:%s",
                level, message.getFormattedMessage());
        assertThat(accessor.getString("message")).isEqualTo(expectedMessage);
    });

}
 
Example 6
Source File: DiscardingAsyncQueueFullPolicyTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetRouteEnqueuesIfThresholdCapacityReachedButLevelMoreSpecificThanThreshold()
        throws Exception {
    final DiscardingAsyncQueueFullPolicy router = new DiscardingAsyncQueueFullPolicy(Level.WARN);

    for (final Level level : new Level[] {Level.ERROR, Level.FATAL, Level.OFF}) {
        assertEquals(level.name(), EventRoute.SYNCHRONOUS, router.getRoute(currentThreadId(), level));
        assertEquals(level.name(), EventRoute.ENQUEUE, router.getRoute(otherThreadId(), level));
        assertEquals(level.name(), EventRoute.SYNCHRONOUS, router.getRoute(currentThreadId(), level));
        assertEquals(level.name(), EventRoute.ENQUEUE, router.getRoute(otherThreadId(), level));
    }
}
 
Example 7
Source File: DiscardingAsyncQueueFullPolicyTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetRouteEnqueueIfOtherThreadQueueFullAndLevelMoreSpecificThanThreshold() throws Exception {
    final DiscardingAsyncQueueFullPolicy router = new DiscardingAsyncQueueFullPolicy(Level.WARN);

    for (final Level level : new Level[] {Level.ERROR, Level.FATAL, Level.OFF}) {
        assertEquals(level.name(), EventRoute.ENQUEUE, router.getRoute(otherThreadId(), level));
    }
}
 
Example 8
Source File: DiscardingAsyncQueueFullPolicyTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetRouteSynchronousIfCurrentThreadQueueFullAndLevelMoreSpecificThanThreshold() throws Exception {
    final DiscardingAsyncQueueFullPolicy router = new DiscardingAsyncQueueFullPolicy(Level.WARN);

    for (final Level level : new Level[] {Level.ERROR, Level.FATAL, Level.OFF}) {
        assertEquals(level.name(), EventRoute.SYNCHRONOUS, router.getRoute(currentThreadId(), level));
    }
}
 
Example 9
Source File: Log4j2LoggingHelper.java    From herd with Apache License 2.0 4 votes vote down vote up
/**
 * Converts a generic log level to a Log4J specific log level.
 *
 * @param logLevel the generic log level.
 *
 * @return the Log4J specific log level.
 */
private Level genericLogLevelToLog4j(LogLevel logLevel)
{
    Level level = Level.ALL;
    if (logLevel.equals(LogLevel.OFF))
    {
        level = Level.OFF;
    }
    else if (logLevel.equals(LogLevel.FATAL))
    {
        level = Level.FATAL;
    }
    else if (logLevel.equals(LogLevel.ERROR))
    {
        level = Level.ERROR;
    }
    else if (logLevel.equals(LogLevel.WARN))
    {
        level = Level.WARN;
    }
    else if (logLevel.equals(LogLevel.INFO))
    {
        level = Level.INFO;
    }
    else if (logLevel.equals(LogLevel.DEBUG))
    {
        level = Level.DEBUG;
    }
    else if (logLevel.equals(LogLevel.TRACE))
    {
        level = Level.TRACE;
    }
    else if (logLevel.equals(LogLevel.ALL))
    {
        level = Level.ALL;
    }
    else
    {
        LOGGER.warn("Unsupported log level encountered: " + logLevel.toString() + ". Using ALL.");
    }
    return level;
}
 
Example 10
Source File: FatalTag.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Override
protected Level getLevel() {
    return Level.FATAL;
}
 
Example 11
Source File: JsonTemplateLayoutTest.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Test
public void test_unresolvable_nested_fields_are_skipped() {

    // Create the event template.
    final String eventTemplate = writeJson(Map(
            "exception", Map(
                    "message", Map(
                            "$resolver", "exception",
                            "field", "message"),
                    "className", Map(
                            "$resolver", "exception",
                            "field", "className")),
            "exceptionRootCause", Map(
                    "message", Map(
                            "$resolver", "exceptionRootCause",
                            "field", "message"),
                    "className", Map(
                            "$resolver", "exceptionRootCause",
                            "field", "className")),
            "source", Map(
                    "lineNumber", Map(
                            "$resolver", "source",
                            "field", "lineNumber"),
                    "fileName", Map(
                            "$resolver", "source",
                            "field", "fileName")),
            "emptyMap", Collections.emptyMap(),
            "emptyList", Collections.emptyList(),
            "null", null));

    // Create the layout.
    final JsonTemplateLayout layout = JsonTemplateLayout
            .newBuilder()
            .setConfiguration(CONFIGURATION)
            .setEventTemplate(eventTemplate)
            .setStackTraceEnabled(false)        // Disable "exception" and "exceptionRootCause" resolvers.
            .setLocationInfoEnabled(false)      // Disable the "source" resolver.
            .build();

    // Create the log event.
    final SimpleMessage message = new SimpleMessage("foo");
    final Level level = Level.FATAL;
    final Exception thrown = new RuntimeException("bar");
    final LogEvent logEvent = Log4jLogEvent
            .newBuilder()
            .setLoggerName(LOGGER_NAME)
            .setMessage(message)
            .setLevel(level)
            .setThrown(thrown)
            .build();

    // Check the serialized event.
    final String expectedSerializedLogEventJson =
            "{}" + JsonTemplateLayoutDefaults.getEventDelimiter();
    final String actualSerializedLogEventJson = layout.toSerializable(logEvent);
    Assertions
            .assertThat(actualSerializedLogEventJson)
            .isEqualTo(expectedSerializedLogEventJson);

}
 
Example 12
Source File: LevelTest.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Test
public void testLevelLogging() {
    final Marker marker = MarkerManager.getMarker("marker");
    final Message msg = new ObjectMessage("msg");
    final Throwable t = new Throwable("test");
    final Level[] levels = new Level[] { Level.TRACE, Level.DEBUG, Level.INFO, Level.WARN, Level.ERROR, Level.FATAL };
    final String[] names = new String[] { "levelTest", "levelTest.Trace", "levelTest.Debug", "levelTest.Info",
            "levelTest.Warn", "levelTest.Error", "levelTest.Fatal" };
    for (final Level level : levels) {
        for (final String name : names) {
            final Logger logger = context.getLogger(name);
            logger.log(level, msg); // Message
            logger.log(level, 123); // Object
            logger.log(level, name); // String
            logger.log(level, marker, msg); // Marker, Message
            logger.log(level, marker, 123); // Marker, Object
            logger.log(level, marker, name); // marker, String
            logger.log(level, msg, t); // Message, Throwable
            logger.log(level, 123, t); // Object, Throwable
            logger.log(level, name, "param1", "param2"); // String, Object...
            logger.log(level, name, t); // String, Throwable
            logger.log(level, marker, msg, t); // Marker, Message, Throwable
            logger.log(level, marker, 123, t); // Marker, Object, Throwable
            logger.log(level, marker, name, "param1", "param2"); // Marker, String, Object...
            logger.log(level, marker, name, t); // Marker, String, Throwable
        }
    }
    // Logger "levelTest" will not receive same events as "levelTest.Trace"
    int levelCount = names.length - 1;

    final int UNIT = 14;
    final Expected[] expectedResults = new Expected[] { //
    new Expected(listAll, UNIT * levelCount, "TRACE", "All"), //
            new Expected(listTrace, UNIT * levelCount--, "TRACE", "Trace"), //
            new Expected(listDebug, UNIT * levelCount--, "DEBUG", "Debug"), //
            new Expected(listInfo, UNIT * levelCount--, "INFO", "Info"), //
            new Expected(listWarn, UNIT * levelCount--, "WARN", "Warn"), //
            new Expected(listError, UNIT * levelCount--, "ERROR", "Error"), //
            new Expected(listFatal, UNIT * levelCount--, "FATAL", "Fatal"), //
    };
    for (final Expected expected : expectedResults) {
        final String description = expected.description;
        final List<LogEvent> events = expected.appender.getEvents();
        assertNotNull(description + ": No events", events);
        assertThat(events, hasSize(expected.expectedEventCount));
        final LogEvent event = events.get(0);
        assertEquals(
            description + ": Expected level " + expected.expectedInitialEventLevel + ", got" + event.getLevel(),
            event.getLevel().name(), expected.expectedInitialEventLevel);
    }
}