getting error when using with using multiple loggers - java

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");
}

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.

logging file NOT created in Eclipse-Java

I have written this piece of code just to check if I'm able to create .log file or not. Is the code correct or not?
package loggingLearn;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class Log4jExampleInJavaProgramming {
public Log4jExampleInJavaProgramming() {
// TODO Auto-generated constructor stub
System.out.println("Inside constructor");
}
final static Logger logger = Logger.getLogger(Log4jExampleInJavaProgramming.class);
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Inside main");
Log4jExampleInJavaProgramming lg = new Log4jExampleInJavaProgramming();
PropertyConfigurator.configure("log4j2.properties");
BasicConfigurator.configure();
logger.info("this is the info by log4j");
logger.error("this is the error by log4j");
System.out.println("After logger.info11");
}
}
I created log4j2.properties file but its not creating .log file
==========================================================
# initialize root logger with level ERROR for stdout and fout
log4j.rootLogger=INFO, stdout, file
# set the log level for these components
#log4j.logger.com.endeca=INFO
#log4j.logger.com.endeca.itl.web.metrics=INFO
# add a ConsoleAppender to the logger stdout to write to the console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# use a simple message format
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
#log4j.appender.file=org.apache.RollingFileAppender
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.file.File=${E:\\eclipse-workspace\\log}/log4j2.debug
log4j.appender.file.File=C:\\log\\log4j2.debug
log4j.appender.file.MaxFileSize=1KB
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
# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
# Set the name of the file
log4j.appender.FILE.File=${log}/log.out
# Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true
# Set the threshold to debug mode
log4j.appender.FILE.Threshold=debug
# Set the append to false, overwrite
log4j.appender.FILE.Append=false
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n

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");
}
}

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

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.

Categories

Resources