tags:

views:

99

answers:

3

Hello, 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:

[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 ?

+1  A: 

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

http://stackoverflow.com/questions/2810348/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.

hgulyan
Not the same question, your question was about reading a log file, this one is about writing one in a given format.
PhiLho
Did you really read the question? If not, just read the edit I added before you made your comment.
hgulyan
+4  A: 

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.

dpb
Not an option. I would like to stick with the default API.
Andrei Ciobanu
@Andrei For most of the java world Log4J is the standard logging API even though, IIRC, LogBack/SLF4J is better.
KitsuneYMG
@Andrei: maybe this link will be useful then http://javablog.co.uk/2008/07/12/logging-with-javautillogging/
dpb
-1, OP specifically asked how to write a logger
Lord Torgamus
@Lord Torgamus: The OP also said that currently he is using the default Logger and also was wondering if it's possible to format the logs. He also said "where should I look for this functionality.". As a result I answered Log4j. When that was not an option for the OP I provided the link in my comment above which states what basically emem answered later. OK with the downvote, but unless doing it for learning, it is not a good idea to roll your own logger implementation.
dpb
@dpb, if you will edit your answer (perhaps to include that link?) I will un-downvote; I mentally mis-assigned comment authors before, I think. But that said, I am a proponent of answering questions as asked without making any assumptions about why the question is being asked. You are, of course, correct about it being better to use existing implementations in nearly all practical situations.
Lord Torgamus
@Lord Torgamus: I added the link to the answer.
dpb
+5  A: 

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.

emem