Hi all,
While extending a FileAppender class in Log4j, for some reason, I just did not want to use timestamp [%d] given by Log4j while logging the messages.
In my appender, I declared one Calendar instance which was an instance variable for the appender class. I used "Calender.getTime()" method to get current time for the logging message.
Compiled code, put the war file in container, started the application.
Things went fine and log messages were getting written to the file properly. Only problem I faced was about "DATE" in my log messages.
All the messages were showing same timestamp as a log time. :O
Went to appender and saw my code where I was putting CurrentTimestamp in the message before writing it to the file.
Went to check the documentation for Calendar.getTime() and documentation says the right thing as well.
Gets this Calendar's current time.
I was lost.. what could be the problem??
Was looking for some help/hint/thread but nothing was found.
Finally, somebody told me about the bug with Id #4479408. Sun claims that they have fixed this bug, but it doesn't look like that.
You can have a look at it by accessing following link.
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4479408
So, for now, I am making a new instance of Calendar everytime I want to have get a Current Timestamp.
I am not very sure about this approach, how efficient is that, but we all know: "Deadline is sole responsible for bad software quality" and hence I had to put it in my code.
I hope this time Sun not only claim that they have fixed this bug, but really look into the issue and test the fix. :)
Thanks.
Wednesday, July 19, 2006
Wednesday, June 21, 2006
Eclipse - new discovery
Hi,
After a long time I am updating my blog.
Something really new to me, encouraged me to do so.
ECLIPSE - we all use it for developing Java applications.
We all use Build, Build All, Clean while developing applications.
Well.. did you know that we can change the default behaviour of Build and Clean?
i.e. when I say clean, I can ask Eclipse to use my ANT target instead of its default behaviour? (deleting generated class files)
Same is the case with Build.
This is achieved by something called "Builder" in Eclipse.
I know it was always there infront of my eyes, but I never noticed it.
I never bothered to go into it to see what it is all about.
I hope that this blog help other ignorant users like me to explore more into Eclipse.. or atleast into Builders. :)
Have a look at it, its really amazing.
Thanks,
Viraj K. Turakhia
After a long time I am updating my blog.
Something really new to me, encouraged me to do so.
ECLIPSE - we all use it for developing Java applications.
We all use Build, Build All, Clean while developing applications.
Well.. did you know that we can change the default behaviour of Build and Clean?
i.e. when I say clean, I can ask Eclipse to use my ANT target instead of its default behaviour? (deleting generated class files)
Same is the case with Build.
This is achieved by something called "Builder" in Eclipse.
I know it was always there infront of my eyes, but I never noticed it.
I never bothered to go into it to see what it is all about.
I hope that this blog help other ignorant users like me to explore more into Eclipse.. or atleast into Builders. :)
Have a look at it, its really amazing.
Thanks,
Viraj K. Turakhia
Tuesday, December 13, 2005
log4j DailyRollingFileAppender
Wanted to log messages in my J2EE web application in new log file everyday.
Log4j is the best option for that. (BEST.. because I know how to use it :))
Using "DailyRollingFileAppender" is the solution.
It is not as simple as it seems to be.
I conveniently made one log4j.xml, put it in /WEB-INF/classes and started working on other things.
Every morning I saw some weird error message saying something like
log4j:ERROR Failed to rename [logfilename] to [logfilename.2005.12.11]
I also observed that, NO new log file is created.
Searching on the net suggested that error is because of “multiple processes accessing same file”.
Because of multiple processes, accessing my log file, renaming was not possible.
We need to deinitialize/initialize logging mechanism every time we restart the server to terminate processes accessing log files.
To achieve this, I wrote one small listener implementing javax.servlet.ServletContextListener interface.
Everytime when context was destroyed, I shutdown log4j’s logging mechanism using org.apache.log4j.LogManager.shutdown() method.
Shutting down the mechanism made sure that all the processes that are using log files are terminated and this way resolved the issue.
Try it and do let me know whether this solved your problem or not.
Please let me know if there is better solution for this.
Thanks.
Log4j is the best option for that. (BEST.. because I know how to use it :))
Using "DailyRollingFileAppender" is the solution.
It is not as simple as it seems to be.
I conveniently made one log4j.xml, put it in /WEB-INF/classes and started working on other things.
Every morning I saw some weird error message saying something like
log4j:ERROR Failed to rename [logfilename] to [logfilename.2005.12.11]
I also observed that, NO new log file is created.
Searching on the net suggested that error is because of “multiple processes accessing same file”.
Because of multiple processes, accessing my log file, renaming was not possible.
We need to deinitialize/initialize logging mechanism every time we restart the server to terminate processes accessing log files.
To achieve this, I wrote one small listener implementing javax.servlet.ServletContextListener interface.
Everytime when context was destroyed, I shutdown log4j’s logging mechanism using org.apache.log4j.LogManager.shutdown() method.
Shutting down the mechanism made sure that all the processes that are using log files are terminated and this way resolved the issue.
Try it and do let me know whether this solved your problem or not.
Please let me know if there is better solution for this.
Thanks.
Wednesday, October 12, 2005
Tomcat lost connection with MySQL server
While working on a project with Tomcat server and MySQL database, I got an exception saying something like "Lost connection to MySQL server".
This problem I faced every morning at the start of a day.
i.e. If I use my application before leaving for a day and if server was left RUNNING, on the next morning when I try to use application again, above exception was thrown.
Problem here was, MySQL's variable "interactive_timeout".
Definition of "interactive_timeout" from MySQL manual is as follows -
"The number of seconds the server waits for activity on an interactive connection before closing it."
This variable's default value is 28800 i.e. 8 hrs.
So when I kept my server running all the night, no interaction was there on connection and server was closing the connection.
This connection was tried to be used by my application and hence above exception was thrown.
Some of the solutions to this problem are
1. If you are using connection pooling, keep the connection interactive so that it is never idle for ${interactive_timeout} seconds
2. Change the value of variable on MySQL server as per your requirement (This solution, I think, is not the best solution)
3. Before firing any query to server, check whether connection is active or not
Drop a comment to let me know whether this helped you in solving the problem.
Thanks.
This problem I faced every morning at the start of a day.
i.e. If I use my application before leaving for a day and if server was left RUNNING, on the next morning when I try to use application again, above exception was thrown.
Problem here was, MySQL's variable "interactive_timeout".
Definition of "interactive_timeout" from MySQL manual is as follows -
"The number of seconds the server waits for activity on an interactive connection before closing it."
This variable's default value is 28800 i.e. 8 hrs.
So when I kept my server running all the night, no interaction was there on connection and server was closing the connection.
This connection was tried to be used by my application and hence above exception was thrown.
Some of the solutions to this problem are
1. If you are using connection pooling, keep the connection interactive so that it is never idle for ${interactive_timeout} seconds
2. Change the value of variable on MySQL server as per your requirement (This solution, I think, is not the best solution)
3. Before firing any query to server, check whether connection is active or not
Drop a comment to let me know whether this helped you in solving the problem.
Thanks.
Tuesday, October 04, 2005
Tomcat /servlet problem
After coding and deploying my first servlet application on Tomcat 5.0, I entered following URL in browser to test the servlet.
http://machineName:portNumber/webapplication/servlet/servletName
This did not work and I got "HTTP 404" error.
I tried to run same servlet application on Resin and it worked.
After googling a little, I came to know that the file
"installationDirectory"\Apache Software Foundation\Tomcat 5.0\conf\web.xml
is responsible for the mapping "/servlet/servletName" to our servlet.
In newer Tomcat versions, this mapping is disabled for some reason I don’t know.
To enable this /servlet/* mapping, you have to uncomment two things:
- servlet "invoker" in <servlet> and
- servlet mapping for /servlet/* which is mapped to "invoker" servlet in <servlet-mapping>
After uncommenting these two things, restart the server and things should go fine.
Do let me know if this doesn’t work.
If anybody have any idea about why /servlet/* mapping is disabled in Tomcat's latest versions, drop me your comment.
http://machineName:portNumber/webapplication/servlet/servletName
This did not work and I got "HTTP 404" error.
I tried to run same servlet application on Resin and it worked.
After googling a little, I came to know that the file
"installationDirectory"\Apache Software Foundation\Tomcat 5.0\conf\web.xml
is responsible for the mapping "/servlet/servletName" to our servlet.
In newer Tomcat versions, this mapping is disabled for some reason I don’t know.
To enable this /servlet/* mapping, you have to uncomment two things:
- servlet "invoker" in <servlet> and
- servlet mapping for /servlet/* which is mapped to "invoker" servlet in <servlet-mapping>
After uncommenting these two things, restart the server and things should go fine.
Do let me know if this doesn’t work.
If anybody have any idea about why /servlet/* mapping is disabled in Tomcat's latest versions, drop me your comment.
Introduction
Hi,
If you are here to read something philosophical, some of my past experiences, some views of mine on world issues, good English then this blog is not for you.
I am a software engineer and I am creating this for my fellow mates.
In our everyday life, we face lot of problems while coding, configuring servers, logging data.. etc. Here I will share all the problems I faced, while working, along with its solution.
I am in no way responsible if anything goes wrong in your system while you are trying any of the solutions written here.
You can refer this only as a guideline to your problem and solutions, stated here, may differ with version of your APIs or platform you are working on.
I will try my best to put all the version details in my blog while stating problems and while giving solutions to them.
Thanks.
If you are here to read something philosophical, some of my past experiences, some views of mine on world issues, good English then this blog is not for you.
I am a software engineer and I am creating this for my fellow mates.
In our everyday life, we face lot of problems while coding, configuring servers, logging data.. etc. Here I will share all the problems I faced, while working, along with its solution.
I am in no way responsible if anything goes wrong in your system while you are trying any of the solutions written here.
You can refer this only as a guideline to your problem and solutions, stated here, may differ with version of your APIs or platform you are working on.
I will try my best to put all the version details in my blog while stating problems and while giving solutions to them.
Thanks.
Subscribe to:
Posts (Atom)