0

I am adding a logger to my program, surfing the net I add to my code a very simple logger with the basic 2-line definition:

private static final Logger LOG = Logger.getLogger(GUI.class.getName());

public MyClass(){

    //other stuff...
    LOG.info("text to log");
    //other stuff...
}

After few hours of developing I was wondering how to format the style of the logged text and I came to this class:

java.util.logging.SimpleFormatter

Reading the Javadoc I can find:

The formatting can be customized by specifying the format string in the java.util.logging.SimpleFormatter.format property.

There are also few examples:

Some example formats:
java.util.logging.SimpleFormatter.format="%4$s: %5$s [%1$tc]%n"
This prints 1 line with the log level (4$), the log message (5$) and the timestamp (1$) in a square bracket.

WARNING: warning message [Tue Mar 22 13:11:31 PDT 2011]

I tried each one of them, but the "format" field is "not visible" (private I suppose) so I can't edit it directly. There isn't any method similar to setStringVar(String regex); to set the var from the outside.

I tried to extends the class public class MyClass extends SimpleFormatter{

but "format" var is not editable. I also tried extending Formatter (the super-class of SimpleFormatter) without success.

The only thing I could do is to extend SimpleFormatter and manually override its: public String format(LogRecord rec); with boring and complicated code.

I was just wondering if is there any way to simply use the inside regex reader of the SimpleFormatter Class

EDIT:errors

Gianmarco
  • 2,536
  • 25
  • 57
  • For specifically setting up the formatter, see http://stackoverflow.com/questions/960099/how-to-set-up-java-logging-using-a-properties-file-java-util-logging – aamit915 Oct 05 '12 at 11:45

2 Answers2

1

Have you looked at the JavaDoc. It gives a overview of the SimpleFormatter.

Also, format is a public method within SimpleFormatter.

Further the line

java.util.logging.SimpleFormatter.format="%4$s: %5$s [%1$tc]%n"

should be defined in a properties file and not the Java class.

To be honest, I don't use java.util.logging I use log4j as Fildor mentioned. I find it much simpler. First you need a config file, like log4j.xml below:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
    <appender name="stdout" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d %5p %c{1}:%L - %m%n"/>
        </layout>
    </appender>
    <root>
        <priority value="debug"></priority>
        <appender-ref ref="stdout"/>
    </root>
</log4j:configuration>

You are free to tweak the conversion pattern, see PatternLayout for details. Then in your class define the following private static member

static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(Testing.class);

The simply when you want to output logging do:

LOG.error("Test Error");
Dave Whittingham
  • 338
  • 3
  • 11
  • Thank you for the help, I did't notice the line that says that the configuration param need to be in a configuration file. Problem solved with: try { LogManager.getLogManager().readConfiguration(new FileInputStream("conf/logger.properties")); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("#### " + LogManager.getLogManager().getProperty("java.util.logging.SimpleFormatter.format")); – Gianmarco Oct 05 '12 at 14:01
  • I was wrong. I can't solve. I just can load the property, but the program seem not to follow my format. Any Idea why? – Gianmarco Oct 05 '12 at 14:29
0

This LINK is about how to configure Java Standard Logging (German). I'm now triying to find one similar in english ...

Meanwhile: I'm not using the util.logging but for what I read in above link, I guess you'll have to implement your own formatter.

Here you go in english.

By the way: I'd recommend log4j or slf4j/logback.

Fildor
  • 14,510
  • 4
  • 35
  • 67
  • why you recommend not standard logging? – Gianmarco Oct 05 '12 at 12:39
  • Quick answer: personal taste. In many projects I work with log4j and find it pretty flexible and in other projects I worked with slf4j and logback, which I love. It is fast (if done right), easy to configure and I like its API. So it's not really that I _dis_courage using java's own logging api, which also has improved much since the early days, I just prefer the other two. Especially when writing libraries. – Fildor Oct 05 '12 at 12:46
  • log4j also has many more appenders than util.logging. You can automatically have rolling files , log to system log, databases ... – Fildor Oct 05 '12 at 12:59
  • I read your links, I read that before, but they tell about extending the class (and that's what I've done) not simply use SimpleFormatter, I was wondering if that "stupid" field "format" is editable as the say [EDIT] and reading the previous answer the fact is that line goes in a configuration file. – Gianmarco Oct 05 '12 at 13:45