0

I have configured the Logging fraction and tried to add an additional handler to store specific logs in a different files, using a category, by looking to the answer in How to log application auditing to separate file on Wildfly 8 but adapting to the Wildfly-Swarm fluent API.

The code looks like this:

LoggingFraction loggingFraction = new LoggingFraction()
            .consoleHandler(level, "COLOR_PATTERN")
            .formatter("PATTERN", "%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%t] (%c{1}) %s%e%n")
            .formatter("COLOR_PATTERN", "%K{level}%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%t] (%c{1}) %s%e%n")
            .formatter("AUDIT", "%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p (%c{1}) %s%e%n")
            .periodicSizeRotatingFileHandler("FILE", h ->{
                h.level(level)
                        .namedFormatter("PATTERN")
                        .append(true)
                        .suffix(".yyyy-MM-dd")
                        .rotateSize(maxSize)
                        .enabled(true)
                        .encoding("UTF-8")
                        .maxBackupIndex(maxFiles);
                Map<String,String> fileSpec = new HashMap<>();
                fileSpec.put("path", getLogsDirectory() + "/" + "application.log");
                h.file(fileSpec);
            })
            .periodicSizeRotatingFileHandler("FILE_AUDIT_HANDLER", h ->{
                h.level(level)
                        .namedFormatter("AUDIT")
                        .append(true)
                        .suffix(".yyyy-MM-dd")
                        .rotateSize(maxSize)
                        .enabled(true)
                        .encoding("UTF-8")
                        .maxBackupIndex(maxFiles);
                        Map<String,String> fileSpec = new HashMap<>();
                        fileSpec.put("path", getLogsDirectory() + "/" + "application-audit.log");
                        h.file(fileSpec);
            })
            .rootLogger(l -> {
                l.level(level)
                        .handler("CONSOLE")
                        .handler("FILE")
                        ;
            })
            .logger("FILE_AUDIT", l -> {
                l.level(level)
                .category("com.company.app.webservice")
                .level(Level.INFO)
                .handler("FILE_AUDIT_HANDLER")
                ;
            })
            ;

Then I created a normal Logger in the code to add a log, like this:

private static final Logger LOGGER_AUDIT = LoggerFactory.getLogger("com.company.app.webservice");
...
LOGGER_AUDIT.info("Testing audit log")

But it doesn't work.
I'm assuming the category name only needs to match Logger name, in a way that the Logger name 'starts with' the category name, then it will be included. Am I right?
I don't know if there's something wrong in my configuration or if the Logger is not supposed to be used like that.

xsilmarx
  • 729
  • 5
  • 22

1 Answers1

1

The category attribute is a bit of a legacy attribute. The logger name is really the category if that makes sense. In your example the above the logger would be named FILE_AUDIT which means you'd it would match a Logger.getLogger("FILE_AUDIT").

Something like the following is probably what you want.

LoggingFraction loggingFraction = new LoggingFraction()
            .consoleHandler(level, "COLOR_PATTERN")
            .formatter("PATTERN", "%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%t] (%c{1}) %s%e%n")
            .formatter("COLOR_PATTERN", "%K{level}%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%t] (%c{1}) %s%e%n")
            .formatter("AUDIT", "%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p (%c{1}) %s%e%n")
            .periodicSizeRotatingFileHandler("FILE", h ->{
                h.level(level)
                        .namedFormatter("PATTERN")
                        .append(true)
                        .suffix(".yyyy-MM-dd")
                        .rotateSize(maxSize)
                        .enabled(true)
                        .encoding("UTF-8")
                        .maxBackupIndex(maxFiles);
                Map<String,String> fileSpec = new HashMap<>();
                fileSpec.put("path", getLogsDirectory() + "/" + "application.log");
                h.file(fileSpec);
            })
            .periodicSizeRotatingFileHandler("FILE_AUDIT_HANDLER", h ->{
                h.level(level)
                        .namedFormatter("AUDIT")
                        .append(true)
                        .suffix(".yyyy-MM-dd")
                        .rotateSize(maxSize)
                        .enabled(true)
                        .encoding("UTF-8")
                        .maxBackupIndex(maxFiles);
                        Map<String,String> fileSpec = new HashMap<>();
                        fileSpec.put("path", getLogsDirectory() + "/" + "application-audit.log");
                        h.file(fileSpec);
            })
            .rootLogger(l -> {
                l.level(level)
                        .handler("CONSOLE")
                        .handler("FILE")
                        ;
            })
            .logger("com.company.app.webservice", l -> {
                l.level(level)
                .level(Level.INFO)
                .handler("FILE_AUDIT_HANDLER")
                ;
            })
            ;

Note the only real change is removing the category (it's a read-only attribute) and changing the FILE_AUDIT name to com.company.app.webservice.

James R. Perkins
  • 16,800
  • 44
  • 60