use of org.jboss.logmanager.errormanager.OnlyOnceErrorManager in project quarkus by quarkusio.
the class LoggingSetupRecorder method initializeBuildTimeLogging.
public static void initializeBuildTimeLogging(LogConfig config, LogBuildTimeConfig buildConfig, ConsoleRuntimeConfig consoleConfig, LaunchMode launchMode) {
final Map<String, CategoryConfig> categories = config.categories;
final LogContext logContext = LogContext.getLogContext();
final Logger rootLogger = logContext.getLogger("");
rootLogger.setLevel(config.level);
ErrorManager errorManager = new OnlyOnceErrorManager();
final Map<String, CleanupFilterConfig> filters = config.filters;
List<LogCleanupFilterElement> filterElements = new ArrayList<>(filters.size());
for (Entry<String, CleanupFilterConfig> entry : filters.entrySet()) {
filterElements.add(new LogCleanupFilterElement(entry.getKey(), entry.getValue().targetLevel, entry.getValue().ifStartsWith));
}
LogCleanupFilter logCleanupFilter = new LogCleanupFilter(filterElements);
final ArrayList<Handler> handlers = new ArrayList<>(3);
if (config.console.enable) {
final Handler consoleHandler = configureConsoleHandler(config.console, consoleConfig, errorManager, logCleanupFilter, Collections.emptyList(), new RuntimeValue<>(Optional.empty()), launchMode);
errorManager = consoleHandler.getErrorManager();
handlers.add(consoleHandler);
}
Map<String, Handler> namedHandlers = createNamedHandlers(config, consoleConfig, Collections.emptyList(), errorManager, logCleanupFilter, launchMode);
for (Map.Entry<String, CategoryConfig> entry : categories.entrySet()) {
final String categoryName = entry.getKey();
final Level logLevel = getLogLevel(categoryName, categories, CategoryConfig::getLevel, buildConfig.minLevel);
final Level minLogLevel = getLogLevel(categoryName, buildConfig.categories, CategoryBuildTimeConfig::getMinLevel, buildConfig.minLevel);
if (logLevel.intValue() < minLogLevel.intValue()) {
log.warnf("Log level %s for category '%s' set below minimum logging level %s, promoting it to %s", logLevel, entry.getKey(), minLogLevel, minLogLevel);
entry.getValue().level = InheritableLevel.of(minLogLevel.toString());
}
}
for (Map.Entry<String, CategoryConfig> entry : categories.entrySet()) {
final String name = entry.getKey();
final Logger categoryLogger = logContext.getLogger(name);
final CategoryConfig categoryConfig = entry.getValue();
if (!categoryConfig.level.isInherited()) {
categoryLogger.setLevel(categoryConfig.level.getLevel());
}
categoryLogger.setUseParentHandlers(categoryConfig.useParentHandlers);
if (categoryConfig.handlers.isPresent()) {
addNamedHandlersToCategory(categoryConfig, namedHandlers, categoryLogger, errorManager);
}
}
InitialConfigurator.DELAYED_HANDLER.setAutoFlush(false);
InitialConfigurator.DELAYED_HANDLER.setBuildTimeHandlers(handlers.toArray(EmbeddedConfigurator.NO_HANDLERS));
}
use of org.jboss.logmanager.errormanager.OnlyOnceErrorManager in project quarkus by quarkusio.
the class LoggingSetupRecorder method initializeLogging.
public void initializeLogging(LogConfig config, LogBuildTimeConfig buildConfig, final boolean enableWebStream, final RuntimeValue<Optional<Handler>> devUiConsoleHandler, final List<RuntimeValue<Optional<Handler>>> additionalHandlers, final List<RuntimeValue<Map<String, Handler>>> additionalNamedHandlers, final List<RuntimeValue<Optional<Formatter>>> possibleFormatters, final RuntimeValue<Optional<Supplier<String>>> possibleBannerSupplier, LaunchMode launchMode) {
final Map<String, CategoryConfig> categories = config.categories;
final LogContext logContext = LogContext.getLogContext();
final Logger rootLogger = logContext.getLogger("");
if (config.level.intValue() < buildConfig.minLevel.intValue()) {
log.warnf("Root log level %s set below minimum logging level %s, promoting it to %s", config.level, buildConfig.minLevel, buildConfig.minLevel);
rootLogger.setLevel(buildConfig.minLevel);
} else {
rootLogger.setLevel(config.level);
}
ErrorManager errorManager = new OnlyOnceErrorManager();
final Map<String, CleanupFilterConfig> filters = config.filters;
List<LogCleanupFilterElement> filterElements;
if (filters.isEmpty()) {
filterElements = Collections.emptyList();
} else {
filterElements = new ArrayList<>(filters.size());
filters.forEach(new BiConsumer<String, CleanupFilterConfig>() {
@Override
public void accept(String loggerName, CleanupFilterConfig config) {
filterElements.add(new LogCleanupFilterElement(loggerName, config.targetLevel, config.ifStartsWith));
}
});
}
LogCleanupFilter cleanupFiler = new LogCleanupFilter(filterElements);
for (Handler handler : LogManager.getLogManager().getLogger("").getHandlers()) {
handler.setFilter(cleanupFiler);
}
final ArrayList<Handler> handlers = new ArrayList<>(3 + additionalHandlers.size());
if (config.console.enable) {
final Handler consoleHandler = configureConsoleHandler(config.console, consoleRuntimeConfig.getValue(), errorManager, cleanupFiler, possibleFormatters, possibleBannerSupplier, launchMode);
errorManager = consoleHandler.getErrorManager();
handlers.add(consoleHandler);
}
if (launchMode.isDevOrTest()) {
handlers.add(new Handler() {
@Override
public void publish(LogRecord record) {
if (record.getThrown() != null) {
ExceptionReporting.notifyException(record.getThrown());
}
}
@Override
public void flush() {
}
@Override
public void close() throws SecurityException {
}
});
}
if (config.file.enable) {
handlers.add(configureFileHandler(config.file, errorManager, cleanupFiler));
}
if (config.syslog.enable) {
final Handler syslogHandler = configureSyslogHandler(config.syslog, errorManager, cleanupFiler);
if (syslogHandler != null) {
handlers.add(syslogHandler);
}
}
if ((launchMode.isDevOrTest() || enableWebStream) && devUiConsoleHandler != null && devUiConsoleHandler.getValue().isPresent()) {
Handler handler = devUiConsoleHandler.getValue().get();
handler.setErrorManager(errorManager);
handler.setFilter(new LogCleanupFilter(filterElements));
if (possibleBannerSupplier != null && possibleBannerSupplier.getValue().isPresent()) {
Supplier<String> bannerSupplier = possibleBannerSupplier.getValue().get();
String header = "\n" + bannerSupplier.get();
handler.publish(new LogRecord(Level.INFO, header));
}
handlers.add(handler);
}
if (!categories.isEmpty()) {
Map<String, Handler> namedHandlers = createNamedHandlers(config, consoleRuntimeConfig.getValue(), possibleFormatters, errorManager, cleanupFiler, launchMode);
Map<String, Handler> additionalNamedHandlersMap;
if (additionalNamedHandlers.isEmpty()) {
additionalNamedHandlersMap = Collections.emptyMap();
} else {
additionalNamedHandlersMap = new HashMap<>();
for (RuntimeValue<Map<String, Handler>> runtimeValue : additionalNamedHandlers) {
runtimeValue.getValue().forEach(new AdditionalNamedHandlersConsumer(additionalNamedHandlersMap, errorManager, filterElements));
}
}
namedHandlers.putAll(additionalNamedHandlersMap);
categories.forEach(new BiConsumer<String, CategoryConfig>() {
@Override
public void accept(String categoryName, CategoryConfig config) {
final Level logLevel = getLogLevel(categoryName, categories, CategoryConfig::getLevel, buildConfig.minLevel);
final Level minLogLevel = getLogLevel(categoryName, buildConfig.categories, CategoryBuildTimeConfig::getMinLevel, buildConfig.minLevel);
if (logLevel.intValue() < minLogLevel.intValue()) {
log.warnf("Log level %s for category '%s' set below minimum logging level %s, promoting it to %s", logLevel, categoryName, minLogLevel, minLogLevel);
config.level = InheritableLevel.of(minLogLevel.toString());
}
}
});
categories.forEach(new CategoryLoggerConsumer(logContext, namedHandlers, errorManager));
}
for (RuntimeValue<Optional<Handler>> additionalHandler : additionalHandlers) {
final Optional<Handler> optional = additionalHandler.getValue();
if (optional.isPresent()) {
final Handler handler = optional.get();
handler.setErrorManager(errorManager);
handler.setFilter(cleanupFiler);
handlers.add(handler);
}
}
InitialConfigurator.DELAYED_HANDLER.setAutoFlush(false);
InitialConfigurator.DELAYED_HANDLER.setHandlers(handlers.toArray(EmbeddedConfigurator.NO_HANDLERS));
}
Aggregations