Reputation: 12838
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
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
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