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.