开发者

Simple logger, how to?

I want to write a default Logger for my application. Currently I am using the default Java API Class Logger .

I was wondering if it's possible to format my logs to look somthing like this:

开发者_StackOverflow[level] [dd:MM:YYYY] [hh:mm:ss] message

The logger should also be able to print the messages into the System.out and into a file ? Where should I look for this functionality ? Can you please give me some code snippets ?


Extend java.util.logging.Formatter, overide format(LogRecord record) method. LogRecord contains all data you need to build up your custom message.

Then change standard SimpleFormatter in logging.properties file in properties java.util.logging.ConsoleHandler.formatter/java.util.logging.FileHandler.formatter to your formatter.


Have you considered using Log4j?

If that is not an option for you, you could change the output format of the logger you are currently using. The following article shows a way to do just that and provide a custom formatter for the java.util.logging API.

I should also mention that, unless doing this to learn and expand your knowledge, or beeing forced by ugly circumstances, it is never a good idea to write your own logger implementation.


Check this question. I think I was asking a similar one.

A simple log file format

EDIT:

As I wrote there, I found a tool called LogExpert. You can write to a file in a format like "level;dd:MM:YYYY;hh:mm:ss;message" and view it with this tool, changind columnizer to CSV.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜