コード例 #1
0
  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));
  }
コード例 #2
0
  private void testDisableHandler(final String profileName, boolean legacy) throws Exception {
    final KernelServices kernelServices = boot();
    final String fileHandlerName = "test-file-handler";

    final File logFile = createLogFile();

    // Add file handler
    addFileHandler(
        kernelServices,
        profileName,
        fileHandlerName,
        org.jboss.logmanager.Level.INFO,
        logFile,
        true);

    // Ensure the handler is listed
    final ModelNode rootLoggerAddress = createRootLoggerAddress(profileName).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));

    // Get the logger
    final Logger logger = getLogger(profileName);

    // Log 3 lines
    logger.info("Test message 1");
    logger.info("Test message 2");
    logger.info("Test message 3");

    // Disable the handler
    final ModelNode handlerAddress =
        createFileHandlerAddress(profileName, fileHandlerName).toModelNode();
    ModelNode disableOp =
        legacy
            ? Util.getEmptyOperation(
                AbstractHandlerDefinition.DISABLE_HANDLER.getName(), handlerAddress)
            : SubsystemOperations.createWriteAttributeOperation(
                handlerAddress, CommonAttributes.ENABLED, false);
    executeOperation(kernelServices, disableOp);

    // The operation should set the enabled attribute to false
    final ModelNode readOp =
        SubsystemOperations.createReadAttributeOperation(handlerAddress, CommonAttributes.ENABLED);
    ModelNode result = executeOperation(kernelServices, readOp);
    assertFalse(
        "enabled attribute should be false when the disable operation is invoked",
        SubsystemOperations.readResult(result).asBoolean());

    // Log 3 more lines
    logger.info("Test message 4");
    logger.info("Test message 5");
    logger.info("Test message 6");

    // Check the file, should only contain 3 lines
    List<String> lines = FileUtils.readLines(logFile);
    assertEquals("Handler was not disable.", 3, lines.size());

    // Re-enable the handler
    ModelNode enableOp =
        legacy
            ? Util.getEmptyOperation(
                AbstractHandlerDefinition.ENABLE_HANDLER.getName(), handlerAddress)
            : SubsystemOperations.createWriteAttributeOperation(
                handlerAddress, CommonAttributes.ENABLED, true);
    executeOperation(kernelServices, enableOp);

    // The operation should set the enabled attribute to true
    result = executeOperation(kernelServices, readOp);
    assertTrue(
        "enabled attribute should be true when the enable operation is invoked",
        SubsystemOperations.readResult(result).asBoolean());

    // Log 3 more lines
    logger.info("Test message 7");
    logger.info("Test message 8");
    logger.info("Test message 9");

    // Check the file, should contain 6 lines
    lines = FileUtils.readLines(logFile);
    assertEquals("Handler was not disable.", 6, lines.size());
  }