All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.logmanager.configuration.PropertyLogContextConfigurator Maven / Gradle / Ivy

The newest version!
/*
 * JBoss, Home of Professional Open Source.
 *
 * Copyright 2022 Red Hat, Inc., and individual contributors
 * as indicated by the @author tags.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jboss.logmanager.configuration;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.Properties;
import java.util.ServiceLoader;
import java.util.logging.Logger;

import org.jboss.logmanager.Level;
import org.jboss.logmanager.LogContext;
import org.jboss.logmanager.LogContextConfigurator;
import org.jboss.logmanager.StandardOutputStreams;
import org.jboss.logmanager.formatters.PatternFormatter;
import org.jboss.logmanager.handlers.ConsoleHandler;

/**
 * A default log context configuration.
 * 

* If the {@linkplain #configure(LogContext, InputStream) input stream} is {@code null} an attempt is made to find a * {@code logging.properties} file. If the file is not found, a {@linkplain java.util.ServiceLoader service loader} is * used to find the first implementation of a {@link LogContextConfigurator}. If that fails a default * {@link ConsoleHandler} is configured with the pattern {@code %d{yyyy-MM-dd'T'HH:mm:ssXXX} %-5p [%c] (%t) %s%e%n}. *

* *

* Locating the {@code logging.properties} happens in the following order: *

    *
  • The {@code logging.configuration} system property is checked
  • *
  • The current threads {@linkplain ClassLoader#getResourceAsStream(String)} class loader} for a * {@code logging.properties}
  • *
  • Finally {@link Class#getResourceAsStream(String)} is used to locate a {@code logging.properties}
  • *
*

* * @author James R. Perkins */ public class PropertyLogContextConfigurator implements LogContextConfigurator { @Override public void configure(final LogContext logContext, final InputStream inputStream) { final InputStream configIn = inputStream != null ? inputStream : findConfiguration(); final LogContext context = logContext == null ? LogContext.getLogContext() : logContext; // Configure the log context based on a property file if (configIn != null) { final Properties properties = new Properties(); try (Reader reader = new InputStreamReader(configIn, StandardCharsets.UTF_8)) { properties.load(reader); } catch (IOException e) { throw new RuntimeException("Failed to configure log manager with configuration file.", e); } final PropertyContextConfiguration configurator = PropertyContextConfiguration.configure(context, properties); context.attachIfAbsent(ContextConfiguration.CONTEXT_CONFIGURATION_KEY, configurator); } else { // Next check the service loader final Iterator serviceLoader = ServiceLoader .load(LogContextConfigurator.class, PropertyLogContextConfigurator.class.getClassLoader()).iterator(); if (serviceLoader.hasNext()) { serviceLoader.next().configure(context, null); } else { // Configure a default console handler, pattern formatter and associated with the root logger final ConsoleHandler handler = new ConsoleHandler( new PatternFormatter("%d{yyyy-MM-dd'T'HH:mm:ssXXX} %-5p [%c] (%t) %s%e%n")); handler.setLevel(Level.INFO); handler.setAutoFlush(true); final Logger rootLogger = context.getLogger(""); rootLogger.setLevel(Level.INFO); rootLogger.addHandler(handler); } } } private static InputStream findConfiguration() { final String propLoc = System.getProperty("logging.configuration"); if (propLoc != null) { try { return new URL(propLoc).openStream(); } catch (IOException e) { StandardOutputStreams.printError("Unable to read the logging configuration from '%s' (%s)%n", propLoc, e); } } final ClassLoader cl = PropertyLogContextConfigurator.class.getClassLoader(); try { return cl.getResourceAsStream("logging.properties"); } catch (Exception ignore) { return null; } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy