开发者

How to write to log files in java?

I have made a java application and wants to generate log files so whenever my client would encounter some problem, he can deliver me those log files so that I can correct my code accordingly.

  1. Kindly provide me a small sample program that writes a statement to a log file. Kindly mention the .class files you are开发者_JS百科 using with their full import statements.

  2. The application is multi-threaded so Is it better to generate separate log files for each thread or not?

  3. Is it better to clear all previous log files before starting the program?


macleojw is correct: You should try writing the code yourself.

Here is an overview of the Java logging framework that ships with the JDK. You may wish to check out Commons Logging and Log4J.

Regarding the second part of your question (which was editted out for some reason) I would recommend having all threads log to the same file but logging the thread name along with the log message allowing you to grep the file for a specific thread if required. Also, with most logging frameworks you can configure them to maintain a rolling window of the last N log files rather than explicitly deleting old files when an application starts.


Apache Log4j does everything you require. I hope that you can figure out how to use it on your own.


Take a look at Log4j, and specifically this set of step-by-step examples. It's pretty trivial.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜