private void testSetRootLogger(final String loggingProfile) throws Exception { final KernelServices kernelServices = boot(); final String fileHandlerName = "test-file-handler"; // Add new file logger so we can test root logger change final File logFile = createLogFile(); addFileHandler( kernelServices, loggingProfile, fileHandlerName, org.jboss.logmanager.Level.INFO, logFile, false); // Read root logger final ModelNode rootLoggerAddress = createRootLoggerAddress(loggingProfile).toModelNode(); ModelNode op = SubsystemOperations.createOperation( ClientConstants.READ_RESOURCE_OPERATION, rootLoggerAddress); final ModelNode rootLoggerResult = executeOperation(kernelServices, op); final List<String> handlers = modelNodeAsStringList(rootLoggerResult.get(CommonAttributes.HANDLERS.getName())); // Remove the root logger op = SubsystemOperations.createRemoveOperation(rootLoggerAddress); executeOperation(kernelServices, op); // Set a new root logger op = SubsystemOperations.createOperation(ModelDescriptionConstants.ADD, rootLoggerAddress); op.get(CommonAttributes.LEVEL.getName()) .set(rootLoggerResult.get(CommonAttributes.LEVEL.getName())); for (String handler : handlers) op.get(CommonAttributes.HANDLERS.getName()).add(handler); op.get(CommonAttributes.HANDLERS.getName()).add(fileHandlerName); executeOperation(kernelServices, op); doLog(loggingProfile, LEVELS, "Test123"); // Remove the root logger op = SubsystemOperations.createRemoveOperation(rootLoggerAddress); executeOperation(kernelServices, op); // Revert root logger op = SubsystemOperations.createOperation(ModelDescriptionConstants.ADD, rootLoggerAddress); op.get(CommonAttributes.LEVEL.getName()) .set(rootLoggerResult.get(CommonAttributes.LEVEL.getName())); op.get(CommonAttributes.HANDLERS.getName()) .set(rootLoggerResult.get(CommonAttributes.HANDLERS.getName())); executeOperation(kernelServices, op); // remove file handler removeFileHandler(kernelServices, loggingProfile, fileHandlerName, false); // check that root logger were changed - file logger was registered String log = FileUtils.readFileToString(logFile); assertTrue(log.contains("Test123")); }
private void removeFileHandler( final KernelServices kernelServices, final String loggingProfile, final String name, final boolean unassign) throws Exception { if (unassign) { // Remove the handler from the logger final ModelNode op = SubsystemOperations.createOperation( RootLoggerResourceDefinition.ROOT_LOGGER_REMOVE_HANDLER_OPERATION_NAME, createRootLoggerAddress(loggingProfile).toModelNode()); op.get(CommonAttributes.NAME.getName()).set(name); executeOperation(kernelServices, op); } // Remove the handler final ModelNode op = SubsystemOperations.createRemoveOperation( createFileHandlerAddress(loggingProfile, name).toModelNode()); executeOperation(kernelServices, op); }
private void testPatternFormatter(final String profileName) throws Exception { final KernelServices kernelServices = boot(); final String fileHandlerName = "test-file-handler"; final File 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(CommonAttributes.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 = FileUtils.readLines(logFile); 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); }
@Test public void testLegacyFilters() throws Exception { final KernelServices kernelServices = boot(); final String fileHandlerName = "test-file-handler"; // add new file logger so we can track logged messages final File logFile = createLogFile(); final ModelNode handlerAddress = createFileHandlerAddress(fileHandlerName).toModelNode(); addFileHandler( kernelServices, null, fileHandlerName, org.jboss.logmanager.Level.TRACE, logFile, true); // Write legacy filters for (Map.Entry<String, ModelNode> entry : FilterConversionTestCase.MAP.entrySet()) { // Validate the write-attribute operation ModelNode op = SubsystemOperations.createWriteAttributeOperation( handlerAddress, CommonAttributes.FILTER, entry.getValue()); executeOperation(kernelServices, op); // Read the current value op = SubsystemOperations.createReadAttributeOperation( handlerAddress, CommonAttributes.FILTER_SPEC); String filterSpecResult = SubsystemOperations.readResultAsString(executeOperation(kernelServices, op)); assertEquals(entry.getKey(), filterSpecResult); // Validate an add operation final ModelNode tempHandlerAddress = createConsoleHandlerAddress("temp").toModelNode(); op = SubsystemOperations.createAddOperation(tempHandlerAddress); op.get(CommonAttributes.FILTER.getName()).set(entry.getValue()); executeOperation(kernelServices, op); // Read the current value op = SubsystemOperations.createReadAttributeOperation( tempHandlerAddress, CommonAttributes.FILTER_SPEC); filterSpecResult = SubsystemOperations.readResultAsString(executeOperation(kernelServices, op)); assertEquals(entry.getKey(), filterSpecResult); // Remove the temp handler op = SubsystemOperations.createRemoveOperation(tempHandlerAddress, true); executeOperation(kernelServices, op); // Add to a logger final ModelNode loggerAddress = createLoggerAddress("test-logger").toModelNode(); op = SubsystemOperations.createAddOperation(loggerAddress); op.get(CommonAttributes.FILTER.getName()).set(entry.getValue()); executeOperation(kernelServices, op); // Read the current value op = SubsystemOperations.createReadAttributeOperation( loggerAddress, CommonAttributes.FILTER_SPEC); filterSpecResult = SubsystemOperations.readResultAsString(executeOperation(kernelServices, op)); assertEquals(entry.getKey(), filterSpecResult); // Remove the attribute op = SubsystemOperations.createUndefineAttributeOperation( loggerAddress, CommonAttributes.FILTER_SPEC); executeOperation(kernelServices, op); op = SubsystemOperations.createReadAttributeOperation(loggerAddress, CommonAttributes.FILTER); // Filter and filter spec should be undefined assertEquals( "Filter was not undefined", SubsystemOperations.UNDEFINED, SubsystemOperations.readResult(executeOperation(kernelServices, op))); op = SubsystemOperations.createReadAttributeOperation( loggerAddress, CommonAttributes.FILTER_SPEC); assertEquals( "Filter was not undefined", SubsystemOperations.UNDEFINED, SubsystemOperations.readResult(executeOperation(kernelServices, op))); // Test writing the attribute to the logger op = SubsystemOperations.createWriteAttributeOperation( loggerAddress, CommonAttributes.FILTER, entry.getValue()); executeOperation(kernelServices, op); // Read the current value op = SubsystemOperations.createReadAttributeOperation( loggerAddress, CommonAttributes.FILTER_SPEC); filterSpecResult = SubsystemOperations.readResultAsString(executeOperation(kernelServices, op)); assertEquals(entry.getKey(), filterSpecResult); // Remove the logger op = SubsystemOperations.createRemoveOperation(loggerAddress, true); executeOperation(kernelServices, op); } // Write new filters for (Map.Entry<String, ModelNode> entry : FilterConversionTestCase.MAP.entrySet()) { // Write to a handler ModelNode op = SubsystemOperations.createWriteAttributeOperation( handlerAddress, CommonAttributes.FILTER_SPEC, entry.getKey()); executeOperation(kernelServices, op); // Read the current value op = SubsystemOperations.createReadAttributeOperation(handlerAddress, CommonAttributes.FILTER); ModelNode filterResult = SubsystemOperations.readResult(executeOperation(kernelServices, op)); ModelTestUtils.compare(entry.getValue(), filterResult); // Validate an add operation final ModelNode tempHandlerAddress = createConsoleHandlerAddress("temp").toModelNode(); op = SubsystemOperations.createAddOperation(tempHandlerAddress); op.get(CommonAttributes.FILTER_SPEC.getName()).set(entry.getKey()); executeOperation(kernelServices, op); // Read the current value op = SubsystemOperations.createReadAttributeOperation( tempHandlerAddress, CommonAttributes.FILTER); filterResult = SubsystemOperations.readResult(executeOperation(kernelServices, op)); ModelTestUtils.compare(entry.getValue(), filterResult); // Remove the temp handler op = SubsystemOperations.createRemoveOperation(tempHandlerAddress, true); executeOperation(kernelServices, op); // Add to a logger final ModelNode loggerAddress = createLoggerAddress("test-logger").toModelNode(); op = SubsystemOperations.createAddOperation(loggerAddress); op.get(CommonAttributes.FILTER_SPEC.getName()).set(entry.getKey()); executeOperation(kernelServices, op); // Read the current value op = SubsystemOperations.createReadAttributeOperation(loggerAddress, CommonAttributes.FILTER); filterResult = SubsystemOperations.readResult(executeOperation(kernelServices, op)); ModelTestUtils.compare(entry.getValue(), filterResult); // Test writing the attribute to the logger op = SubsystemOperations.createWriteAttributeOperation( loggerAddress, CommonAttributes.FILTER_SPEC, entry.getKey()); executeOperation(kernelServices, op); // Read the current value op = SubsystemOperations.createReadAttributeOperation(loggerAddress, CommonAttributes.FILTER); filterResult = SubsystemOperations.readResult(executeOperation(kernelServices, op)); ModelTestUtils.compare(entry.getValue(), filterResult); // Remove the logger op = SubsystemOperations.createRemoveOperation(loggerAddress, true); executeOperation(kernelServices, op); } removeFileHandler(kernelServices, null, fileHandlerName, true); }