3 Replies Latest reply on Aug 3, 2017 5:24 PM by James Perkins

    Add new loggers in runtime JBOSS EAP 6.4 from Java class

    Suhas Talele Newbie

      Hello,

       

      We are using JBOSS EAP 6.4 version with logging periodic-rotating-file-handler configuration in standalone.xml file. In production environment, we have disabled info logs and just printing WARN logs. But in some cases, we need to enable INFO logs it at runtime via Java code. For this, we tried with Log4j APIs and on console its correctly printing INFO logs even when INFO logs disabled. but its not writing that logs in INFO file log.

       

      Please advice on how we can enable and disable INFO logs in JBOSS EAP 6.4 from Java class at runtime.

       

      Thanks in advance!

       

      Suhas Talele

        • 1. Re: Add new loggers in runtime JBOSS EAP 6.4 from Java class
          James Perkins Master

          If you want changes at runtime it's best to not use the log4j API's. You can do this with management operations quite easily.

           

          The first question I have though is it really necessary to have this done in Java code? It's a lot easier to do with CLI, the web console or the HTTP management interface.

           

          --

          James R. Perkins

          • 2. Re: Add new loggers in runtime JBOSS EAP 6.4 from Java class
            Suhas Talele Newbie

            Hello James,

             

            Thanks for your reply. We have to do this run time without using CLI, the web console or the HTTP management interface. So is there any way, we can enable or disable it from java code at run time.

             

            Thanks,

             

            Suhas Talele

            • 3. Re: Add new loggers in runtime JBOSS EAP 6.4 from Java class
              James Perkins Master

              The best approach would be to use the management API if you don't want to use one of the other approaches.

              try (ModelControllerClient client = ModelControllerClient.Factory.create(InetAddress.getLocalHost(), 9990)) {
                  // Create the address for the org.jboss.as.logging logger
                  final ModelNode address = Operations.createAddress("subsystem", "logging", "logger", "org.jboss.as.logging");
                  // Create the add operation
                  ModelNode op = Operations.createAddOperation(address);
                  op.get("level").set("DEBUG");
                  ModelNode result = client.execute(op);
                  if (!Operations.isSuccessfulOutcome(result)) {
                      throw new RuntimeException(Operations.getFailureDescription(result).asString());
                  }
                  // Change the console to show DEBUG logging
                  final ModelNode consoleHandlerAddress = Operations.createAddress("subsystem", "logging", "console-handler", "CONSOLE");
                  op = Operations.createWriteAttributeOperation(consoleHandlerAddress, "level", "DEBUG");
                  result = client.execute(op);
                  if (!Operations.isSuccessfulOutcome(result)) {
                      throw new RuntimeException(Operations.getFailureDescription(result).asString());
                  }
              }
              

              --

              James R. Perkins