Apps

 

 

 

What is Log :

  • We need some information to be logged in the console, file or database

 

What is Log4j :

  • Used for logging mechanism
  • It is fast, flexible and reliable logging framework
  • It is written by java and distributed under Apache Software License

 

Why Log4j

  • Open source
  • Can be used for small or large projects
  • Possible to store the flow details of our selenium automation in a file, console or database

 

Log 4j Components

  • Loggers :

It is responsible for logging information.

Create the instance of logger class

Log4j levels :

  • All - it will log everything
  • DEBUG - print debugging information
  • INFO - print info message that highlights the progress of apps -
  • WARN - print infor regarding faulty and unexpected system behavior
  • ERROR - print error message that might allows system to continue
  • FATAL - print system critical info which cousing application to crsh
  • OFF - no logging
  • Appenders :

used to deliver log Events to their destination

  • ConsoleAppender - used to log standard output
  • FileAppender - used to prints logs to some file
  • RollingFileAppender - to a file with maximum size
  • Layouts :

It is responsible for formatting logging info in different styles

 

Steps to create log

  • Download log4j jar files or get the dependecy if you are using maven project

https://mvnrepository.com/artifact/log4j/log4j

  • Place log4j.properties / log4j.xml file in project home directory ( only can be used)
  • log4j.properties file
    • Logger logg = Logger.getLogger("class_name");
    • PropertyConfigurator.configure("Log4j.properties")
  • Log4j.xml file
    • Logger logg = Logger.getLogger("class_name");
    • DOMConfigurator.configure("log4j.xml");
  • Create utility class (log) to initialize log4j logs
  • Create base Class and configure ur log4j.xml using DOMConfigurator
  • Write your test cases using test class

 

 

Steps to create for creating log 4j by automation step by step (Working)

  • Add Log4j libraries in the java project

Can add jar files or maven dependency

https://logging.apache.org/log4j/2.x/maven-artifacts.html

 

  • Create reference for logger in the class

Class where you want to add log statements

 

public class Log4jExample {

 

public static Logger logger = LogManager.getLogger(Log4jExample.class);

 

public static void main(String[] args) {

System.out.println("\n  start --------------");

 

logger.info("this is information message");

logger.error("this is error message");

logger.warn("this is warn message");

logger.fatal("this is fatal message");

logger.trace("this is trace message");

System.out.println("\n  Completed --------------");

}

}

 

  • Create log4j.xml or log4j.properties file

 log4j.properties file :

  1. Create Log4j2.properties file in resource folder
  2. Copy file from https://springframework.guru/log4j-2-configuration-using-properties-file/

name=PropertiesConfig

property.filename = logs

appenders = console, file

 

appender.console.type = Console

appender.console.name = STDOUT

appender.console.layout.type = PatternLayout

appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

 

appender.file.type = File

appender.file.name = LOGFILE

appender.file.fileName=${filename}/propertieslogs.log

appender.file.layout.type=PatternLayout

appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

 

loggers=file

logger.file.name=learn.log4j  ///***change package name

logger.file.level = trace

logger.file.appenderRefs = file

logger.file.appenderRef.file.ref = LOGFILE

 

rootLogger.level = trace

rootLogger.appenderRefs = stdout

rootLogger.appenderRef.stdout.ref = STDOUT

 

 

 

 

 

 

 

 

 

Comments