Re: log4j losing log files SOLVED
Hello all, A bit late, I'm afraid, but since I just got an off-list question about it, and that it was a problem for me, maybe it can help others in the future. Hope it helps you, Tim, if not, don't hesitate to subscribe and post to the list, there are many people that can help you better than me there! So, here is how I solved my problem of log4j losing its log files when rotating them. Maybe there are better ways, but that one seems to work nicely so far for me. I created a very small class implementing ServletContextListener: package com.infores.fr.ihs; import java.util.*; import javax.servlet.*; import org.apache.log4j.LogManager; /** * This class is used to shutdown log4j properly. * When the context of the application is destroyed, log4j should * be shut down to avoid keeping locked log files. */ public class IHScontextListener implements ServletContextListener { /** * Do nothing. */ public void contextInitialized(ServletContextEvent sce) { // Do nothing here... } /** * Shuts log4j down. * This is necessary to avoid lost log files: when the app * is reloaded, the JVM might keep a lock on the last log file * otherwise, and will not be able to rename it before * restarting it. */ public void contextDestroyed(ServletContextEvent sce) { LogManager.shutdown(); } } This class is put in a JAR file after compilation, and the JAR file itself is in the WEB-INF/lib directory of my webapp. Then, I configured my webapp so it uses the newly implemented listener. I edited the WEB-INF/web.xml file, and added: !-- ServletContextListener called when the application ends -- listener listener-classcom.infores.fr.ihs.IHScontextListener/listener-class /listener Hope this helps others as I was helped myself :-) Laurent - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: log4j losing log files
More data on my problem. I could reproduce the problem: it happened right after I reload the apps using the Manager app, using the URL: https://myserver/manager/reload?path=/webapp I could do this several times. After a Tomcat start/stop, the logging and rotating works. After a reload, the current log file stayed there, the alread rotated files (ext = 1) are rotated to ext+1, but no new file with extension .1 appeared I had tried Tim's workaround, but it seems to no avail. My current config is: log4j.rootLogger=DEBUG log4j.logger.dev=DEBUG, dev log4j.appender.dev=com.infores.fr.ihs.IHSlog log4j.appender.dev.File=C:/logs/dev_01.log log4j.appender.dev.layout=org.apache.log4j.PatternLayout log4j.appender.dev.layout.ConversionPattern=%d %X{RemoteHost} %C %-5p %c - %m%n log4j.appender.dev.MaxFileSize=10KB log4j.appender.dev.MaxBackupIndex=100 The IHSlog class is: public class IHSlog extends RollingFileAppender { /** * Rename file, if fails, copy it. * * @param src The source file * @param target The target file */ private static void renameFile(File src, File target) { if (!src.renameTo(target)) { System.out.println(renameTo() false); try { copyFile(src, target); } catch (IOException e) { System.out.println(Can't rename file 'cause + e.getMessage()); } } else { System.out.println(renameTo() false); } } /** * Copy a file to another file. * * @param source The source file * @param target The target file * @exception IOException */ private static void copyFile(File source, File target) throws IOException { char c = '\u0400'; byte abyte0[] = new byte[c]; FileInputStream fileinputstream = new FileInputStream(source); BufferedInputStream bufferedinputstream = new BufferedInputStream(fileinputstream); FileOutputStream fileoutputstream = new FileOutputStream(target); BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(fileoutputstream); int i; while((i = fileinputstream.read(abyte0)) != -1) { fileoutputstream.write(abyte0, 0, i); } bufferedoutputstream.close(); bufferedinputstream.close(); } } Data I got to the console (I had started Tomcat with the startup.bat script, and -Dlog4j.debug=true): log4j: Parsing for [dev] with value=[DEBUG, dev]. log4j: Level token is [DEBUG]. log4j: Category dev set to DEBUG log4j: Parsing appender named dev. log4j: Parsing layout options for dev. log4j: Setting property [conversionPattern] to [%d %X{RemoteHost} %C %-5p %c - % m%n]. log4j: End of parsing for dev. log4j: Setting property [maxFileSize] to [10KB]. log4j: Setting property [file] to [C:/logs/dev_01.log]. log4j: Setting property [maxBackupIndex] to [100]. log4j: setFile called: C:/logs/dev_01.log, true log4j: setFile ended log4j: Parsed dev options. log4j: Handling log4j.additivity.dev=[null] log4j: Finished configuring. log4j: rolling over count=10260 log4j: maxBackupIndex=100 log4j: Renaming file C:\logs\dev_01.log.3 to C:\logs\dev_01.log.4 log4j: Renaming file C:\logs\dev_01.log.2 to C:\logs\dev_01.log.3 log4j: Renaming file C:\logs\dev_01.log.1 to C:\logs\dev_01.log.2 log4j: Renaming file C:\logs\dev_01.log to C:\logs\dev_01.log.1 log4j: setFile called: C:/logs/dev_01.log, false log4j: setFile ended log4j: Parsing for [root] with value=[DEBUG]. log4j: Level token is [DEBUG]. log4j: Category root set to DEBUG log4j: Parsing for [dev] with value=[DEBUG, dev]. log4j: Level token is [DEBUG]. log4j: Category dev set to DEBUG log4j: Parsing appender named dev. log4j: Parsing layout options for dev. log4j: Setting property [conversionPattern] to [%d %X{RemoteHost} %C %-5p %c - % m%n]. log4j: End of parsing for dev. log4j: Setting property [maxFileSize] to [10KB]. log4j: Setting property [file] to [C:/logs/dev_01.log]. log4j: Setting property [maxBackupIndex] to [100]. log4j: setFile called: C:/logs/dev_01.log, true log4j: setFile ended log4j: Parsed dev options. log4j: Handling log4j.additivity.dev=[null] log4j: Finished configuring. So it seems that it did try to rename the file. Now, I don't know where the println I added in renameFile() is gone. I think there is actually some debug missing in tomcat, but I don't know why. I've been using it for over one year, and still need to learn a lot about how to configure it. I cleaned up the data a bit to make it more readable. I hope I didn't cut out any piece of important data inadvertently. Any idea? Laurent - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: log4j losing log files
Jacob Kjome wrote: Are you calling LogManager.shutdown() at application shutdown? If not, try that, otherwise, the file will probably remain locked by the VM. Do this in a servlet context listener contextDestroyed() method. No, I'm not, and it's a good idea. But I'm unsure if it's possible to do that: all my JSPs are called through a servlet which handles the sessions, and I don't have the source of that servlet. Or would it be correct to do it in the JSPs themselves? Laurent - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: log4j losing log files
Howdy, Neither in the JSPs nor the servlet: take Jacob's advice about the ServletContextListener, it's a good one. Yoav Shapira Millennium ChemInformatics -Original Message- From: Laurent Blume [mailto:[EMAIL PROTECTED] Sent: Wednesday, October 29, 2003 12:36 PM To: Log4J Users List Subject: Re: log4j losing log files Jacob Kjome wrote: Are you calling LogManager.shutdown() at application shutdown? If not, try that, otherwise, the file will probably remain locked by the VM. Do this in a servlet context listener contextDestroyed() method. No, I'm not, and it's a good idea. But I'm unsure if it's possible to do that: all my JSPs are called through a servlet which handles the sessions, and I don't have the source of that servlet. Or would it be correct to do it in the JSPs themselves? Laurent - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] This e-mail, including any attachments, is a confidential business communication, and may contain information that is confidential, proprietary and/or privileged. This e-mail is intended only for the individual(s) to whom it is addressed, and may not be saved, copied, printed, disclosed or used by anyone else. If you are not the(an) intended recipient, please immediately delete this e-mail from your computer system and notify the sender. Thank you. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: log4j losing log files
Shapira, Yoav wrote: Howdy, Neither in the JSPs nor the servlet: take Jacob's advice about the ServletContextListener, it's a good one. Ok, I got it! I didn't know about that, I'm still quite a newbie at JSPs. Created my class implementing ServletContextListener, put it in web.xml of my app, restarted Tomcat, checked rotating was working, reloaded the app, and the rotating *still* works! So it seems this IS the solution, and it was definitely non-trivial for me (although it's probably obvious for people with more experience). Many thanks Jacob, Yoav and Tim for your help! I'm going to have a careful look at those logs for a while, and I'll be sure to come back to you if it breaks again :-) Laurent - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: log4j losing log files
Now I'm wondering if this is the problem we had and the dirty fix we used was exactly that - dirty! Tim. - Original Message - From: Laurent Blume [EMAIL PROTECTED] To: Log4J Users List [EMAIL PROTECTED] Sent: Wednesday, October 29, 2003 10:11 AM Subject: Re: log4j losing log files Shapira, Yoav wrote: Howdy, Neither in the JSPs nor the servlet: take Jacob's advice about the ServletContextListener, it's a good one. Ok, I got it! I didn't know about that, I'm still quite a newbie at JSPs. Created my class implementing ServletContextListener, put it in web.xml of my app, restarted Tomcat, checked rotating was working, reloaded the app, and the rotating *still* works! So it seems this IS the solution, and it was definitely non-trivial for me (although it's probably obvious for people with more experience). Many thanks Jacob, Yoav and Tim for your help! I'm going to have a careful look at those logs for a while, and I'll be sure to come back to you if it breaks again :-) Laurent - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: log4j losing log files
Howdy, I don't have a solution to your problem, just a couple of points/questions. ;) The log4j1.2.8.jar file is in each webapp/WEB-INF/lib. Each webapp has its own config file in webapp/WEB-INF/classes. Good. Each config file contains: log4j.rootLogger=DEBUG log4j.logger.dev=DEBUG, dev log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n log4j.appender.dev=org.apache.log4j.RollingFileAppender log4j.appender.dev.File=C:/logs/dev_01.log log4j.appender.dev.layout=org.apache.log4j.PatternLayout log4j.appender.dev.layout.ConversionPattern=%d %X{RemoteHost} %C %-5p %c - %m%n log4j.appender.dev.MaxFileSize=1000KB log4j.appender.dev.MaxBackupIndex=100 Why define the stdout appender if you don't use it? Try removing it. I haven't used the straight rolling file appender much. I mostly use the DailyRollingFileAppender and haven't had a problem with it. Perhaps you could give this a shot (again, as you say you've done it in the past) and see what happens. That way, I can get such a list of files as: .log .log.10 .log.11 .log.12 .log.13 .log.14 .log.15 .log.16 .log.17 .log.18 .log.19 .log.20 .log.49 .log.50 .log.51 .log.52 .log.53 I simply don't have any idea about what is going on. I spend a lot of Add -Dlog4j.debug=true to your JAVA_OPTS when launching tomcats. Log4j will then output internal debugging information to $CATALINA_HOME/logs/catalina.out. Perhaps that will tell you something useful, especially when you see the output generated when log4j is rolling files over. Yoav Shapira This e-mail, including any attachments, is a confidential business communication, and may contain information that is confidential, proprietary and/or privileged. This e-mail is intended only for the individual(s) to whom it is addressed, and may not be saved, copied, printed, disclosed or used by anyone else. If you are not the(an) intended recipient, please immediately delete this e-mail from your computer system and notify the sender. Thank you. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: log4j losing log files
I just joined the list so I missed the original post. Is this about Log4J not rolling over files properly with the RollingFileAppender? We ended up writing a modified version because for some reason it held locks and wouldn't 'rollover' properly, so we lost the log files. Let me know if this is what you are experiencing and I'll dig out the code. Also, what operating system are you running on? Regards, Tim. - Original Message - From: Shapira, Yoav [EMAIL PROTECTED] To: Log4J Users List [EMAIL PROTECTED] Sent: Tuesday, October 28, 2003 5:55 AM Subject: RE: log4j losing log files Howdy, I don't have a solution to your problem, just a couple of points/questions. ;) The log4j1.2.8.jar file is in each webapp/WEB-INF/lib. Each webapp has its own config file in webapp/WEB-INF/classes. Good. Each config file contains: log4j.rootLogger=DEBUG log4j.logger.dev=DEBUG, dev log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n log4j.appender.dev=org.apache.log4j.RollingFileAppender log4j.appender.dev.File=C:/logs/dev_01.log log4j.appender.dev.layout=org.apache.log4j.PatternLayout log4j.appender.dev.layout.ConversionPattern=%d %X{RemoteHost} %C %-5p %c - %m%n log4j.appender.dev.MaxFileSize=1000KB log4j.appender.dev.MaxBackupIndex=100 Why define the stdout appender if you don't use it? Try removing it. I haven't used the straight rolling file appender much. I mostly use the DailyRollingFileAppender and haven't had a problem with it. Perhaps you could give this a shot (again, as you say you've done it in the past) and see what happens. That way, I can get such a list of files as: .log .log.10 .log.11 .log.12 .log.13 .log.14 .log.15 .log.16 .log.17 .log.18 .log.19 .log.20 .log.49 .log.50 .log.51 .log.52 .log.53 I simply don't have any idea about what is going on. I spend a lot of Add -Dlog4j.debug=true to your JAVA_OPTS when launching tomcats. Log4j will then output internal debugging information to $CATALINA_HOME/logs/catalina.out. Perhaps that will tell you something useful, especially when you see the output generated when log4j is rolling files over. Yoav Shapira This e-mail, including any attachments, is a confidential business communication, and may contain information that is confidential, proprietary and/or privileged. This e-mail is intended only for the individual(s) to whom it is addressed, and may not be saved, copied, printed, disclosed or used by anyone else. If you are not the(an) intended recipient, please immediately delete this e-mail from your computer system and notify the sender. Thank you. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: log4j losing log files
Tim Williams wrote: I just joined the list so I missed the original post. Is this about Log4J not rolling over files properly with the RollingFileAppender? Yep! We ended up writing a modified version because for some reason it held locks and wouldn't 'rollover' properly, so we lost the log files. Sounds like what I'm experiencing. I attached my original post, I hope this list will allow it. Let me know if this is what you are experiencing and I'll dig out the code. Also, what operating system are you running on? It's a Windows 2000 box, I think reasonably up-to-date. I'm going to try Yoav's advice right now, and I'll come back to the list with some fresh (and hopefully helpful) debug output! Laurent ---BeginMessage--- Hello all, I hope this is not too much of a newbie question. I did look through the documentation and on the internet, but I might have not done it the right way. Anyway, here is the problem: I'm using log4j 1.2.8 in several webapps, for each it is inside a class used by several JSPs. The application server is Tomcat 4.1.27, running on Windows 2000 SP4 behind an Apache 2.0.47 server on the same machine. The JDK is 1.4.2 (not quite the last one, but close enough). The log4j1.2.8.jar file is in each webapp/WEB-INF/lib. Each webapp has its own config file in webapp/WEB-INF/classes. Each config file contains: log4j.rootLogger=DEBUG log4j.logger.dev=DEBUG, dev log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n log4j.appender.dev=org.apache.log4j.RollingFileAppender log4j.appender.dev.File=C:/logs/dev_01.log log4j.appender.dev.layout=org.apache.log4j.PatternLayout log4j.appender.dev.layout.ConversionPattern=%d %X{RemoteHost} %C %-5p %c - %m%n log4j.appender.dev.MaxFileSize=1000KB log4j.appender.dev.MaxBackupIndex=100 Where dev_01.log is different for each webapp. It mostly works, ie, the dev_01.log file is always there. However, the re are regularly holes in the backup of the file. I saw it happens, when the current log file must be turned over, instead, it simply disappears. The existing backup files are turned over, though. When such a hole in the backups appears, it tends to stay there for a while, but it eventually disappears. That way, I can get such a list of files as: .log .log.10 .log.11 .log.12 .log.13 .log.14 .log.15 .log.16 .log.17 .log.18 .log.19 .log.20 .log.49 .log.50 .log.51 .log.52 .log.53 I simply don't have any idea about what is going on. I spend a lot of time in Tomcat's logs, for developping, and they don't have that problem, but they're rotated on a daily basis. I didn't see anything in those logs about what might be going on. Actually, once I tried to get also a daily rotation for the log4j logs, but the problem got only worse, so I gave up (it was a while ago). Anybody has an idea? Many thanks in advance! Laurent - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] ---End Message--- - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: log4j losing log files
Laurent, We solved the problem with the following code. We extended the Log4J RollingFilleAppender and overrode the rollover() method. All calls to file.renameTo() were replaced with calls to our own rename method. This tries to do a normal file.renameTo, but if it fails it does a manual copy. I can't say this is a solution to be proud of, but it works. The environment is AIX 4.3, JRE 1.1.8. I can't recall the full detials, I don't know if we reproduced in on Windows or not and we couldn;t explain why the original code fails. We are running in an application server, so multiple threads are all calling the logger. We couldn't find anybody else experiencing the same problem when we looked at it 9 months ago. Here's the code anyway. Give it a try, you could always add a println to see if renameTo() ever returns false. Does anybody else have any comments? Tim. /** * Rename file. If fails, copy it. * * @param src The source file * @param target The target file */ private static void renameFile(File src, File target) { if (!src.renameTo(target)) { try { copyFile(src, target); } catch (IOException e) { System.out.println(Can't rename file 'cause + e.getMessage()); } } } /** * Copy a file to another file * * @param source The source file * @param target The target file * @exception IOException */ private static void copyFile(File source, File target) throws IOException { char c = '\u0400'; byte abyte0[] = new byte[c]; FileInputStream fileinputstream = new FileInputStream(source); BufferedInputStream bufferedinputstream = new BufferedInputStream(fileinputstream); FileOutputStream fileoutputstream = new FileOutputStream(target); BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(fileoutputstream); int i; while((i = fileinputstream.read(abyte0)) != -1) { fileoutputstream.write(abyte0, 0, i); } bufferedoutputstream.close(); bufferedinputstream.close(); } - Original Message - From: Laurent Blume [EMAIL PROTECTED] To: Log4J Users List [EMAIL PROTECTED] Sent: Tuesday, October 28, 2003 6:27 AM Subject: Re: log4j losing log files Tim Williams wrote: I just joined the list so I missed the original post. Is this about Log4J not rolling over files properly with the RollingFileAppender? Yep! We ended up writing a modified version because for some reason it held locks and wouldn't 'rollover' properly, so we lost the log files. Sounds like what I'm experiencing. I attached my original post, I hope this list will allow it. Let me know if this is what you are experiencing and I'll dig out the code. Also, what operating system are you running on? It's a Windows 2000 box, I think reasonably up-to-date. I'm going to try Yoav's advice right now, and I'll come back to the list with some fresh (and hopefully helpful) debug output! Laurent - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]