Java Code Examples for org.apache.log4j.Logger#isEnabledFor()

The following examples show how to use org.apache.log4j.Logger#isEnabledFor() . 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: DefaultMmLogger.java    From megamek with GNU General Public License v2.0 5 votes vote down vote up
@Override
public <T extends Throwable> T log(final String className, final String methodName,
                                   final LogLevel logLevel, String message, final T throwable) {
    // Make sure logging has been initialized.
    if (!initialized.get()) {
        if (!Logger.getRootLogger().getAllAppenders().hasMoreElements()) {
            LogConfig.getInstance().enableSimplifiedLogging();
        }
        initialized.set(true);
    }

    Logger logger = getLogger(className);

    // If logging has been turned off simply return.
    if (!logger.isEnabledFor(logLevel.getLevel())) {
        return throwable;
    }

    // Track the methods logged.
    LoggingProperties.getInstance().putProperty("method", methodName);

    message = methodName + " : " + message;

    // Write the log entry.
    logger.log(logLevel.getLevel(), message, throwable);

    return throwable;
}
 
Example 2
Source File: WebUtils.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
 * Iterates through and logs (at the given level) all attributes and
 * parameters of the given request onto the given Logger
 * 
 * @param request
 * @param logger
 */
public static void logRequestContents(Logger logger, Level level, HttpServletRequest request) {
	if (logger.isEnabledFor(level)) {
		logger.log(level, "--------------------");
		logger.log(level, "HttpRequest attributes:");
		for (Enumeration e = request.getAttributeNames(); e.hasMoreElements();) {
			String attrName = (String) e.nextElement();
			Object attrValue = request.getAttribute(attrName);

			if (attrValue.getClass().isArray()) {
				logCollection(logger, level, attrName, Arrays.asList((Object[]) attrValue));
			}
			else if (attrValue instanceof Collection) {
				logCollection(logger, level, attrName, (Collection) attrValue);
			}
			else if (attrValue instanceof Map) {
				logMap(logger, level, attrName, (Map) attrValue);
			}
			else {
				logObject(logger, level, attrName, attrValue);
			}
		}

		logger.log(level, "--------------------");
		logger.log(level, "HttpRequest parameters:");
		for (Enumeration i = request.getParameterNames(); i.hasMoreElements();) {
			String paramName = (String) i.nextElement();
			String[] paramValues = (String[]) request.getParameterValues(paramName);

			logArray(logger, level, paramName, paramValues);
		}

		logger.log(level, "--------------------");
	}
}
 
Example 3
Source File: BufferedLogger.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
 * Wraps {@link Logger#warn(String)}
 * 
 * @param pattern to format against
 * @param objs an array of objects used as parameters to the <code>pattern</code>
 */
public static final void warn(Object ... objs) {
    Logger log = getLogger();
    if (log.isEnabledFor(WARN)) {
        log.warn(getMessage(objs));
    }
}
 
Example 4
Source File: BufferedLogger.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
 * Wraps {@link Logger#error(String)}
 * 
 * @param pattern to format against
 * @param objs an array of objects used as parameters to the <code>pattern</code>
 */
public static final void error(Object ... objs) {
    Logger log = getLogger();
    if (log.isEnabledFor(ERROR)) {
        getLogger().error(getMessage(objs));
    }
}
 
Example 5
Source File: BufferedLogger.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
 * Wraps {@link Logger#fatal(String)}
 * 
 * @param pattern to format against
 * @param objs an array of objects used as parameters to the <code>pattern</code>
 */
public static final void fatal(Object ... objs) {
    Logger log = getLogger();
    if (log.isEnabledFor(FATAL)) {
        log.fatal(getMessage(objs));
    }
}
 
Example 6
Source File: Journal.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public void record(Logger logger, Level p, String msg, Object... params) {
    if (logger.isEnabledFor(p)) {
        StringBuilder buf = new StringBuilder();
        toString(buf, msg, params);
        String entry = buf.toString();
        log(entry);
        logger.log(p, entry);
    } else {
        log(msg, params);
    }
}
 
Example 7
Source File: DefaultMmLogger.java    From megamek with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean willLog(final Class<?> callingClass, final LogLevel level) {
    Logger logger = getLogger(callingClass.getName());
    return logger.isEnabledFor(level.getLevel());
}