Log4j printing to console every time - java

I have placed following log4j.properties file in src folder of my eclipse dynamic web project. But everytime logs are getting printed on console though I am writing code to print the logs in file only. If I am deleting log4j.properties file then also it is getting printed on console.Don't know what went wrong.
###############################################################################
log4j.rootLogger=INFO, file
###############################################################################
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:/apache-tomcat-7.0.47/webapps/LogFile.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %-5p %c - %m%n
###############################################################################
I am using following code to print the logs.
import org.apache.log4j.Logger;
Logger log = Logger.getLogger(Client.class.getName());
log.info("First log");

Try to override the log4j configuration in your class like this:
import org.apache.log4j.PropertyConfigurator;;
static{
PropertyConfigurator.configure("src/main/resources/log4j.properties");
}
This assumes you have a file called log4j.properties under src/main/resources

Thanks Nikhil
I have used follwing code by looking your code and it worked fine.
import org.apache.log4j.PropertyConfigurator;
PropertyConfigurator.configure(getClass().getClassLoader().getResourceAsStream("log4j.properties"));

Related

Why log4j renames filename?

I use the bellow codes.When I run project create MyLogFile.log file.
Next day I run project and log4j renames MyLogFile to
MyLogFile.log_Yesterday.log and create new MyLogFile file and start to write this file.
Why It doesn't crate MyLogFile.log_ToDay.log file?
Why it renames fileName?
Thank in advance
log4j.rootLogger=DEBUG, stdout
log4j.rootLogger=DEBUG, RollingAppender
log4j.appender.RollingAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.RollingAppender.File=d:/Logs/MyLogFile.log
log4j.appender.RollingAppender.DatePattern='_'yyyy-MM-dd'.log'
log4j.appender.RollingAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.RollingAppender.layout.ConversionPattern=[%p] %d %c %M - %m%n
The appender being used is org.apache.log4j.DailyRolling‌​FileAppender which takes a back up of the current log file whenever the date changes.
2017-08-29, The log file will be created with the name MyLogFile.log and logs will be written to it.
2017-08-30, Whenever the code encounters something to be logged, the appender will rename the file created on the previous day to MyLogFile_2017-08-29.log and create a fresh MyLogFile.log for the current day.
Please refer to the Javadoc for DailyRollingFileAppender for more details on how it works.

Cannot get log4j log file created

I know this question has been asked before, but none of the answers have worked for me. I have the following log4j.properties file:
# Define the root logger with appender file
log4j.rootLogger = INFO, FILE, stdout
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=/home/sutureself/log.out
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n
# Define the layout for the stdout appender
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}
I have this file located in <workspace-root>/resources (I have tried it within src and in <workspace-root>. The resources folder has been added to the build path in Eclipse, along with the jar files log4j-api-2.0-rc1.jar and log4j-core-2.0-rc1.jar. However, when I export the project as a runnable jar, no log file is created, and also nothing is written to stdout either.
I read in some of the other answers on SO, that it needs to be in a folder that is on your classpath. So I added the following to my code:
String classpath = System.getProperty("java.class.path");
System.out.println("Classpath [" + classpath + "]");
... and put the properties file in the location this printed out, but still no file or stdout. In each of my java files where the logger is used, it is implemented:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class SutureSelfExample {
private static Logger log = LogManager.getLogger(SutureSelfExample.class.getName());
...
log.info("Log something interesting.");
}
If I run the jar with the following commands:
./SutureSelfRunnable.jar -Dlog4j.configuration=./log4j.properties
./SutureSelfRunnable.jar -cp /home/sutureself/
... still nothing.
What is missing? Do I need to have more in my Java code in order to get this working?
for log4j 2 you need to config by xml config file : see Configuration Log4j 2 for more.
if you want to use the properties file you must downgrade your libs to log4j 1.x.

Log4j logfile error FileNotFoundException

I am trying to write a log4j logfile using Netbeans Java. I succeded writing the log file to C:\tmp\log.txt , but for some reason i have no premission to write it at the root of my project : log.txt.
This is my log4j.properties:
log4j.rootLogger = all, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=log.txt <- failes
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=[%5p](%F:%L) %m%n
log4j.appender.FILE.Append=false
Have you try this :
log4j.appender.FILE.File=./log.txt
. represents the current folder (usually the project root folder). Maybe the log file that you set are placed somewhere in root of your drive.
This reference may useful for you :
Where Are Your Log4j Log Files?

NTEventLogAppender (Library is already loaded in another ClassLoader)

I have a log4j.properties file like this on my src package:
log4j.rootLogger=DEBUG, CA, EVA
#Console Appender
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
#Event Viewer Appender
log4j.appender.EVA=org.apache.log4j.nt.NTEventLogAppender
log4j.appender.EVA.layout=org.apache.log4j.PatternLayout
log4j.appender.EVA.source=MySource
log4j.appender.EVA.layout.ConversionPattern=[%c][%l][%p][%thread]: %m%n
I create the logger on a interface (LogInterface.java) like this:
package components;
import org.apache.log4j.Logger;
public interface LogInterface {
static final Logger logger = Logger.getLogger("MyLogger");
}
I place the NTEventLogAppender.dll under:
C:\Program Files (x86)\IBM\WID7_WTE\runtimes\bi_v7\java\jre\bin
Sometimes I am getting the following error when there is an exception to log:
NTEventLogAppender (Library is already loaded in another ClassLoader)
How can I solve this issue?
Thanks
The solution for this problem was to place the log4j JAR file under:
Application_Server_Install_Path\lib
On the project I added the log4j JAR file to the project's build path as a variable (add variable option)
This solves the JAR reference on the IDE the only thing left to do is to add the previous log4j JAR file reference on the Websphere Application Server's runtime.
To do this I accessed the WAS administration console and added the previous log4j JAR file path under:
Environment -> Shared Libraries
Despite no one seems to have this same problem, here's the solution for future similar problems
Thanks

Logging a specific package in log4j programmatically

I have to deploy a web app and the the log4j.properties file is created by the client so I dont have control over it.
Their properties file is like this:
log4j.rootCategory= FILE
!-----------FILE--------------!
log4j.category.FILE=DEBUG
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=${catalina.base}/logs/rcweb.log
log4j.appender.FILE.MaxFileSize=1024KB
log4j.appender.FILE.MaxBackupIndex=10
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%-2d{dd/MM/yyyy HH:mm:ss} [%t] %5p %c:%L - %m%n
And in my classes I do something like this:
private static final Logger LOG = Logger.getLogger(MaterialController.class);
LOG.info("my log");
But the log file has never been created.
I did the test and changed the log4j.properties file and deployed it in my computer adding the following line:
log4j.logger.br.com.golive.requisicaoCompras=DEBUG
This works, but I can't use the file like this.
Are there any ideas?
Problem solved. My client's log4j.properties file was wrong, the first line should be:
log4j.rootCategory=DEBUG, FILE

Categories