Search in sources :

Example 1 with Filter

use of java.util.logging.Filter in project OpenAM by OpenRock.

the class Logger method processNewLoggerObject.

/**
     * To add handlers and formatters to the new logger object
     */
private static void processNewLoggerObject(Logger result) {
    Formatter formatter = null;
    String handlerClass = LogManager.HANDLER;
    String formatterClass = LogManager.FORMATTER;
    String levelProp = LogConstants.LOG_PROP_PREFIX + "." + result.logName + ".level";
    /*
         *  see if logging level for this file already defined.
         *  if not, then check AMConfig.properties.
         *  if not, then use Logging service config value.
         *  if not, then use default ("INFO")
         */
    String levelString = lm.getProperty(levelProp);
    if ((levelString == null) || !(levelString.length() > 0)) {
        levelString = SystemProperties.get(levelProp);
        if ((levelString == null) || !(levelString.length() > 0)) {
            levelString = lm.getProperty(LogConstants.LOGGING_LEVEL);
            if ((levelString == null) || !(levelString.length() > 0)) {
                levelString = LogConstants.DEFAULT_LOGGING_LEVEL_STR;
            }
        }
    }
    Level logLevel = null;
    try {
        logLevel = Level.parse(levelString);
    } catch (IllegalArgumentException iaex) {
        logLevel = LogConstants.DEFAULT_LOGGING_LEVEL;
    }
    result.setLevel(logLevel);
    //  but disabled logging in AMConfig.properties takes precedence
    String logStatus = lm.getProperty(LogConstants.LOG_STATUS_ATTR);
    if (logStatus != null && logStatus.startsWith("INACTIVE")) {
        logLevel = Level.OFF;
    }
    result.setLevel(logLevel);
    Class clz = null;
    Class[] parameters = { String.class };
    Object[] parameterObjects = { result.logName };
    Constructor cons = null;
    Handler handler = null;
    if (handlerClass == null) {
        Debug.error("Logger:processNewLoggerObject:" + "HandlerClass not in classpath ");
        return;
    }
    try {
        clz = Class.forName(handlerClass);
    } catch (Exception e) {
        Debug.error("Logger:processNewLoggerObject:" + "HandlerClass not in classpath: " + handlerClass, e);
        return;
    }
    try {
        if (clz != null) {
            cons = clz.getDeclaredConstructor(parameters);
        }
    } catch (Exception e) {
        Debug.error("Logger:processNewLoggerObject:" + "constructor parameter mismatch ", e);
        return;
    }
    try {
        if (cons != null) {
            handler = (Handler) cons.newInstance(parameterObjects);
        }
    } catch (Exception e) {
        Debug.error("Logger:processNewLoggerObject:" + "Could not instantiate handler: " + handlerClass, e);
        return;
    }
    if (formatterClass == null) {
        Debug.error("Logger:processNewLoggerObject:" + "formatterClass not in classpath ");
        return;
    }
    try {
        clz = Thread.currentThread().getContextClassLoader().loadClass(formatterClass);
    } catch (Exception e) {
        Debug.error("Logger:processNewLoggerObject:" + "Could not load Formatter Class: " + formatterClass, e);
        return;
    }
    try {
        if (clz != null) {
            formatter = (Formatter) clz.newInstance();
        }
    } catch (Exception e) {
        Debug.error("Logger:processNewLoggerObject:" + "Could not get Formatter instance " + formatterClass, e);
        return;
    }
    try {
        handler.setFormatter(formatter);
        result.addHandler(handler);
    } catch (Exception e) {
        Debug.error("Logger:processNewLoggerObject:" + "Unable to add Handler", e);
        return;
    }
    String filterClassName = lm.getProperty(LogConstants.FILTER_CLASS_NAME);
    try {
        if (filterClassName != null) {
            Filter filter = (Filter) Class.forName(filterClassName).newInstance();
            result.setFilter(filter);
        }
    } catch (Exception e) {
        Debug.error("Logger:processNewLoggerObject:" + "Could not set Filter: " + filterClassName, e);
    }
    result.setUseParentHandlers(false);
    resolveHostName = Boolean.valueOf(lm.getProperty(LogConstants.LOG_RESOLVE_HOSTNAME_ATTR)).booleanValue();
}
Also used : Filter(java.util.logging.Filter) Formatter(java.util.logging.Formatter) Constructor(java.lang.reflect.Constructor) Handler(java.util.logging.Handler) Level(java.util.logging.Level) MissingResourceException(java.util.MissingResourceException) IOException(java.io.IOException) SSOException(com.iplanet.sso.SSOException)

Example 2 with Filter

use of java.util.logging.Filter in project j2objc by google.

the class MemoryHandlerTest method testFlush.

public void testFlush() {
    Filter filter = handler.getFilter();
    Formatter formatter = handler.getFormatter();
    writer.getBuffer().setLength(0);
    handler.flush();
    assertEquals(writer.toString(), "flush");
    assertEquals(handler.getFilter(), filter);
    assertEquals(handler.getFormatter(), formatter);
    assertNull(handler.getEncoding());
    assertNotNull(handler.getErrorManager());
    assertEquals(handler.getLevel(), Level.FINE);
    assertEquals(handler.getPushLevel(), Level.WARNING);
    assertTrue(handler.isLoggable(new LogRecord(Level.SEVERE, "test")));
}
Also used : Filter(java.util.logging.Filter) LogRecord(java.util.logging.LogRecord) SimpleFormatter(java.util.logging.SimpleFormatter) Formatter(java.util.logging.Formatter)

Example 3 with Filter

use of java.util.logging.Filter in project j2objc by google.

the class LoggerTest method testGetSetFilter_NamedLoggerSufficientPrivilege.

/*
	 * Test getFilter & setFilter with normal value for a named logger, having
	 * sufficient privilege.
	 */
public void testGetSetFilter_NamedLoggerSufficientPrivilege() {
    Logger log = Logger.getLogger("testGetSetFilter_NamedLoggerSufficientPrivilege");
    Filter f = new MockFilter();
    assertNull(log.getFilter());
    log.setFilter(f);
    assertSame(f, log.getFilter());
}
Also used : Filter(java.util.logging.Filter) Logger(java.util.logging.Logger)

Example 4 with Filter

use of java.util.logging.Filter in project sis by apache.

the class LoggerAdapter method log.

/**
 * Logs a record. The default implementation delegates to one of the
 * {@link #logrb(Level,String,String,ResourceBundle,String,Object[]) logrb} or
 * {@link #logp(Level,String,String,String)} methods.
 *
 * @param record  the log record to be published.
 */
@Override
public void log(final LogRecord record) {
    /*
         * The filter should always be null since we overrode the 'setFilter' method as a no-op.
         * But we check it anyway as matter of principle just in case some subclass overrides the
         * 'getFilter()' method. This is the only method where we can do this check cheaply. Note
         * that this is NOT the check for logging level; Filters are for user-specified criterions.
         */
    final Filter filter = getFilter();
    if (filter != null && !filter.isLoggable(record)) {
        return;
    }
    final Level level = record.getLevel();
    final String sourceClass = record.getSourceClassName();
    final String sourceMethod = record.getSourceMethodName();
    final String message = record.getMessage();
    final Object[] params = record.getParameters();
    final Throwable thrown = record.getThrown();
    final ResourceBundle bundle = record.getResourceBundle();
    final boolean useThrown = (thrown != null) && (params == null || params.length == 0);
    if (bundle != null) {
        if (useThrown) {
            logrb(level, sourceClass, sourceMethod, bundle, message, thrown);
        } else {
            logrb(level, sourceClass, sourceMethod, bundle, message, params);
        }
    } else {
        final String bundleName = record.getResourceBundleName();
        if (bundleName != null) {
            if (useThrown) {
                logrb(level, sourceClass, sourceMethod, bundleName, message, thrown);
            } else {
                logrb(level, sourceClass, sourceMethod, bundleName, message, params);
            }
        } else {
            if (useThrown) {
                logp(level, sourceClass, sourceMethod, message, thrown);
            } else {
                logp(level, sourceClass, sourceMethod, message, params);
            }
        }
    }
}
Also used : Filter(java.util.logging.Filter) Level(java.util.logging.Level) ResourceBundle(java.util.ResourceBundle)

Example 5 with Filter

use of java.util.logging.Filter in project evosuite by EvoSuite.

the class MockFileHandler method isLoggable.

// ----- methods from StreamHandler ----------
@Override
public boolean isLoggable(LogRecord record) {
    if (record == null) {
        return false;
    }
    int levelValue = getLevel().intValue();
    if (record.getLevel().intValue() < levelValue || levelValue == offValue) {
        return false;
    }
    Filter filter = getFilter();
    if (filter == null) {
        return true;
    }
    return filter.isLoggable(record);
}
Also used : Filter(java.util.logging.Filter)

Aggregations

Filter (java.util.logging.Filter)19 LogRecord (java.util.logging.LogRecord)6 Logger (java.util.logging.Logger)6 Formatter (java.util.logging.Formatter)5 File (java.io.File)3 IOException (java.io.IOException)3 Level (java.util.logging.Level)3 ConsoleHandler (java.util.logging.ConsoleHandler)2 Handler (java.util.logging.Handler)2 SimpleFormatter (java.util.logging.SimpleFormatter)2 Test (org.junit.Test)2 JacORBFilter (alma.acs.logging.adapters.JacORBFilter)1 UnnamedLogger (alma.maci.loggingconfig.UnnamedLogger)1 FinalizationPredicate (com.google.common.testing.GcFinalization.FinalizationPredicate)1 SSOException (com.iplanet.sso.SSOException)1 GFLogRecord (com.sun.common.util.logging.GFLogRecord)1 EarlyLogHandler (com.sun.enterprise.module.bootstrap.EarlyLogHandler)1 EarlyLogger (com.sun.enterprise.util.EarlyLogger)1 AgentFormatterDelegate (com.sun.enterprise.v3.logging.AgentFormatterDelegate)1 JSONLogFormatter (fish.payara.enterprise.server.logging.JSONLogFormatter)1