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 ModelNode addFileHandler( final KernelServices kernelServices, final String loggingProfile, final String name, final Level level, final File file, final boolean assign) throws Exception { final ModelNode address = createFileHandlerAddress(loggingProfile, name).toModelNode(); // add file handler ModelNode op = SubsystemOperations.createAddOperation(address); op.get(CommonAttributes.NAME.getName()).set(name); op.get(CommonAttributes.LEVEL.getName()).set(level.getName()); op.get(CommonAttributes.FILE.getName()) .get(PathResourceDefinition.PATH.getName()) .set(file.getAbsolutePath()); op.get(CommonAttributes.AUTOFLUSH.getName()).set(true); executeOperation(kernelServices, op); // register it with root logger if (assign) { op = SubsystemOperations.createOperation( RootLoggerResourceDefinition.ROOT_LOGGER_ADD_HANDLER_OPERATION_NAME, createRootLoggerAddress(loggingProfile).toModelNode()); op.get(CommonAttributes.NAME.getName()).set(name); executeOperation(kernelServices, op); } return address; }