logging file NOT created in Eclipse-Java - 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

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