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; }
private void testAddRemoveFileHandler(final String loggingProfile) throws Exception { final KernelServices kernelServices = boot(); final String fileHandlerName = "test-file-handler"; File logFile = createLogFile(); // Add file handler addFileHandler( kernelServices, loggingProfile, fileHandlerName, org.jboss.logmanager.Level.INFO, logFile, true); // Ensure the handler is listed final ModelNode rootLoggerAddress = createRootLoggerAddress(loggingProfile).toModelNode(); ModelNode op = SubsystemOperations.createReadAttributeOperation( rootLoggerAddress, CommonAttributes.HANDLERS); ModelNode handlerResult = executeOperation(kernelServices, op); List<String> handlerList = SubsystemOperations.readResultAsList(handlerResult); assertTrue( String.format("Handler '%s' was not found. Result: %s", fileHandlerName, handlerResult), handlerList.contains(fileHandlerName)); doLog(loggingProfile, LEVELS, "Test123"); // Remove handler from logger op = SubsystemOperations.createOperation( RootLoggerResourceDefinition.ROOT_LOGGER_REMOVE_HANDLER_OPERATION_NAME, rootLoggerAddress); op.get(CommonAttributes.NAME.getName()).set(fileHandlerName); executeOperation(kernelServices, op); // Ensure the handler is not listed op = SubsystemOperations.createReadAttributeOperation( rootLoggerAddress, CommonAttributes.HANDLERS); handlerResult = executeOperation(kernelServices, op); handlerList = SubsystemOperations.readResultAsList(handlerResult); assertFalse( String.format("Handler '%s' was not removed. Result: %s", fileHandlerName, handlerResult), handlerList.contains(fileHandlerName)); // Remove the handler removeFileHandler(kernelServices, loggingProfile, fileHandlerName, false); // check generated log file assertTrue(FileUtils.readFileToString(logFile).contains("Test123")); // verify that the logger is stopped, no more logs are comming to the file long checksum = FileUtils.checksumCRC32(logFile); doLog(loggingProfile, LEVELS, "Test123"); assertEquals(checksum, FileUtils.checksumCRC32(logFile)); }
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); }