Search in sources :

Example 16 with LogContext

use of org.jboss.logmanager.LogContext in project kafka-admin-api by bf2fc6cc711aee1a0c2a.

the class LoggingConfigWatcher method modify.

private void modify(Path file) {
    Properties properties = new Properties();
    try (InputStream stream = Files.newInputStream(file)) {
        properties.load(stream);
    } catch (IOException e) {
        LOGGER.warn("File {} cannot be read", file, e);
        return;
    }
    LogContext context = LogContext.getLogContext();
    properties.entrySet().stream().filter(this::isLogLevelConfiguration).map(entry -> toLoggerLevel(context, entry.getKey().toString(), entry.getValue().toString())).forEach(override -> {
        org.jboss.logmanager.Logger logger = override.getKey();
        String category = logger.getName();
        Level level = override.getValue();
        Level originalLevel = logger.getLevel() == null ? INHERIT : logger.getLevel();
        LOGGER.infof("Overriding log level for category %s: %s => %s", category.isEmpty() ? "ROOT" : category, originalLevel, level);
        // Save the original so that it can be restored if the override is removed
        overriddenLoggers.computeIfAbsent(category, k -> originalLevel);
        // TODO -- min-level handling? min-level is compile-time optimization
        // See https://github.com/quarkusio/quarkus/blob/2.3.0.Final/core/runtime/src/main/java/io/quarkus/runtime/logging/LoggingSetupRecorder.java#L92
        // TODO -- Handle other category properties (besides .level)
        // See https://quarkus.io/guides/logging#logging-categories
        logger.setLevel(level);
    });
}
Also used : LogContext(org.jboss.logmanager.LogContext) NoSuchFileException(java.nio.file.NoSuchFileException) Logger(org.jboss.logging.Logger) HashMap(java.util.HashMap) Singleton(javax.inject.Singleton) Level(java.util.logging.Level) Inject(javax.inject.Inject) WatchKey(java.nio.file.WatchKey) StandardWatchEventKinds(java.nio.file.StandardWatchEventKinds) Kind(java.nio.file.WatchEvent.Kind) Matcher(java.util.regex.Matcher) Observes(javax.enterprise.event.Observes) Duration(java.time.Duration) Map(java.util.Map) Path(java.nio.file.Path) Properties(java.util.Properties) Files(java.nio.file.Files) ShutdownEvent(io.quarkus.runtime.ShutdownEvent) IOException(java.io.IOException) File(java.io.File) UncheckedIOException(java.io.UncheckedIOException) WatchService(java.nio.file.WatchService) ClosedWatchServiceException(java.nio.file.ClosedWatchServiceException) ManagedExecutor(org.eclipse.microprofile.context.ManagedExecutor) Optional(java.util.Optional) StartupEvent(io.quarkus.runtime.StartupEvent) ConfigProperty(org.eclipse.microprofile.config.inject.ConfigProperty) Pattern(java.util.regex.Pattern) FileSystems(java.nio.file.FileSystems) InputStream(java.io.InputStream) InputStream(java.io.InputStream) LogContext(org.jboss.logmanager.LogContext) Level(java.util.logging.Level) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Properties(java.util.Properties)

Example 17 with LogContext

use of org.jboss.logmanager.LogContext in project kas-fleetshard by bf2fc6cc711aee1a0c2a.

the class LoggingConfigWatcher method updateLogLevel.

/**
 * Taken from https://github.com/quarkusio/quarkus/blob/9861d4f0ccf61ed1fea4f067c309266ce07b0610/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/logstream/LogController.java#L78
 * rather than introducing a full dependency
 * @param loggerName
 * @param levelValue
 */
public static void updateLogLevel(String loggerName, String levelValue) {
    LogContext logContext = LogContext.getLogContext();
    org.jboss.logmanager.Logger logger = logContext.getLogger(loggerName);
    if (logger != null) {
        java.util.logging.Level level = Level.parse(levelValue);
        logger.setLevel(level);
        log.infof("Log level updated [%s] changed to [%s]", loggerName, levelValue);
    }
}
Also used : LogContext(org.jboss.logmanager.LogContext) Level(java.util.logging.Level)

Example 18 with LogContext

use of org.jboss.logmanager.LogContext in project activemq-artemis by apache.

the class LoggingConfigurationFileReloader method getOrCreateUpdater.

private LoggingConfigurationUpdater getOrCreateUpdater() {
    final LogContext logContext = LogContext.getLogContext();
    final org.jboss.logmanager.Logger rootLogger = logContext.getLogger("");
    LoggingConfigurationUpdater updater = rootLogger.getAttachment(KEY);
    if (updater == null) {
        final LogContextConfiguration logContextConfiguration = getOrCreateConfiguration(rootLogger);
        if (logContextConfiguration == null) {
            return null;
        }
        updater = new LoggingConfigurationUpdater(logContextConfiguration);
        final LoggingConfigurationUpdater appearing = rootLogger.attachIfAbsent(KEY, updater);
        if (appearing != null) {
            updater = appearing;
        }
    }
    return updater;
}
Also used : LogContextConfiguration(org.jboss.logmanager.config.LogContextConfiguration) LogContext(org.jboss.logmanager.LogContext)

Example 19 with LogContext

use of org.jboss.logmanager.LogContext in project wildfly-core by wildfly.

the class LoggingSubsystemRollbackTestCase method rollbackUpdateAttribute.

public void rollbackUpdateAttribute(final String profileName) throws Exception {
    // Save the current model
    final ModelNode validSubsystemModel = getSubsystemModel(kernelServices);
    // Add a handler to be removed
    final PathAddress consoleHandler = createConsoleHandlerAddress(profileName, "CONSOLE");
    // Create a new handler
    ModelNode op = SubsystemOperations.createOperation(AbstractHandlerDefinition.CHANGE_LEVEL_OPERATION_NAME, consoleHandler.toModelNode());
    op.get(CommonAttributes.LEVEL.getName()).set("DEBUG");
    ModelNode result = kernelServices.executeOperation(op);
    Assert.assertFalse("The update operation should have failed, but was successful: " + result, SubsystemOperations.isSuccessfulOutcome(result));
    // verify the subsystem model matches the old model
    ModelNode currentModel = getSubsystemModel(kernelServices);
    compare(profileName, validSubsystemModel, currentModel);
    final LogContext logContext = (profileName == null ? LogContext.getLogContext() : LoggingProfileContextSelector.getInstance().get(profileName));
    ConfigurationPersistence config = ConfigurationPersistence.getConfigurationPersistence(logContext);
    compare(profileName, currentModel, config);
    // Fail on a logger write attribute
    final PathAddress rootLoggerAddress = createRootLoggerAddress(profileName);
    op = SubsystemOperations.createOperation("change-root-log-level", rootLoggerAddress.toModelNode());
    op.get(CommonAttributes.LEVEL.getName()).set("TRACE");
    result = kernelServices.executeOperation(op);
    Assert.assertFalse("The update operation should have failed, but was successful: " + result, SubsystemOperations.isSuccessfulOutcome(result));
    // verify the subsystem model matches the old model
    currentModel = getSubsystemModel(kernelServices);
    compare(profileName, validSubsystemModel, currentModel);
    config = ConfigurationPersistence.getConfigurationPersistence(logContext);
    compare(profileName, currentModel, config);
}
Also used : ConfigurationPersistence(org.jboss.as.logging.logmanager.ConfigurationPersistence) PathAddress(org.jboss.as.controller.PathAddress) LogContext(org.jboss.logmanager.LogContext) ModelNode(org.jboss.dmr.ModelNode)

Example 20 with LogContext

use of org.jboss.logmanager.LogContext in project wildfly-core by wildfly.

the class LoggingSubsystemRollbackTestCase method rollbackRemove.

public void rollbackRemove(final String profileName) throws Exception {
    // Save the current model
    final ModelNode validSubsystemModel = getSubsystemModel(kernelServices);
    final CompositeOperationBuilder compositeOperationBuilder = CompositeOperationBuilder.create();
    // The handler address to remove
    final PathAddress consoleHandler = createConsoleHandlerAddress(profileName, "CONSOLE");
    // Remove the handler
    compositeOperationBuilder.addStep(SubsystemOperations.createRemoveOperation(consoleHandler.toModelNode()));
    // The logger to remove
    final PathAddress loggerAddress = createLoggerAddress(profileName, "org.jboss.as.logging");
    compositeOperationBuilder.addStep(SubsystemOperations.createRemoveOperation(loggerAddress.toModelNode()));
    // Add a step to fail
    final ModelNode rootLoggerAddress = createRootLoggerAddress(profileName).toModelNode();
    compositeOperationBuilder.addStep(SubsystemOperations.createWriteAttributeOperation(rootLoggerAddress, CommonAttributes.LEVEL, "INFO"));
    ModelNode result = kernelServices.executeOperation(compositeOperationBuilder.build().getOperation());
    Assert.assertFalse("The update operation should have failed, but was successful: " + result, SubsystemOperations.isSuccessfulOutcome(result));
    // verify the subsystem model matches the old model
    ModelNode currentModel = getSubsystemModel(kernelServices);
    compare(profileName, validSubsystemModel, currentModel);
    final LogContext logContext = (profileName == null ? LogContext.getLogContext() : LoggingProfileContextSelector.getInstance().get(profileName));
    ConfigurationPersistence config = ConfigurationPersistence.getConfigurationPersistence(logContext);
    compare(profileName, currentModel, config);
}
Also used : CompositeOperationBuilder(org.jboss.as.controller.client.helpers.Operations.CompositeOperationBuilder) ConfigurationPersistence(org.jboss.as.logging.logmanager.ConfigurationPersistence) PathAddress(org.jboss.as.controller.PathAddress) LogContext(org.jboss.logmanager.LogContext) ModelNode(org.jboss.dmr.ModelNode)

Aggregations

LogContext (org.jboss.logmanager.LogContext)33 ModelNode (org.jboss.dmr.ModelNode)7 PathAddress (org.jboss.as.controller.PathAddress)6 ConfigurationPersistence (org.jboss.as.logging.logmanager.ConfigurationPersistence)6 Logger (org.jboss.logmanager.Logger)6 ArrayList (java.util.ArrayList)5 Level (java.util.logging.Level)5 PropertyConfigurator (org.jboss.logmanager.PropertyConfigurator)5 File (java.io.File)4 InputStream (java.io.InputStream)4 LogContextConfiguration (org.jboss.logmanager.config.LogContextConfiguration)4 StdioContext (org.jboss.stdio.StdioContext)4 IOException (java.io.IOException)3 Path (java.nio.file.Path)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Properties (java.util.Properties)3 Handler (java.util.logging.Handler)3 DeploymentUnit (org.jboss.as.server.deployment.DeploymentUnit)3 Configurator (org.jboss.logmanager.Configurator)3