How Log4j is configured

We need to implement the appender to configure Log4j. According to it parameters of appender will be set.

We basically set two configurations or logs,

  1. Root logger, which will write all system generated logs in file name i.e. Selenium.logs
  2. We will write the information generated by manual commands in code into the file name- Manual.logs

Root logger

log4j.rootLogger=DEBUG,file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=E:\\Automation\\Workspace\\ePlanner\\log4jTest\\src\\Manual.logs
log4j.appender.file.maxFileSize=900KB
log4j.appender.file.maxBackupIndex=5
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c<strong>{1}</strong>:%L - %m%n
log4j.appender.file.Append=false

 

Application Logs

log4j.logger.devpinoyLogger=DEBUG, dest1
log4j.appender.dest1=org.apache.log4j.RollingFileAppender
log4j.appender.dest1.maxFileSize=900KB
log4j.appender.dest1.maxBackupIndex=6
log4j.appender.dest1.layout=org.apache.log4j.PatternLayout
log4j.appender.dest1.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss} %c %m%n
log4j.appender.dest1.File=E:\\Automation\\Workspace\\ePlanner\\log4jTest\\src\\Manual.logs
log4j.appender.dest1.Append=false

 

As per the above code, we have configured log4j to log in two different files:

  • file and dest1 are the two identifiers.
  • “File” is used to give file name in which logs will be saved
  • “maxFileSize” is used to configure the maximum size of the log file. When file reaches this size, a new file will be created with the same name and the old file name will be add as an Index to it.
  • “maxBackupIndex” is used to configure maximum number of files to be backup.
  • “layout” is used to set the format of the log file.
  • “Append” is used to set append function. If it is set to false, than every time a new file will be created rather than old file will be used for logging.