Search in sources :

Example 1 with SyslogHandler

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

the class SyslogAuditLogHandler method stop.

@Override
void stop() {
    connected = false;
    SyslogHandler handler = this.handler;
    this.handler = null;
    if (handler != null) {
        handler.close();
    }
}
Also used : SyslogHandler(org.jboss.logmanager.handlers.SyslogHandler)

Example 2 with SyslogHandler

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

the class HandlerOperations method handleProperty.

/**
 * Handle updating the configuration.
 *
 * @param attribute               the attribute definition
 * @param context                 the context of the operation
 * @param model                   the model to update
 * @param logContextConfiguration the log context configuration
 * @param configuration           the handler configuration
 * @param resolveValue            {@code true} if the value should be resolved via the attribute, otherwise {@code
 *                                false} if the value is already resolved.
 *
 * @throws OperationFailedException if an error occurs
 */
private static void handleProperty(final AttributeDefinition attribute, final OperationContext context, final ModelNode model, final LogContextConfiguration logContextConfiguration, final HandlerConfiguration configuration, final boolean resolveValue) throws OperationFailedException {
    if (attribute.getName().equals(ENABLED.getName())) {
        final boolean value = ((resolveValue ? ENABLED.resolveModelAttribute(context, model).asBoolean() : model.asBoolean()));
        if (value) {
            enableHandler(logContextConfiguration, configuration.getName());
        } else {
            disableHandler(logContextConfiguration, configuration.getName());
        }
    } else if (attribute.getName().equals(ENCODING.getName())) {
        final String resolvedValue = (resolveValue ? ENCODING.resolvePropertyValue(context, model) : model.isDefined() ? model.asString() : null);
        configuration.setEncoding(resolvedValue);
    } else if (attribute.getName().equals(FORMATTER.getName())) {
        // The handler name will be used for the name of a formatter for the formatter attribute
        final String defaultFormatterName = getDefaultFomatterName(configuration.getName());
        // Get the current model and check for a defined named-formatter attribute
        final Resource resource = context.readResource(PathAddress.EMPTY_ADDRESS);
        final ModelNode m = resource.getModel();
        if (m.hasDefined(NAMED_FORMATTER.getName())) {
            // remove the formatter
            if (logContextConfiguration.getFormatterNames().contains(defaultFormatterName)) {
                logContextConfiguration.removeFormatterConfiguration(defaultFormatterName);
            }
        } else {
            // Create a formatter based on the handlers name
            final FormatterConfiguration fmtConfig;
            if (logContextConfiguration.getFormatterNames().contains(defaultFormatterName)) {
                fmtConfig = logContextConfiguration.getFormatterConfiguration(defaultFormatterName);
            } else {
                fmtConfig = logContextConfiguration.addFormatterConfiguration(null, PatternFormatter.class.getName(), defaultFormatterName, PATTERN.getPropertyName());
            }
            final String resolvedValue = (resolveValue ? FORMATTER.resolvePropertyValue(context, model) : model.asString());
            fmtConfig.setPropertyValueString(PATTERN.getPropertyName(), resolvedValue);
            configuration.setFormatterName(defaultFormatterName);
        }
    } else if (attribute.getName().equals(NAMED_FORMATTER.getName())) {
        // The name of the handler will be used for a "formatter" if the named-formatter is not defined
        final String handlerName = configuration.getName();
        final String defaultFormatterName = getDefaultFomatterName(handlerName);
        final ModelNode valueNode = (resolveValue ? NAMED_FORMATTER.resolveModelAttribute(context, model) : model);
        // Set the formatter if the value is defined
        if (valueNode.isDefined()) {
            final String resolvedValue = valueNode.asString();
            configuration.setFormatterName(resolvedValue);
            // If the formatter was previously defined by the formatter attribute, remove the formatter
            if (logContextConfiguration.getFormatterNames().contains(defaultFormatterName)) {
                logContextConfiguration.removeFormatterConfiguration(defaultFormatterName);
            }
        } else if (configuration.getClassName().equals(SyslogHandler.class.getName())) {
            // The value shouldn't be defined so we want to remove the current formatter, however a null formatter
            // is not allowed in a java.util.logging.Handler. Therefore we must configure a formatter of some kind.
            // We'll skip the config step on this and set the handler manually. This allows the formatter
            // configuration not to be persisted. By default the SyslogHandler will use
            // ExtLogRecord.getFormattedMessage() to get the message which the %s pattern should duplicate.
            final Handler instance = configuration.getInstance();
            if (instance != null) {
                instance.setFormatter(new PatternFormatter("%s"));
            }
        } else {
            // If the named-formatter was undefined we need to create a formatter based on the formatter attribute
            final FormatterConfiguration fmtConfig;
            if (logContextConfiguration.getFormatterNames().contains(defaultFormatterName)) {
                fmtConfig = logContextConfiguration.getFormatterConfiguration(defaultFormatterName);
            } else {
                fmtConfig = logContextConfiguration.addFormatterConfiguration(null, PatternFormatter.class.getName(), defaultFormatterName, PATTERN.getPropertyName());
            }
            // Get the current model and set the value of the formatter based on the formatter attribute
            final Resource resource = context.readResource(PathAddress.EMPTY_ADDRESS);
            fmtConfig.setPropertyValueString(PATTERN.getPropertyName(), FORMATTER.resolvePropertyValue(context, resource.getModel()));
            configuration.setFormatterName(defaultFormatterName);
        }
    } else if (attribute.getName().equals(FILTER_SPEC.getName())) {
        final ModelNode valueNode = (resolveValue ? FILTER_SPEC.resolveModelAttribute(context, model) : model);
        final String resolvedValue = (valueNode.isDefined() ? valueNode.asString() : null);
        configuration.setFilter(resolvedValue);
    } else if (attribute.getName().equals(LEVEL.getName())) {
        final String resolvedValue = (resolveValue ? LEVEL.resolvePropertyValue(context, model) : LEVEL.resolver().resolveValue(context, model));
        configuration.setLevel(resolvedValue);
    } else if (attribute.getName().equals(SUBHANDLERS.getName())) {
        final Collection<String> resolvedValue = (resolveValue ? SUBHANDLERS.resolvePropertyValue(context, model) : SUBHANDLERS.resolver().resolveValue(context, model));
        if (resolvedValue.contains(configuration.getName())) {
            throw createOperationFailure(LoggingLogger.ROOT_LOGGER.cannotAddHandlerToSelf(configuration.getName()));
        }
        configuration.setHandlerNames(resolvedValue);
    } else if (attribute.getName().equals(HANDLER_NAME.getName())) {
    // no-op just ignore the name attribute
    } else if (attribute.getName().equals(PROPERTIES.getName())) {
        final PropertyConfigurable propertyConfigurable;
        // A POJO configuration will have the same name as the handler
        final PojoConfiguration pojoConfiguration = logContextConfiguration.getPojoConfiguration(configuration.getName());
        if (pojoConfiguration == null) {
            propertyConfigurable = configuration;
        } else {
            propertyConfigurable = pojoConfiguration;
            // A log4j appender may be an OptionHandler which requires the invocation of activateOptions(). Setting
            // a dummy property on the Log4jAppenderHandler is required to invoke this method as all properties are
            // set on the POJO which is the actual appender
            configuration.setPropertyValueString(Log4jAppenderHandler.ACTIVATOR_PROPERTY_METHOD_NAME, "");
        }
        // undefined properties
        if (model.hasDefined(PROPERTIES.getName())) {
            final ModelNode resolvedValue = (resolveValue ? PROPERTIES.resolveModelAttribute(context, model) : model);
            for (Property property : resolvedValue.asPropertyList()) {
                propertyConfigurable.setPropertyValueString(property.getName(), property.getValue().asString());
            }
        }
    } else {
        if (attribute instanceof ConfigurationProperty) {
            // We need to set the file last in case it was set after the append for example. We'll use a new step
            // for this.
            final boolean addAsStep = (FILE.equals(attribute));
            @SuppressWarnings("unchecked") final ConfigurationProperty<String> configurationProperty = (ConfigurationProperty<String>) attribute;
            if (resolveValue) {
                if (addAsStep) {
                    context.addStep((c, m) -> {
                        configurationProperty.setPropertyValue(c, m, configuration);
                    }, Stage.RUNTIME);
                } else {
                    configurationProperty.setPropertyValue(context, model, configuration);
                }
            } else {
                // Get the resolver
                final ModelNodeResolver<String> resolver = configurationProperty.resolver();
                // Resolve the value
                final String resolvedValue = (resolver == null ? (model.isDefined() ? model.asString() : null) : resolver.resolveValue(context, model));
                if (resolvedValue == null) {
                    // Note that primitive attributes should use a default value as null is invalid
                    if (addAsStep) {
                        context.addStep((c, m) -> {
                            configuration.setPropertyValueString(configurationProperty.getPropertyName(), null);
                            configuration.removeProperty(configurationProperty.getPropertyName());
                        }, Stage.RUNTIME);
                    } else {
                        configuration.setPropertyValueString(configurationProperty.getPropertyName(), null);
                        configuration.removeProperty(configurationProperty.getPropertyName());
                    }
                } else {
                    if (addAsStep) {
                        context.addStep((c, m) -> configuration.setPropertyValueString(configurationProperty.getPropertyName(), resolvedValue), Stage.RUNTIME);
                    } else {
                        // Set the string value
                        configuration.setPropertyValueString(configurationProperty.getPropertyName(), resolvedValue);
                    }
                }
            }
        } else {
            LoggingLogger.ROOT_LOGGER.invalidPropertyAttribute(attribute.getName());
        }
    }
}
Also used : SyslogHandler(org.jboss.logmanager.handlers.SyslogHandler) ConfigurationProperty(org.jboss.as.logging.ConfigurationProperty) Resource(org.jboss.as.controller.registry.Resource) PropertyConfigurable(org.jboss.logmanager.config.PropertyConfigurable) SyslogHandler(org.jboss.logmanager.handlers.SyslogHandler) Log4jAppenderHandler(org.jboss.as.logging.logmanager.Log4jAppenderHandler) Handler(java.util.logging.Handler) AsyncHandler(org.jboss.logmanager.handlers.AsyncHandler) OperationStepHandler(org.jboss.as.controller.OperationStepHandler) FormatterConfiguration(org.jboss.logmanager.config.FormatterConfiguration) PojoConfiguration(org.jboss.logmanager.config.PojoConfiguration) PatternFormatter(org.jboss.logmanager.formatters.PatternFormatter) ModelNode(org.jboss.dmr.ModelNode) Property(org.jboss.dmr.Property) ConfigurationProperty(org.jboss.as.logging.ConfigurationProperty)

Example 3 with SyslogHandler

use of org.jboss.logmanager.handlers.SyslogHandler in project quarkus by quarkusio.

the class AsyncSyslogHandlerTest method asyncSyslogHandlerConfigurationTest.

@Test
public void asyncSyslogHandlerConfigurationTest() throws NullPointerException {
    Handler handler = getHandler(AsyncHandler.class);
    assertThat(handler.getLevel()).isEqualTo(Level.WARNING);
    AsyncHandler asyncHandler = (AsyncHandler) handler;
    assertThat(asyncHandler.getHandlers()).isNotEmpty();
    assertThat(asyncHandler.getQueueLength()).isEqualTo(256);
    assertThat(asyncHandler.getOverflowAction()).isEqualTo(AsyncHandler.OverflowAction.DISCARD);
    Handler nestedSyslogHandler = Arrays.stream(asyncHandler.getHandlers()).filter(h -> (h instanceof SyslogHandler)).findFirst().get();
    SyslogHandler syslogHandler = (SyslogHandler) nestedSyslogHandler;
    assertThat(syslogHandler.getPort()).isEqualTo(5144);
    assertThat(syslogHandler.getAppName()).isEqualTo("quarkus");
    assertThat(syslogHandler.getHostname()).isEqualTo("quarkus-test");
    assertThat(syslogHandler.getFacility()).isEqualTo(SyslogHandler.Facility.LOG_ALERT);
    assertThat(syslogHandler.getSyslogType()).isEqualTo(SyslogHandler.SyslogType.RFC3164);
    assertThat(syslogHandler.getProtocol()).isEqualTo(SyslogHandler.Protocol.UDP);
    assertThat(syslogHandler.isUseCountingFraming()).isEqualTo(true);
    assertThat(syslogHandler.isTruncate()).isEqualTo(false);
    assertThat(syslogHandler.isBlockOnReconnect()).isEqualTo(false);
}
Also used : SyslogHandler(org.jboss.logmanager.handlers.SyslogHandler) AsyncHandler(org.jboss.logmanager.handlers.AsyncHandler) SyslogHandler(org.jboss.logmanager.handlers.SyslogHandler) Handler(java.util.logging.Handler) LoggingTestsHelper.getHandler(io.quarkus.logging.LoggingTestsHelper.getHandler) AsyncHandler(org.jboss.logmanager.handlers.AsyncHandler) Test(org.junit.jupiter.api.Test) QuarkusUnitTest(io.quarkus.test.QuarkusUnitTest)

Example 4 with SyslogHandler

use of org.jboss.logmanager.handlers.SyslogHandler in project quarkus by quarkusio.

the class SyslogHandlerTest method syslogOutputTest.

@Test
public void syslogOutputTest() {
    Handler handler = getHandler(SyslogHandler.class);
    assertThat(handler.getLevel()).isEqualTo(Level.WARNING);
    Formatter formatter = handler.getFormatter();
    assertThat(formatter).isInstanceOf(PatternFormatter.class);
    PatternFormatter patternFormatter = (PatternFormatter) formatter;
    assertThat(patternFormatter.getPattern()).isEqualTo("%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%c{3.}] (%t) %s%e%n");
    SyslogHandler syslogHandler = (SyslogHandler) handler;
    assertThat(syslogHandler.getPort()).isEqualTo(5140);
    assertThat(syslogHandler.getAppName()).isEqualTo(getProcessName());
    assertThat(syslogHandler.getHostname()).isEqualTo(getQualifiedHostName());
    assertThat(syslogHandler.getFacility()).isEqualTo(SyslogHandler.Facility.USER_LEVEL);
    assertThat(syslogHandler.getSyslogType()).isEqualTo(SyslogHandler.SyslogType.RFC5424);
    assertThat(syslogHandler.getProtocol()).isEqualTo(SyslogHandler.Protocol.TCP);
    assertThat(syslogHandler.isUseCountingFraming()).isEqualTo(false);
    assertThat(syslogHandler.isTruncate()).isEqualTo(true);
    assertThat(syslogHandler.isBlockOnReconnect()).isEqualTo(false);
}
Also used : SyslogHandler(org.jboss.logmanager.handlers.SyslogHandler) PatternFormatter(org.jboss.logmanager.formatters.PatternFormatter) Formatter(java.util.logging.Formatter) SyslogHandler(org.jboss.logmanager.handlers.SyslogHandler) Handler(java.util.logging.Handler) LoggingTestsHelper.getHandler(io.quarkus.logging.LoggingTestsHelper.getHandler) PatternFormatter(org.jboss.logmanager.formatters.PatternFormatter) Test(org.junit.jupiter.api.Test) QuarkusUnitTest(io.quarkus.test.QuarkusUnitTest)

Example 5 with SyslogHandler

use of org.jboss.logmanager.handlers.SyslogHandler in project quarkus by quarkusio.

the class LoggingSetupRecorder method createNamedHandlers.

private static Map<String, Handler> createNamedHandlers(LogConfig config, ConsoleRuntimeConfig consoleRuntimeConfig, List<RuntimeValue<Optional<Formatter>>> possibleFormatters, ErrorManager errorManager, LogCleanupFilter cleanupFilter, LaunchMode launchMode) {
    Map<String, Handler> namedHandlers = new HashMap<>();
    for (Entry<String, ConsoleConfig> consoleConfigEntry : config.consoleHandlers.entrySet()) {
        ConsoleConfig namedConsoleConfig = consoleConfigEntry.getValue();
        if (!namedConsoleConfig.enable) {
            continue;
        }
        final Handler consoleHandler = configureConsoleHandler(namedConsoleConfig, consoleRuntimeConfig, errorManager, cleanupFilter, possibleFormatters, null, launchMode);
        addToNamedHandlers(namedHandlers, consoleHandler, consoleConfigEntry.getKey());
    }
    for (Entry<String, FileConfig> fileConfigEntry : config.fileHandlers.entrySet()) {
        FileConfig namedFileConfig = fileConfigEntry.getValue();
        if (!namedFileConfig.enable) {
            continue;
        }
        final Handler fileHandler = configureFileHandler(namedFileConfig, errorManager, cleanupFilter);
        addToNamedHandlers(namedHandlers, fileHandler, fileConfigEntry.getKey());
    }
    for (Entry<String, SyslogConfig> sysLogConfigEntry : config.syslogHandlers.entrySet()) {
        SyslogConfig namedSyslogConfig = sysLogConfigEntry.getValue();
        if (!namedSyslogConfig.enable) {
            continue;
        }
        final Handler syslogHandler = configureSyslogHandler(namedSyslogConfig, errorManager, cleanupFilter);
        if (syslogHandler != null) {
            addToNamedHandlers(namedHandlers, syslogHandler, sysLogConfigEntry.getKey());
        }
    }
    return namedHandlers;
}
Also used : HashMap(java.util.HashMap) SyslogHandler(org.jboss.logmanager.handlers.SyslogHandler) FileHandler(org.jboss.logmanager.handlers.FileHandler) PeriodicSizeRotatingFileHandler(org.jboss.logmanager.handlers.PeriodicSizeRotatingFileHandler) ConsoleHandler(org.jboss.logmanager.handlers.ConsoleHandler) SizeRotatingFileHandler(org.jboss.logmanager.handlers.SizeRotatingFileHandler) Handler(java.util.logging.Handler) AsyncHandler(org.jboss.logmanager.handlers.AsyncHandler)

Aggregations

SyslogHandler (org.jboss.logmanager.handlers.SyslogHandler)8 Handler (java.util.logging.Handler)5 AsyncHandler (org.jboss.logmanager.handlers.AsyncHandler)4 PatternFormatter (org.jboss.logmanager.formatters.PatternFormatter)3 LoggingTestsHelper.getHandler (io.quarkus.logging.LoggingTestsHelper.getHandler)2 QuarkusUnitTest (io.quarkus.test.QuarkusUnitTest)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 ConsoleHandler (org.jboss.logmanager.handlers.ConsoleHandler)2 FileHandler (org.jboss.logmanager.handlers.FileHandler)2 PeriodicSizeRotatingFileHandler (org.jboss.logmanager.handlers.PeriodicSizeRotatingFileHandler)2 SizeRotatingFileHandler (org.jboss.logmanager.handlers.SizeRotatingFileHandler)2 Test (org.junit.jupiter.api.Test)2 FileInputStream (java.io.FileInputStream)1 PortUnreachableException (java.net.PortUnreachableException)1 KeyStore (java.security.KeyStore)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Optional (java.util.Optional)1 ErrorManager (java.util.logging.ErrorManager)1