Search in sources :

Example 16 with Filter

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

the class AbstractDelegatingLogger method internalLog.

protected void internalLog(final LogRecord record) {
    final Filter filter = getFilter();
    if (filter != null && !filter.isLoggable(record)) {
        return;
    }
    final String msg = formatMessage(record);
    internalLogFormatted(msg, record);
}
Also used : Filter(java.util.logging.Filter)

Example 17 with Filter

use of java.util.logging.Filter in project Payara by payara.

the class LogManagerService method postConstruct.

/**
 * Initialize the loggers
 */
@Override
public void postConstruct() {
    // if the system property is already set, we don't need to do anything
    if (System.getProperty("java.util.logging.config.file") != null) {
        System.out.println("\n\n\n#!## LogManagerService.postConstruct : java.util.logging.config.file=" + System.getProperty("java.util.logging.config.file"));
        return;
    }
    // logging.properties massaging.
    final LogManager logMgr = LogManager.getLogManager();
    File loggingPropertiesFile = null;
    // reset settings
    try {
        loggingPropertiesFile = getLoggingFile();
        System.setProperty("java.util.logging.config.file", loggingPropertiesFile.getAbsolutePath());
        String rootFolder = env.getProps().get(com.sun.enterprise.util.SystemPropertyConstants.INSTALL_ROOT_PROPERTY);
        String templateDir = rootFolder + File.separator + "lib" + File.separator + "templates";
        File src = new File(templateDir, ServerEnvironmentImpl.kLoggingPropertiesFileName);
        File dest = new File(env.getConfigDirPath(), ServerEnvironmentImpl.kLoggingPropertiesFileName);
        System.out.println("\n\n\n#!## LogManagerService.postConstruct : rootFolder=" + rootFolder);
        System.out.println("#!## LogManagerService.postConstruct : templateDir=" + templateDir);
        System.out.println("#!## LogManagerService.postConstruct : src=" + src);
        System.out.println("#!## LogManagerService.postConstruct : dest=" + dest);
        if (!loggingPropertiesFile.exists()) {
            LOGGER.log(Level.FINE, "{0} not found, creating new file from template.", loggingPropertiesFile.getAbsolutePath());
            FileUtils.copy(src, dest);
            loggingPropertiesFile = new File(env.getConfigDirPath(), ServerEnvironmentImpl.kLoggingPropertiesFileName);
        }
        logMgr.readConfiguration();
    } catch (IOException e) {
        LOGGER.log(Level.SEVERE, LogFacade.ERROR_READING_CONF_FILE, e);
    }
    FormatterDelegate agentDelegate = null;
    if (agent != null) {
        agentDelegate = new AgentFormatterDelegate(agent);
    }
    // force the ConsoleHandler to use GF formatter
    String formatterClassName = null;
    try {
        Map<String, String> props = getLoggingProperties();
        formatterClassName = props.get(CONSOLEHANDLER_FORMATTER_PROPERTY);
        setConsoleHandlerLogFormat(formatterClassName, props, logMgr);
        // setting default attributes value for all properties
        setDefaultLoggingProperties(props);
        Collection<Handler> handlers = getHandlerServices(props);
        if (handlers != null && handlers.size() > 0) {
            // add the new handlers to the root logger
            for (Handler handler : handlers) {
                addHandler(handler);
            }
        }
        // add the filter if there is one
        String filterClassName = props.get(LoggingXMLNames.xmltoPropsMap.get("log-filter"));
        if (filterClassName != null) {
            Filter filterClass = habitat.getService(Filter.class, filterClassName);
            Logger rootLogger = Logger.getLogger("");
            if (rootLogger != null) {
                rootLogger.setFilter(filterClass);
            }
        }
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, LogFacade.ERROR_APPLYING_CONF, e);
    }
    // Logger.getLogger() is still synchronized.
    synchronized (java.util.logging.Logger.class) {
        synchronized (logMgr) {
            Enumeration<String> loggerNames = logMgr.getLoggerNames();
            while (loggerNames.hasMoreElements()) {
                String loggerName = loggerNames.nextElement();
                Logger logger = logMgr.getLogger(loggerName);
                if (logger == null) {
                    continue;
                }
                for (Handler handler : logger.getHandlers()) {
                    Formatter formatter = handler.getFormatter();
                    if (formatter != null && formatter instanceof UniformLogFormatter) {
                        ((UniformLogFormatter) formatter).setDelegate(agentDelegate);
                    }
                }
            }
        }
    }
    // finally listen to changes to the loggingPropertiesFile.properties file
    listenToChangesOnloggingPropsFile(loggingPropertiesFile, logMgr);
    // Log the messages that were generated very early before this Service
    // started.  Just use our own logger...
    List<EarlyLogger.LevelAndMessage> catchUp = EarlyLogger.getEarlyMessages();
    if (!catchUp.isEmpty()) {
        for (EarlyLogger.LevelAndMessage levelAndMessage : catchUp) {
            LOGGER.log(levelAndMessage.getLevel(), levelAndMessage.getMessage());
        }
        catchUp.clear();
    }
    ArrayBlockingQueue<LogRecord> catchEarlyMessage = EarlyLogHandler.earlyMessages;
    while (!catchEarlyMessage.isEmpty()) {
        LogRecord logRecord = catchEarlyMessage.poll();
        if (logRecord != null) {
            LOGGER.log(logRecord);
        }
    }
}
Also used : Formatter(java.util.logging.Formatter) JSONLogFormatter(fish.payara.enterprise.server.logging.JSONLogFormatter) EarlyLogHandler(com.sun.enterprise.module.bootstrap.EarlyLogHandler) Handler(java.util.logging.Handler) PayaraNotificationFileHandler(fish.payara.enterprise.server.logging.PayaraNotificationFileHandler) ConsoleHandler(java.util.logging.ConsoleHandler) IOException(java.io.IOException) Logger(java.util.logging.Logger) EarlyLogger(com.sun.enterprise.util.EarlyLogger) EarlyLogger(com.sun.enterprise.util.EarlyLogger) AgentFormatterDelegate(com.sun.enterprise.v3.logging.AgentFormatterDelegate) IOException(java.io.IOException) ValidationException(javax.validation.ValidationException) AgentFormatterDelegate(com.sun.enterprise.v3.logging.AgentFormatterDelegate) Filter(java.util.logging.Filter) LogRecord(java.util.logging.LogRecord) GFLogRecord(com.sun.common.util.logging.GFLogRecord) LogManager(java.util.logging.LogManager) File(java.io.File)

Example 18 with Filter

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

the class AbstractDelegatingLogger method internalLog.

protected void internalLog(LogRecord record) {
    Filter filter = getFilter();
    if (filter != null && !filter.isLoggable(record)) {
        return;
    }
    String msg = formatMessage(record);
    internalLogFormatted(msg, record);
}
Also used : Filter(java.util.logging.Filter)

Example 19 with Filter

use of java.util.logging.Filter in project grpc-java by grpc.

the class ManagedChannelOrphanWrapperTest method orphanedChannelsAreLogged.

@Test
public void orphanedChannelsAreLogged() {
    ManagedChannel mc = new TestManagedChannel();
    String channelString = mc.toString();
    final ReferenceQueue<ManagedChannelOrphanWrapper> refqueue = new ReferenceQueue<>();
    ConcurrentMap<ManagedChannelReference, ManagedChannelReference> refs = new ConcurrentHashMap<>();
    assertEquals(0, refs.size());
    @SuppressWarnings("UnusedVariable") ManagedChannelOrphanWrapper channel = new ManagedChannelOrphanWrapper(mc, refqueue, refs);
    assertEquals(1, refs.size());
    // Try to capture the log output but without causing terminal noise.  Adding the filter must
    // be done before clearing the ref or else it might be missed.
    final List<LogRecord> records = new ArrayList<>(1);
    Logger orphanLogger = Logger.getLogger(ManagedChannelOrphanWrapper.class.getName());
    Filter oldFilter = orphanLogger.getFilter();
    orphanLogger.setFilter(new Filter() {

        @Override
        public boolean isLoggable(LogRecord record) {
            synchronized (records) {
                records.add(record);
            }
            return false;
        }
    });
    try {
        channel = null;
        final AtomicInteger numOrphans = new AtomicInteger();
        GcFinalization.awaitDone(new FinalizationPredicate() {

            @Override
            public boolean isDone() {
                numOrphans.getAndAdd(ManagedChannelReference.cleanQueue(refqueue));
                return numOrphans.get() > 0;
            }
        });
        assertEquals("unexpected extra orphans", 1, numOrphans.get());
        LogRecord lr;
        synchronized (records) {
            assertEquals(1, records.size());
            lr = records.get(0);
        }
        assertThat(lr.getMessage()).contains("shutdown");
        assertThat(lr.getParameters()).asList().containsExactly(channelString).inOrder();
        assertEquals(Level.SEVERE, lr.getLevel());
        assertEquals(0, refs.size());
    } finally {
        orphanLogger.setFilter(oldFilter);
    }
}
Also used : ReferenceQueue(java.lang.ref.ReferenceQueue) ArrayList(java.util.ArrayList) Logger(java.util.logging.Logger) ManagedChannelReference(io.grpc.internal.ManagedChannelOrphanWrapper.ManagedChannelReference) LogRecord(java.util.logging.LogRecord) Filter(java.util.logging.Filter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ManagedChannel(io.grpc.ManagedChannel) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) FinalizationPredicate(com.google.common.testing.GcFinalization.FinalizationPredicate) Test(org.junit.Test)

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