Cannot get log4j log file created - java

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.

Related

No appenders could be found for logger using Log4j

I'm running an application locally and trying to get it to output logs to a file I created. the log4j.properties file looks like this:
# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=C:\\Users\\jkm144\\workspace\\LdapPurge\\log.out
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n
It's a Maven project. It's defined in my pom.xml and is listed in my Maven dependencies.
I've imported three classes:
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.FileAppender;
and the code I'm using to define the logger is:
static Logger log = Logger.getLogger(LdapPurge.class.getName());
Any time I run into a log, it tells me that "log4j:WARN No appenders could be found for logger"
I'm not sure exactly what I'm doing wrong. This is my first time using log4j.
Your screenshot suggests this is a Maven project, so put the log4j.properties in src/main/resources. This is the Maven-standard path for resource files that need to end up on the classpath at runtime, and anything you put in there will be packaged into your application's JAR file along with the classes that were compiled from the sources in src/main/java.
You should place the log4j.properties under the
WEB-INF/classes
directory of your web-applications. Log4j will find the properties file and initialize itself. This is easy to do and it works.
Manaully Adding log4j.properties:
you can add it any where you want, when you run your project configure the classpath and add the location of the log4j.properties files by clicking on: Run->Run Configuration -> [classpath tab] -> click on user Entries -> Advanced -> Select Add Folder -> select the location of your log4j.properties file. Click OK

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?

Log is not working inside the java file. But works inside the servlet

I have issue in executing the logs. I have two projects: one is a servlet and the other one is a simple java file. private static Logger logger=Logger.getLogger(myServiceServlet.class); This was working fine. Its writing log wen i start running the Tomcat server. But the same thing is not achieve in my simple java file, it converted into WS and try to deploy in axis/Tomcat server
Servlet is directly running in Tomcat server.
But java file is converted into Webservice.aar inside the Axis2--->Tomcat server.
Log Properites
# Log levels
log4j.rootLogger=INFO,CONSOLE,R
# Appender Configuration
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
# Pattern to output the caller's file name and line number
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
# Rolling File Appender
log4j.appender.R=org.apache.log4j.RollingFileAppender
# Path and file name to store the log file
log4j.appender.R.File=C:/res backup/apache-tomcat-6.0.35/webapps/mylog/logs/servicelog.log
log4j.appender.R.MaxFileSize=2048KB
# Number of backup files
log4j.appender.R.MaxBackupIndex=50
# Layout for Rolling File Appender
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d - %c - %p - %m%n`
The .aar files are not running in the same context as the JARs in WEB-INF/lib.
They share some kind of context as you can load references to the WEB-INF/lib classes, however its not 100% consistent at this point.
I would recommend you build some kind of wrapper in your .aar files that do nothing else then forwarding the calls into methods of JAR files that are situated in WEB-INF/lib.
You just need to make sure you load the reference from the .aar to the methods in WEB-INF/lib context correctly.
To throw errors in the .aar classes I would recommend you catch them and then throw an "AxisFault".
throw new AxisFault(err.getMessage());
http://axis.apache.org/axis2/java/core/api/org/apache/axis2/AxisFault.html
That way we have have been able to solve almost all our issue with the context of .aar files.
Sebastian

Need to create separate log file for jar file?

I am creating a jar file that i need to provide to client, but my client is asking for separate logger for the jar file as this is doing some integration work.
anyone can suggest how can i create logger for only one jar file, can i put log4j.properties in same jar file.
I am using web-logic server. we will not deploy this jar file instead we will just keep it in domain lib folder.
Thanks
If the properties file is in the jar, then you could do something like this:
Properties props = new Properties();
props.load(getClass().getResourceAsStream("/log4j.properties"));
PropertyConfigurator.configure(props);
The above assumes that the log4j.properties is in the root folder of the jar file.
If this does not work for your needs in this case then you can always use:
-Dlog4j.configuration=log4j_for_some_jar.properties
If the other application is using Log4j as well. The easier method would be to just config your log4j file to send anything from your classes in your jar to a new log file like so:
log4j.rootLogger=ERROR, logfile
log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.logfile.datePattern='-'dd'.log'
log4j.appender.logfile.File=log/radius-prod.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%-6r %d{ISO8601} %-5p %40.40c %x - %m\n
log4j.logger.foo.bar.Baz=DEBUG, myappender
log4j.additivity.foo.bar.Baz=false
log4j.appender.myappender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.myappender.datePattern='-'dd'.log'
log4j.appender.myappender.File=log/access-ext-dmz-prod.log
log4j.appender.myappender.layout=org.apache.log4j.PatternLayout
log4j.appender.myappender.layout.ConversionPattern=%-6r %d{ISO8601} %-5p %40.40c %x - %m\n

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

Categories