saving the logs in text file which gets generated in selenium webdriver - java

How to save the logs in text file which gets generated in selenium webdriver?
I know how to log normal logs in java like using logger.
Below is the step which i tried to log.
Logger logger = Logger.getRootLogger();
PropertyConfigurator.configure("data/config/log4j.properties");
FileAppender appender = (FileAppender)logger.getAppender("file");
String logFilePath ="C:\\Users\\priyarsi\\Desktop\\LdapLog.txt";
appender.setFile(logFilePath);
appender.setThreshold(Level.DEBUG);
appender.setAppend(true);
appender.activateOptions();
log.debug("Hello this is an debug message");
log.info("Hello this is an info message");
Can anyone please help me in logging selenium webdriver output to a text file?

Try this out -
# Root logger option
log4j.rootLogger=INFO, file, stdout
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=../AutoLogs.log
#log4j.appender.file.File=${file.name}
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss} %5p %c{1}:%L %t - %m%n
# Direct log messages to stdout
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{dd MMM yyyy HH:mm:ss} %5p %c{1}:%L %t - %m%n

You can try logging Log4j API. It is very easy to understand and implement. Perform following steps:
Download Log4j jars from (https://logging.apache.org/log4j/1.2/download.html)
Add downloaded jars to project's build path.
Create a new XML file – log4j.xml and place it under the Project root folder.
Add new xml file or you can customise it the way you want. (http://toolsqa.com/selenium-webdriver/log4j-logging/)
Create new Java Class and paste following code:
package utility;
import org.apache.log4j.Logger;
public class Log {
// Initialize Log4j logs
private static Logger Log = Logger.getLogger(Log.class.getName());//
// This is to print log for the beginning of the test case, as we usually
// run so many test cases as a test suite
public static void startTestCase(String sTestCaseName) {
Log.info("****************************************************************************************");
Log.info("****************************************************************************************");
Log.info("$$$$$$$$$$$$$$$$$$$$$ " + sTestCaseName + " $$$$$$$$$$$$$$$$$$$$$$$$$");
Log.info("****************************************************************************************");
Log.info("****************************************************************************************");
}
// This is to print log for the ending of the test case
public static void endTestCase(String sTestCaseName) {
Log.info("XXXXXXXXXXXXXXXXXXXXXXX " + "-E---N---D-" + " XXXXXXXXXXXXXXXXXXXXXX");
Log.info("X");
Log.info("X");
Log.info("X");
Log.info("X");
}
// Need to create these methods, so that they can be called
public static void info(String message) {
Log.info(message);
}
public static void warn(String message) {
Log.warn(message);
}
public static void error(String message) {
Log.error(message);
}
public static void fatal(String message) {
Log.fatal(message);
}
public static void debug(String message) {
Log.debug(message);
}
}
Call above methods in your test scripts.

Related

log4j error: Value not resolved by key: project_report_directory

I try to use log4j for my project and created follow logic:
Log class:
public class Log {
//Initialize Log4j instance
private static Logger Log = Logger.getLogger(Log.class.getName());
//We can use it when starting tests
public static void startLog (String testClassName){
Log.info("Test is Starting...");
}
//We can use it when ending tests
public static void endLog (String testClassName){
Log.info("Test is Ending...");
}
//Info Level Logs
public static void info (String message) {
Log.info(message);
}
//Warn Level Logs
public static void warn (String message) {
Log.warn(message);
}
//Error Level Logs
public static void error (String message) {
Log.error(message);
}
//Fatal Level Logs
public static void fatal (String message) {
Log.fatal(message);
}
//Debug Level Logs
public static void debug (String message) {
Log.debug(message);
}
}
log4j.properties:
#logging level
log4j.rootLogger=INFO, stdout, file, console
# stdout settings
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=%m%n
log4j.logger.org.mortbay.log = INFO
# File settings
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=c:\\Combo.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
So when I try to execute the test:
public class LoginTests extends BaseTest {
#Test (priority = 0, description="Invalid Login Scenario with wrong username and password.")
public void invalidLoginTest_InvalidUserNameInvalidPassword () {
Log.info("Debug message");
}
I'm getting the stacktrace which start with:
and finished with:
The console contains a lot of lines (several hundred) with text 2020-11-05 23:00:51 PlaceholderResolver [main] [WARN] Value not resolved by key: project_report_directory
And veri long stactrace with java.lang.StackOverflowError:
What means this error, and how to resolve this?
This issue occurs when file lo4j.properties is absent.
Example of the file with which the project works well:
# Root logger option
log4j.rootLogger=INFO, file, stdout
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=${user.dir}/Log4j/log4j-test-automation.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
#do not append the old file. Create a new log file everytime
log4j.appender.dest1.Append=false
# Direct log messages to stdout
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=[%-4p] %d{dd-MM-yy HH:mm:ss} %t | %m%n

Despite setting rootlogger to INFO/DEBUG, only error statements are printed. why?

If I put info, only error log is printed.
If I put debug, only error log is printed.
Why is the logging level not working?
public class Test {
private static Logger log = Logger.getLogger(Test.class);
public static void main(String[] args) {
// TODO Auto-generated method stub
BasicConfigurator.configure();
log.info("info test");
log.debug("debug test");
log.error("error test");
}
}
Console:
0 [main] ERROR com.testfiles.Test - error test
log4j properties file:
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, theConsoleAppender
# A1 is set to be a ConsoleAppender.
log4j.appender.theConsoleAppender=org.apache.log4j.ConsoleAppender
# A1 uses PatternLayout.
log4j.appender.theConsoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.theConsoleAppender.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
You're not supposed to call the BasicConfigurator directly if you have a properties file. Remove that call and the properties file should be picked up, provided it's on the classpath.

getting error when using with using multiple loggers

i am using multiple loggers to append into multiple log files.the path of these log files are set dynamically.
i am doing my project in java and using log4j framework.
my project structure.
src
log4test
log4test.java
log4test2
log4test2.java
log4j.properties
log4test.java:-
public class Log4Test{
public static void main(String[] args)throws IOException,SQLException{
System.setProperty("logs", "C:\\New folder 3\\logs.log");
Logger log = Logger.getLogger("file");
log.info("hello");
Log4Test2.fefe();
}
}
log4test2.java:-
public class Log4Test2{
/* Get actual class name to be printed on */
public static void fefe()throws IOException,SQLException{
System.setProperty("logs.file", "C:\\New folder 2\\logs.log");
Logger log = Logger.getLogger("admin");
log.info("Debug2");
log.info("Info2");
}
}
log4j.properties:-
log4j.rootLogger=INFO
log4j.logger.file=INFO, file
log4j.logger.admin=INFO, admin
# Define the file appender
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=$[logs}
# Define the layout for file appender
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss}
[%p] %m%n
# Define the file appender
log4j.appender.admin=org.apache.log4j.FileAppender
log4j.appender.admin.File=${logs.file}
# Define the layout for file appender
log4j.appender.admin.layout=org.apache.log4j.PatternLayout
log4j.appender.admin.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss}
[%p] %m%n
i am getting this error:-
log4j:ERROR setFile(null,true) call failed
Could you check this line
# Define the file appender
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=$[logs} //TODO should be ${logs}
If I am not mistaken. Thank you.
The second thought is:
public class Log4Test2{
private final Logger logger = LoggerFactory.getLogger("admin");
public void fefe()throws IOException,SQLException{ //try remove static
System.setProperty("logs.file", "C:\\New folder 2\\logs.log");
log.info("Debug2");
log.info("Info2");
}

Java : How to use a logger to log from multiple classes into a same file?

I have a main class say class A where :
public class A {
someMethod(){
log.debug("inside some method in class A");
}
}
And then class B where :
public class B {
someOtherMethod(){
log.debug("inside some other method in class B");
}
}
Now how do I use loggers here, i.e., to log from both these classes and log into a common log file?
What should be configuration of the properties file?
if you are using log4j, you can add a config.properties file to your resources:
# Root logger option
log4j.rootLogger=INFO, file, stdout
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
# this is the ouput file path and name
log4j.appender.file.File=filename.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Direct log messages to stdout
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{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
Then you can use like this:
import org.apache.log4j.Logger;
public class A {
Logger log= Logger.getLogger(A.class);
someMethod(){
log.debug("inside some method in class A");
}
public class B {
Logger log= Logger.getLogger(B.class);
someOtherMethod(){
log.debug("inside some other method in class B");
}
}

How can I create 2 separate log files with one log4j config file?

I can't figure out how to configure my log4j so that my debugLog and my reportsLog are separate from each other (not additive). Why is it, that in the configuration below, the reportsLog is always empty?
log4j.rootLogger=TRACE, stdout, debugLog
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n
log4j.appender.debugLog=org.apache.log4j.FileAppender
log4j.appender.debugLog.File=logs/debug.log
log4j.appender.debugLog.layout=org.apache.log4j.PatternLayout
log4j.appender.debugLog.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n
log4j.reportsLog=DEBUG,reportsLog
log4j.appender.reportsLog=org.apache.log4j.FileAppender
log4j.appender.reportsLog.File=logs/reports.log
log4j.appender.reportsLog.layout=org.apache.log4j.PatternLayout
log4j.appender.reportsLog.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n
And here is my Java code:
package test;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class HelloLogger {
static final Logger debugLog = Logger.getLogger("debugLog");
static final Logger resultLog = Logger.getLogger("reportsLog");
public static void main(String[] args) {
PropertyConfigurator.configure("log4j.properties");
debugLog.debug("Hello debugLog message");
resultLog.debug("Hello reportsLog message");
}
}
Try the following configuration:
log4j.rootLogger=TRACE, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n
log4j.appender.debugLog=org.apache.log4j.FileAppender
log4j.appender.debugLog.File=logs/debug.log
log4j.appender.debugLog.layout=org.apache.log4j.PatternLayout
log4j.appender.debugLog.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n
log4j.appender.reportsLog=org.apache.log4j.FileAppender
log4j.appender.reportsLog.File=logs/reports.log
log4j.appender.reportsLog.layout=org.apache.log4j.PatternLayout
log4j.appender.reportsLog.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n
log4j.category.debugLogger=TRACE, debugLog
log4j.additivity.debugLogger=false
log4j.category.reportsLogger=DEBUG, reportsLog
log4j.additivity.reportsLogger=false
Then configure the loggers in the Java code accordingly:
static final Logger debugLog = Logger.getLogger("debugLogger");
static final Logger resultLog = Logger.getLogger("reportsLogger");
Do you want output to go to stdout? If not, change the first line of log4j.properties to:
log4j.rootLogger=OFF
and get rid of the stdout lines.
Modify your log4j.properties file accordingly:
log4j.rootLogger=TRACE,stdout
...
log4j.logger.debugLog=TRACE,debugLog
log4j.logger.reportsLog=DEBUG,reportsLog
Change the log levels for each logger depending to your needs.

Categories

Resources