0

Hello I'm looking for a little bit of help with log4J. I'm running a test suite that has a series of classes with methods that are set-up as individual tests (These are then threaded), within these methods I've used the info logger to out put simple test annotation in a given, when then format simply for the business to read/debug when stuff goes wrong and random test data generated to a file to trace. A need has arisen to output the given/when/then with test data used into an individual file based on class and method. Essentially what i want to achieve is that each test will have its own record. For example a file will be named after the method it came from and contain the given/when/then and the test data it used. Is there a way of doing this through the log4J.properties file: here is a copy of the code i used to output the test steps and data:

# Direct log messages to err
log4j.appender.stderr=org.apache.log4j.ConsoleAppender
log4j.appender.stderr.Target=System.err
log4j.appender.stderr.layout=org.apache.log4j.PatternLayout
log4j.appender.stderr.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

# Direct log messages to out
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %c{1} - %m%n

log4j.rootLogger=WARN, stdout
log4j.logger.[TEST-LOGGER]=INFO
log4j.logger.org.hibernate=WARN
log4j.logger.org.hibernate.type=WARN    
log4j.logger.com.gargoylesoftware=FATAL
log4j.logger.com.gargoylesoftware.htmlunit.javascript=FATAL
log4j.logger.com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl=FATAL
log4j.logger.com.ga.endeavour=WARN

log4j.appender.rollingFile=org.apache.log4j.RollingFileAppender
log4j.appender.rollingFile.File=/tmp/mylog.log
log4j.appender.rollingFile.MaxFileSize=2MB
log4j.appender.rollingFile.MaxBackupIndex=2
log4j.appender.rollingFile.layout = org.apache.log4j.PatternLayout
log4j.appender.rollingFile.layout.ConversionPattern=%p %t %c - %m%n
log4j.logger.JSONLOG = INFO, rollingFile
saab613
  • 213
  • 1
  • 2
  • 19

1 Answers1

0

I think this question covers a similar situation. In short, you can look into using the Mapped Diagnostic Context and a customer appender.

That's a little more involved than just editing the properties file, but will hopefully not prove too much work.

Community
  • 1
  • 1
chooban
  • 9,018
  • 2
  • 20
  • 36