Andrei Ciobanu
Andrei Ciobanu

Reputation: 12838

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:

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

Upvotes: 1

Views: 1168

Answers (3)

user159088
user159088

Reputation:

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.

Upvotes: 5

michalicekmilan
michalicekmilan

Reputation: 76

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.

Upvotes: 6

hgulyan
hgulyan

Reputation: 8239

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.

Upvotes: 1

Related Questions