0

My web application uses correctly slf4j, but I have a method that generates a considerable amount of log, so I would like this method to have its own logger to write on a separate file, or tell the main logger to write to a different file.

How could I accomplish that?

This is my log4j.properties:

################################################################################
#### Configurazione log root
################################################################################
log4j.rootCategory=debug, stdout, file
log4j.category.org.apache=info
log4j.category.org.hibernate=info
log4j.category.com.mchange=warn
log4j.category.org.springframework=info
################################################################################
#### Appender per console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss} - %-5p - (%F:%L) - %m%n
################################################################################
#### Appender su file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=pathToLog.log
log4j.appender.file.MaxFileSize=5000KB
log4j.appender.file.MaxBackupIndex=5
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss} - %-5p - (%F:%L) - %m%n

Thanks in advance

lateralus
  • 1,020
  • 1
  • 12
  • 35

1 Answers1

0

You can create a custom logger for your specific method, and use only that logger inside the method:

Logger customLogger = Logger.getLogger("customLoggerName");

Then if you want to control that specific logger's log level, you can do that for customLoggerName as you control all the others (ie. instead of the package name's prefix).

You can also consult the answers provided here: Log4J: Strategies for creating Logger instances

Community
  • 1
  • 1
Shakkalakka
  • 439
  • 4
  • 12