Java Code Examples for org.jboss.logging.Logger#Level

The following examples show how to use org.jboss.logging.Logger#Level . 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: QuarkusExceptionHandler.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@Override
public boolean handleThrowable(HttpServerExchange exchange, ServletRequest request, ServletResponse response, Throwable t) {
    String uid = BASE_ID + ERROR_COUNT.incrementAndGet();
    request.setAttribute(ERROR_ID, uid);
    ExceptionLog log = t.getClass().getAnnotation(ExceptionLog.class);
    if (log != null) {
        Logger.Level level = log.value();
        Logger.Level stackTraceLevel = log.stackTraceLevel();
        String category = log.category();
        handleCustomLog(exchange, t, level, stackTraceLevel, category, uid);
    } else if (t instanceof IOException) {
        //we log IOExceptions at a lower level
        //because they can be easily caused by malicious remote clients in at attempt to DOS the server by filling the logs
        UndertowLogger.REQUEST_IO_LOGGER.debugf(t, "Exception handling request %s to %s", uid, exchange.getRequestURI());
    } else {
        UndertowLogger.REQUEST_IO_LOGGER.errorf(t, "Exception handling request %s to %s", uid, exchange.getRequestURI());
    }
    return false;
}
 
Example 2
Source File: QuarkusExceptionHandler.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private void handleCustomLog(HttpServerExchange exchange, Throwable t, Logger.Level level, Logger.Level stackTraceLevel,
        String category, String uid) {
    BasicLogger logger = UndertowLogger.REQUEST_LOGGER;
    if (!category.isEmpty()) {
        logger = Logger.getLogger(category);
    }
    boolean stackTrace = true;
    if (stackTraceLevel.ordinal() > level.ordinal()) {
        if (!logger.isEnabled(stackTraceLevel)) {
            stackTrace = false;
        }
    }
    if (stackTrace) {
        logger.logf(level, t, "Exception handling request %s to %s", uid, exchange.getRequestURI());
    } else {
        logger.logf(level, "Exception handling request %s to %s: %s", uid, exchange.getRequestURI(), t.getMessage());
    }
}
 
Example 3
Source File: LoggingExceptionHandler.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
private void handleCustomLog(HttpServerExchange exchange, Throwable t, Logger.Level level, Logger.Level stackTraceLevel, String category) {
    BasicLogger logger = UndertowLogger.REQUEST_LOGGER;
    if (!category.isEmpty()) {
        logger = Logger.getLogger(category);
    }
    boolean stackTrace = true;
    if (stackTraceLevel.ordinal() > level.ordinal()) {
        if (!logger.isEnabled(stackTraceLevel)) {
            stackTrace = false;
        }
    }
    if (stackTrace) {
        logger.logf(level, t, "Exception handling request to %s", exchange.getRequestURI());
    } else {
        logger.logf(level, "Exception handling request to %s: %s", exchange.getRequestURI(), t.getMessage());
    }
}
 
Example 4
Source File: JsonFormatterTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static boolean isValidLevel(final String level) {
    for (Logger.Level l : LoggingServiceActivator.LOG_LEVELS) {
        if (l.name().equals(level)) {
            return true;
        }
    }
    return false;
}
 
Example 5
Source File: XmlFormatterTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static boolean isValidLevel(final String level) {
    Assert.assertNotNull("A null level was found", level);
    for (Logger.Level l : LoggingServiceActivator.LOG_LEVELS) {
        if (l.name().equals(level)) {
            return true;
        }
    }
    return false;
}
 
Example 6
Source File: JibProcessor.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private Logger.Level toJBossLoggingLevel(LogEvent.Level level) {
    switch (level) {
        case ERROR:
            return Logger.Level.ERROR;
        case WARN:
            return Logger.Level.WARN;
        case LIFECYCLE:
            return Logger.Level.INFO;
        default:
            return Logger.Level.DEBUG;
    }
}
 
Example 7
Source File: JBossLoggingEventListenerProvider.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public void onEvent(AdminEvent adminEvent, boolean includeRepresentation) {
    Logger.Level level = adminEvent.getError() != null ? errorLevel : successLevel;

    if (logger.isEnabled(level)) {
        StringBuilder sb = new StringBuilder();

        sb.append("operationType=");
        sb.append(adminEvent.getOperationType());
        sb.append(", realmId=");
        sb.append(adminEvent.getAuthDetails().getRealmId());
        sb.append(", clientId=");
        sb.append(adminEvent.getAuthDetails().getClientId());
        sb.append(", userId=");
        sb.append(adminEvent.getAuthDetails().getUserId());
        sb.append(", ipAddress=");
        sb.append(adminEvent.getAuthDetails().getIpAddress());
        sb.append(", resourceType=");
        sb.append(adminEvent.getResourceTypeAsString());
        sb.append(", resourcePath=");
        sb.append(adminEvent.getResourcePath());

        if (adminEvent.getError() != null) {
            sb.append(", error=");
            sb.append(adminEvent.getError());
        }
        
        if(logger.isTraceEnabled()) {
            setKeycloakContext(sb);
        }

        logger.log(logger.isTraceEnabled() ? Logger.Level.TRACE : level, sb.toString());
    }
}
 
Example 8
Source File: LoggingServiceActivator.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static Set<Logger.Level> getLevels(final Map<String, Deque<String>> params) {
    final Deque<String> values = params.remove(LOG_LEVELS_KEY);
    if (values != null) {
        // We're only taking the first value which should be comma delimited
        final String levelValue = values.getFirst();
        if (levelValue != null) {
            final EnumSet<Logger.Level> result = EnumSet.noneOf(Logger.Level.class);
            for (String level : levelValue.split(",")) {
                result.add(Logger.Level.valueOf(level.toUpperCase(Locale.ROOT)));
            }
            return result;
        }
    }
    return EnumSet.allOf(Logger.Level.class);
}
 
Example 9
Source File: RequestLoggingFilter.java    From hawkular-metrics with Apache License 2.0 5 votes vote down vote up
private Logger.Level getLogLevel(String config) {
    if (config == null) {
        return  null;
    }
    switch (config) {
        case "TRACE": return Logger.Level.TRACE;
        case "INFO": return Logger.Level.INFO;
        case "WARN": return Logger.Level.WARN;
        case "ERROR": return Logger.Level.ERROR;
        case "FATAL": return Logger.Level.FATAL;
        default: return Logger.Level.DEBUG;
    }
}
 
Example 10
Source File: LoggingExceptionHandler.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public Builder add(Class<? extends Throwable> exception, String category, Logger.Level level, Logger.Level stackTraceLevel) {
    exceptionDetails.put(exception, new ExceptionDetails(level, stackTraceLevel, category));
    return this;
}
 
Example 11
Source File: JBossLoggingEventListenerProvider.java    From keycloak with Apache License 2.0 4 votes vote down vote up
public JBossLoggingEventListenerProvider(KeycloakSession session, Logger logger, Logger.Level successLevel, Logger.Level errorLevel) {
    this.session = session;
    this.logger = logger;
    this.successLevel = successLevel;
    this.errorLevel = errorLevel;
}
 
Example 12
Source File: LoggingServiceActivator.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static String formatMessage(final String msg, final Logger.Level level) {
    return msg + " - " + level.name().toLowerCase(Locale.ROOT);
}
 
Example 13
Source File: LoggingServiceActivator.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
private static String getMessage(final String msg, final Logger.Level level, final boolean includeLevel) {
    if (includeLevel) {
        return formatMessage(msg, level);
    }
    return msg;
}
 
Example 14
Source File: LoggingServiceActivator.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
@SuppressWarnings("Convert2Lambda")
@Override
protected HttpHandler getHttpHandler() {
    return new HttpHandler() {
        @Override
        public void handleRequest(final HttpServerExchange exchange) {
            final Map<String, Deque<String>> params = new TreeMap<>(exchange.getQueryParameters());
            final String msg = getValue(params, MSG_KEY, DEFAULT_MESSAGE);
            final boolean includeLevel = getValue(params, INCLUDE_LEVEL_KEY, false);
            final int logCount = getValue(params, LOG_COUNT_KEY, 1);
            final boolean logInfoOnly = getValue(params, LOG_INFO_ONLY_KEY, false);
            final boolean logException = getValue(params, LOG_EXCEPTION_KEY, false);
            final String ndcValue = getValue(params, NDC_KEY, null);
            final Set<Logger.Level> logLevels = getLevels(params);
            final String loggerName = getValue(params, LOG_NAME_KEY, null);
            if (ndcValue != null) {
                NDC.push(ndcValue);
            }
            // Assume other parameters are MDC key/value pairs
            for (String key : params.keySet()) {
                MDC.put(key, params.get(key).getFirst());
            }
            final Logger logger = (loggerName == null ? LOGGER : Logger.getLogger(loggerName));
            for (int i = 0; i < logCount; i++) {
                if (logInfoOnly) {
                    logger.info(getMessage(msg, Logger.Level.INFO, includeLevel));
                } else {
                    for (Logger.Level level : logLevels) {
                        if (logException) {
                            logger.log(level, getMessage(msg, level, includeLevel), createMultiNestedCause());
                        } else {
                            logger.log(level, getMessage(msg, level, includeLevel));
                        }
                    }
                }
            }
            // Clear NDC and MDC
            NDC.clear();
            MDC.clear();
            exchange.getResponseSender().send("Response sent");
        }
    };
}
 
Example 15
Source File: JBossLoggingEventListenerProvider.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Override
public void onEvent(Event event) {
    Logger.Level level = event.getError() != null ? errorLevel : successLevel;

    if (logger.isEnabled(level)) {
        StringBuilder sb = new StringBuilder();

        sb.append("type=");
        sb.append(event.getType());
        sb.append(", realmId=");
        sb.append(event.getRealmId());
        sb.append(", clientId=");
        sb.append(event.getClientId());
        sb.append(", userId=");
        sb.append(event.getUserId());
        sb.append(", ipAddress=");
        sb.append(event.getIpAddress());

        if (event.getError() != null) {
            sb.append(", error=");
            sb.append(event.getError());
        }

        if (event.getDetails() != null) {
            for (Map.Entry<String, String> e : event.getDetails().entrySet()) {
                sb.append(", ");
                sb.append(e.getKey());
                if (e.getValue() == null || e.getValue().indexOf(' ') == -1) {
                    sb.append("=");
                    sb.append(e.getValue());
                } else {
                    sb.append("='");
                    sb.append(e.getValue());
                    sb.append("'");
                }
            }
        }
        
        AuthenticationSessionModel authSession = session.getContext().getAuthenticationSession(); 
        if(authSession!=null) {
            sb.append(", authSessionParentId=");
            sb.append(authSession.getParentSession().getId());
            sb.append(", authSessionTabId=");
            sb.append(authSession.getTabId());
        }
        
        if(logger.isTraceEnabled()) {
            setKeycloakContext(sb);
        }

        logger.log(logger.isTraceEnabled() ? Logger.Level.TRACE : level, sb.toString());
    }
}
 
Example 16
Source File: LoggingExceptionHandler.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public Builder add(Class<? extends Throwable> exception, String category, Logger.Level level) {
    exceptionDetails.put(exception, new ExceptionDetails(level, Logger.Level.FATAL, category));
    return this;
}
 
Example 17
Source File: LoggingExceptionHandler.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private ExceptionDetails(Logger.Level level, Logger.Level stackTraceLevel, String category) {
    this.level = level;
    this.stackTraceLevel = stackTraceLevel;
    this.category = category;
}
 
Example 18
Source File: LoggingExceptionHandler.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
public Builder add(Class<? extends Throwable> exception, String category, Logger.Level level, Logger.Level stackTraceLevel) {
    exceptionDetails.put(exception, new ExceptionDetails(level, stackTraceLevel, category));
    return this;
}
 
Example 19
Source File: LoggingExceptionHandler.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
public Builder add(Class<? extends Throwable> exception, String category, Logger.Level level) {
    exceptionDetails.put(exception, new ExceptionDetails(level, Logger.Level.FATAL, category));
    return this;
}
 
Example 20
Source File: LoggingExceptionHandler.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
private ExceptionDetails(Logger.Level level, Logger.Level stackTraceLevel, String category) {
    this.level = level;
    this.stackTraceLevel = stackTraceLevel;
    this.category = category;
}