Search in sources :

Example 1 with Logger

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

the class HandlerOperationsTestCase method testFormatsNoColor.

@Test
public void testFormatsNoColor() throws Exception {
    final Path logFile = LoggingTestEnvironment.get().getLogDir().resolve("formatter.log");
    // Delete the file if it exists
    Files.deleteIfExists(logFile);
    // Create a file handler
    final String fileHandlerName = "formatter-handler";
    final ModelNode handlerAddress = createFileHandlerAddress(fileHandlerName).toModelNode();
    ModelNode op = SubsystemOperations.createAddOperation(handlerAddress);
    op.get(CommonAttributes.LEVEL.getName()).set("INFO");
    op.get(CommonAttributes.ENCODING.getName()).set(ENCODING);
    op.get(CommonAttributes.FILE.getName()).get(PathResourceDefinition.PATH.getName()).set(logFile.toAbsolutePath().toString());
    op.get(CommonAttributes.AUTOFLUSH.getName()).set(true);
    op.get(FileHandlerResourceDefinition.FORMATTER.getName()).set("%s%n");
    executeOperation(kernelServices, op);
    // Create a logger
    final Logger logger = LogContext.getSystemLogContext().getLogger(HandlerOperationsTestCase.class.getName());
    final ModelNode loggerAddress = createLoggerAddress(logger.getName()).toModelNode();
    op = SubsystemOperations.createAddOperation(loggerAddress);
    op.get(LoggerResourceDefinition.USE_PARENT_HANDLERS.getName()).set(false);
    op.get(LoggerAttributes.HANDLERS.getName()).setEmptyList().add(fileHandlerName);
    executeOperation(kernelServices, op);
    // Log a few records
    logger.log(Level.INFO, "Test message 1");
    logger.log(Level.INFO, "Test message 2");
    // Read the file
    List<String> lines = Files.readAllLines(logFile, StandardCharsets.UTF_8);
    assertEquals("Number of lines logged and found in the file do not match", 2, lines.size());
    // Check the lines
    assertEquals("Test message 1", lines.get(0));
    assertEquals("Test message 2", lines.get(1));
    // Create a pattern formatter
    final ModelNode patternFormatterAddress = createPatternFormatterAddress("PATTERN").toModelNode();
    op = SubsystemOperations.createAddOperation(patternFormatterAddress);
    op.get(PatternFormatterResourceDefinition.PATTERN.getName()).set("[changed-pattern] %s%n");
    executeOperation(kernelServices, op);
    // The formatter will need to be undefined before the named-formatter can be written
    executeOperation(kernelServices, SubsystemOperations.createUndefineAttributeOperation(handlerAddress, FileHandlerResourceDefinition.FORMATTER));
    // Assign the pattern to the handler
    executeOperation(kernelServices, SubsystemOperations.createWriteAttributeOperation(handlerAddress, FileHandlerResourceDefinition.NAMED_FORMATTER, "PATTERN"));
    // Check that the formatter attribute was undefined
    op = SubsystemOperations.createReadAttributeOperation(handlerAddress, FileHandlerResourceDefinition.FORMATTER);
    op.get("include-defaults").set(false);
    ModelNode result = executeOperation(kernelServices, op);
    assertFalse("formatter attribute was not undefined after the change to a named-formatter", SubsystemOperations.readResult(result).isDefined());
    // Log some more records
    logger.log(Level.INFO, "Test message 3");
    logger.log(Level.INFO, "Test message 4");
    // Read the file
    lines = Files.readAllLines(logFile, StandardCharsets.UTF_8);
    assertEquals("Number of lines logged and found in the file do not match", 4, lines.size());
    // Check the lines
    assertTrue("Line logged does not match expected: 3", Arrays.equals("[changed-pattern] Test message 3".getBytes(ENCODING), lines.get(2).getBytes(ENCODING)));
    // Second line will start with the clear string, followed by the color string
    assertTrue("Line logged does not match expected: 4", Arrays.equals("[changed-pattern] Test message 4".getBytes(ENCODING), lines.get(3).getBytes(ENCODING)));
    // Remove the handler operation
    final ModelNode removeHandlerOp = SubsystemOperations.createOperation("remove-handler", loggerAddress);
    removeHandlerOp.get("name").set(fileHandlerName);
    // Finally clean everything up
    op = SubsystemOperations.CompositeOperationBuilder.create().addStep(removeHandlerOp).addStep(SubsystemOperations.createRemoveOperation(handlerAddress)).addStep(SubsystemOperations.createRemoveOperation(patternFormatterAddress)).addStep(SubsystemOperations.createRemoveOperation(loggerAddress)).build().getOperation();
    executeOperation(kernelServices, op);
}
Also used : Path(java.nio.file.Path) ModelNode(org.jboss.dmr.ModelNode) Logger(org.jboss.logmanager.Logger) Test(org.junit.Test)

Example 2 with Logger

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

the class LoggingOperationsSubsystemTestCase method testPatternFormatter.

private void testPatternFormatter(final String profileName) throws Exception {
    final String fileHandlerName = "test-file-handler";
    final Path logFile = createLogFile();
    // Add file handler
    final ModelNode handlerAddress = addFileHandler(kernelServices, profileName, fileHandlerName, org.jboss.logmanager.Level.INFO, logFile, false);
    // Get the logger
    final Logger logger = getLogger(profileName);
    // Create the logger
    final ModelNode loggerAddress = createLoggerAddress(profileName, logger.getName()).toModelNode();
    ModelNode op = SubsystemOperations.createAddOperation(loggerAddress);
    op.get(LoggerResourceDefinition.USE_PARENT_HANDLERS.getName()).set(false);
    op.get(LoggerAttributes.HANDLERS.getName()).setEmptyList().add(fileHandlerName);
    executeOperation(kernelServices, op);
    // Create a pattern formatter
    final ModelNode patternFormatterAddress = createPatternFormatterAddress(profileName, "PATTERN").toModelNode();
    op = SubsystemOperations.createAddOperation(patternFormatterAddress);
    // Add a format that can be read back to make sure it matches the pattern used in the handler
    op.get(PatternFormatterResourceDefinition.PATTERN.getName()).set("[NAMED-PATTERN] %s%n");
    executeOperation(kernelServices, op);
    // Add the named formatter to the handler
    op = SubsystemOperations.createWriteAttributeOperation(handlerAddress, FileHandlerResourceDefinition.NAMED_FORMATTER, "PATTERN");
    executeOperation(kernelServices, op);
    // Log 3 lines
    logger.info("Test message 1");
    logger.info("Test message 2");
    logger.info("Test message 3");
    // Check the file, should only contain 3 lines
    final List<String> lines = Files.readAllLines(logFile, StandardCharsets.UTF_8);
    assertEquals("Additional messages written to handler that should not be there.", 3, lines.size());
    // Check each line
    assertEquals("Line patterns don't match.", "[NAMED-PATTERN] Test message 1", lines.get(0));
    assertEquals("Line patterns don't match.", "[NAMED-PATTERN] Test message 2", lines.get(1));
    assertEquals("Line patterns don't match.", "[NAMED-PATTERN] Test message 3", lines.get(2));
    // Clean up
    op = SubsystemOperations.CompositeOperationBuilder.create().addStep(SubsystemOperations.createRemoveOperation(loggerAddress)).addStep(SubsystemOperations.createRemoveOperation(handlerAddress)).addStep(SubsystemOperations.createRemoveOperation(patternFormatterAddress)).build().getOperation();
    executeOperation(kernelServices, op);
}
Also used : Path(java.nio.file.Path) ModelNode(org.jboss.dmr.ModelNode) Logger(org.jboss.logmanager.Logger)

Example 3 with Logger

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

the class LogContextStdioContextSelector method getStdioContext.

@Override
public StdioContext getStdioContext() {
    final LogContext logContext = LogContext.getLogContext();
    final Logger root = logContext.getLogger(CommonAttributes.ROOT_LOGGER_NAME);
    StdioContext stdioContext = root.getAttachment(STDIO_CONTEXT_ATTACHMENT_KEY);
    if (stdioContext == null) {
        // Create the StdioContext
        stdioContext = createContext(logContext);
        final StdioContext appearing = attachIfAbsent(root, stdioContext);
        if (appearing != null) {
            stdioContext = appearing;
        }
    }
    return stdioContext;
}
Also used : LogContext(org.jboss.logmanager.LogContext) StdioContext(org.jboss.stdio.StdioContext) Logger(org.jboss.logmanager.Logger)

Example 4 with Logger

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

the class ConfigurationPersistence method getOrCreateConfigurationPersistence.

/**
 * Gets the property configurator. If the {@link ConfigurationPersistence} does not exist a new one is created.
 *
 * @param logContext the log context used to find the property configurator or to attach it to.
 *
 * @return the property configurator
 */
public static ConfigurationPersistence getOrCreateConfigurationPersistence(final LogContext logContext) {
    final Logger root = logContext.getLogger(CommonAttributes.ROOT_LOGGER_NAME);
    final ConfigurationPersistence result;
    synchronized (LOCK) {
        Configurator configurator = root.getAttachment(Configurator.ATTACHMENT_KEY);
        if (configurator == null) {
            configurator = new ConfigurationPersistence(logContext);
            Configurator existing = root.attachIfAbsent(Configurator.ATTACHMENT_KEY, configurator);
            if (existing != null) {
                configurator = existing;
            }
        }
        if (configurator instanceof ConfigurationPersistence) {
            // We have the correct configurator
            result = (ConfigurationPersistence) configurator;
        } else if (configurator instanceof PropertyConfigurator) {
            // Create a new configurator delegating to the configurator found
            result = new ConfigurationPersistence((PropertyConfigurator) configurator);
            root.attach(Configurator.ATTACHMENT_KEY, result);
        } else {
            // An unknown configurator, log a warning and replace
            LoggingLogger.ROOT_LOGGER.replacingConfigurator(configurator);
            result = new ConfigurationPersistence(logContext);
            root.attach(Configurator.ATTACHMENT_KEY, result);
        }
    }
    return result;
}
Also used : PropertyConfigurator(org.jboss.logmanager.PropertyConfigurator) Configurator(org.jboss.logmanager.Configurator) Logger(org.jboss.logmanager.Logger) LoggingLogger(org.jboss.as.logging.logging.LoggingLogger) PropertyConfigurator(org.jboss.logmanager.PropertyConfigurator)

Example 5 with Logger

use of org.jboss.logmanager.Logger in project quarkus by quarkusio.

the class LogController method getLogger.

public static Json.JsonObjectBuilder getLogger(String loggerName) {
    LogContext logContext = LogContext.getLogContext();
    if (loggerName != null && !loggerName.isEmpty()) {
        Logger logger = logContext.getLogger(loggerName);
        Json.JsonObjectBuilder jsonObject = Json.object();
        jsonObject.put("name", loggerName);
        jsonObject.put("effectiveLevel", getEffectiveLogLevel(logger));
        jsonObject.put("configuredLevel", getConfiguredLogLevel(logger));
        return jsonObject;
    }
    return null;
}
Also used : LogContext(org.jboss.logmanager.LogContext) Json(io.quarkus.vertx.http.runtime.devmode.Json) Logger(org.jboss.logmanager.Logger)

Aggregations

Logger (org.jboss.logmanager.Logger)11 LogContext (org.jboss.logmanager.LogContext)6 Path (java.nio.file.Path)3 Handler (java.util.logging.Handler)3 Level (java.util.logging.Level)3 ModelNode (org.jboss.dmr.ModelNode)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ErrorManager (java.util.logging.ErrorManager)2 LoggingLogger (org.jboss.as.logging.logging.LoggingLogger)2 Configurator (org.jboss.logmanager.Configurator)2 PropertyConfigurator (org.jboss.logmanager.PropertyConfigurator)2 OnlyOnceErrorManager (org.jboss.logmanager.errormanager.OnlyOnceErrorManager)2 AsyncHandler (org.jboss.logmanager.handlers.AsyncHandler)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 SyslogHandler (org.jboss.logmanager.handlers.SyslogHandler)2