Why log4j renames filename? - java

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.

Related

log4j2 DailyRollingFileAppende separate log files on roll based pid in actual log

I've implemented a plugin that adds to log information about pid.
appender.DRFA.layout.pattern = %d{ISO8601} %5p **[%pid]** [%t] %c{2}: %m%n
Also there is another plugin for FilePattern conversion that adds pid to file when rolling takes place
appender.DRFA.filePattern = ${sys:some.log.dir}/${sys:some.log.file}.%d{yyyy-MM-dd}.%pid
Trying to understand how to make next:
at the end of the day take all rows in log files that have specific pid and roll them to file with the same pid defined by pattern
appender.DRFA.filePattern = ${sys:some.log.dir}/${sys:some.log.file}.%d{yyyy-MM-dd}.%pid
Log4j2 rollover cannot combine multiple log files into a single zip file. Rollover can rename and/or compress a single log file. What you can do is move all log files with the same pid info the same directory:
appender.DRFA.filePattern = ${sys:some.log.dir}.%pid/${sys:some.log.file}.%d{yyyy-MM-dd}
If you need to compress multiple files together, or append these files together into a single text file, the built-in rollover cannot currently do that.
Take a look at the custom delete action that can be triggered on rollover.
You may be able to extend that into what you have in mind. Please raise a JIRA ticket if you need Log4j2 to make API changes for your custom rollover actions.

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?

Log4j - Can't find the log file

I'm having trouble with an application that crashes when I deploy it to other computers who are running JRE 1.7. When I run this inside of NetBeans (or even directly from the JAR file) on my PC, everything is fine. But on another computer it fails at specific events (button clicks) during execution.
So, I learned about logging using the log4j library. This gave me some information on a problem in my application, and the logging works perfectly, again on MY computer. But when I deploy the JAR file to other computers, who are only running JRE (Java 7 Update 17), I can find no traces of any log files.
Here is my log4j.properties file:
# 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=C:\logging.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{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
On my computer, I can see the logging.log file right inside of the project folder. To that extent, everything works perfectly. However, on the user PC, there is no sign of this file at all. Not in C:\ (where I thought it would be), not in C:\Program Files (x86)\ or anywhere else. I've done a complete search of my hard disk, but nothing comes back.
Where should this file be stored? Are my properties set correctly? Very confused...
Thank you!
In case anybody ever stumbles on this post, I wanted to document how I solved it.
First of all, as DwB correctly pointed out, the problem was indeed that the user's account did not have sufficient privileges to create the logging.log file. In my code, I had a catch block that was being executed and contained code to exit the system in the event of a file write error. Since it was exactly the logging detail was what I was trying to write, I was unable to get any output to clue me into this as the source of my problem.
Once I realized this, I just had to change where I was writing the log file. Rather than write it to the root (C:\) or to the folder where the jar file was (C:\Program Files\), both of which were places where I could not guarantee that a user would have full privileges, I decided to create a folder in their AppData path.
The very first line in my program is now a call to this function (using the generic name MyProgram):
private static void makeAppDataFolder() {
File dir = new File(System.getenv("APPDATA") + "\\MyProgram");
if (!dir.exists()) {dir.mkdir();}
}
This creates a folder called MyProgram in the user's account at \AppData\Roaming. Since this path is part of the user account path, I believe that the user will always be able to write to it, thereby solving the permissions issue. If anybody knows otherwise, please let me know.
Then, in my log4j.properties file, I change the line
log4j.appender.file.File=logging.log
to
log4j.appender.file.File=${user.home}/Application Data/MyProgram/logging.log
This directs the file to the folder that I just created.
Once I did this, and added some well-placed logging messages, I was able to find my other issues, and now everything works just fine.
I hope that this helps somebody out. If anyone has any suggestions, please post.

log4j log file not getting updated

I am using a apache's log4j for logging errors in my application. When I start the tomcat server, the log info information is written into the log file as expected (Also the log info is written into the log file when I stop the tomcat server). But when I start using the application, I log information I am expecting to be written through the app is not written into the log file. For e.g. I am giving inputs that will give exception, but the log.error(e,e) isn't getting written in the log file.
this is how i am using the Logger
static Logger log = Logger.getLogger(MyClass.class);
log.info("my message" );
log.error(e,e);
Please help
EDIT: Adding log4j.properties file contents
log4j.rootLogger =INFO, FILE, stdout
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=${catalina.home}/logs/myapp.log
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{ABSOLUTE} %5p\t\t%c{1} :%L - %m%n
It seems that you have not configured your logger. Please find log4j.xml or logging.properties typically located under TOMCAT_HOME/conf. Edit them to include your application's package.
This will help you to write your application level log messages to log file of your server (tomcat).
Better way is to manage your own logging configuration and files. Create log4j.xml file and put it under WEB-INF/classes into your application. You can find a lot of examples and tutorials that will help you to write your log4j.xml. For example this one: http://logging.apache.org/log4j/1.2/manual.html

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

Categories