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);
}
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);
}
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;
}
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;
}
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;
}
Aggregations