I have a java package with specialized operations. Specialized in the sense that they are rarely used and i don't want to have them be mixed with normal logging.
I know that adding logging.file=myapplication.log
will redirect the logging to this file but is there a way to specify only logging from specific packages to another file? Like logging.file.my.package=special.log
?
Spring uses Logback as default logger. According to the official doc you can setup logback.xml yourself to add to the default logging mechanism your 'special' behavior:
Here we use Spring default
FILE
andCONSOLE
appenders to log app info as usual (exceptlogging.file.my.package
), and useSPECIAL_FILE
appender to log info from this package to filelog/special.log
.It's not possible with the logging configuration Spring Boot provides. However, you can fall back upon the configuration that the logging framework provides. By default, this is Logback, which can be configured to log to multiple logging files.
To do that, you need to add a logback.xml file to your classpath and configure multiple appenders. For example:
And now you can define a separate logger in case you want to log to a different file. Make sure to add the
additivity="false"
otherwise, the log message will still be printed in both log files:In this case, all logs will be written to
log1.log
(FILE1
appender), while logs from the packagecom.example.apps.special-package
will be written tolog2.log
(FILE2
appender).