Log4j logfile error FileNotFoundException - java

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?

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.

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

Reduce logs of log4j in catalina.out

I am using the log4j API for logging in my Java based Tomcat application and using version 1.2.14 (log4j-1.2.14.jar). The problem is that it creates more logs in the catalina.out log file, which are very micro level logs and not required for me. I am facing the problem of memory in my machine, and I want to reduce the logging of the log4j logger. Is there a way to reduce logs such that I can save my memory?
Setting a properties or a XML configuration file will allow you to control log4j output as stated in the log4j manual.
An example of the properties file would be like this
# Root logger option
log4j.rootLogger=**ERROR**, file
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\mylogging.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{ABSOLUTE} %5p %c{1}:%L - %m%n
Note the rootLogger parameter, as it controls how much information you get, whether you need only error messages or everything down to information messages.
In order to set it properly, look in Log4j Configuration Using Properties File for extra information as you can also use it in the XML version (see Multiple Appenders Using XML File). Tip: place it in src/ or in project root.
You will have to look at CATALINA_HOME/conf folder for log4j.properties and raise the logging level to say, warn or error depending upon your needs

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

Categories