0

The standard Java logging defines the following log levels:

  • SEVERE (highest)
  • WARNING
  • INFO
  • CONFIG
  • FINE
  • FINER
  • FINEST

This is incompatible to RFC 5424, because it lacks a NOTICE level between INFO and WARNING. Java lacks some additional log levels (error, critical, alert and emergency) but NOTICE is the one which hurts me.

Is there any way to extend the list of possible log levels in order to add the missing NOTICE level? Or is it necessary to rewrite the whole logging?

3 Answers 3

2

This should work

class Level2 extends Level {
    public static final Level2 NOTICE = new Level2("NOTICE", 850);

    private Level2(String name, int level) {
        super(name, level);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

this is logging level from FINEST = 300 to SEVERE = 1000. INFO = 800 WARNING = 900
How would one use the example (meaning: it could be a bit more verbose)?
2

Thanks to Evgeniy. This is the complete example:

SyslogLevel

import java.util.logging.Level;

public class SyslogLevel extends Level
{
    public static SyslogLevel EMERG   = new SyslogLevel("EMERG",   1100);
    public static SyslogLevel ALERT   = new SyslogLevel("ALERT",   1000);
    public static SyslogLevel ERR     = new SyslogLevel("ERR",     950);
    public static SyslogLevel WARNING = new SyslogLevel("WARNING", 900);
    public static SyslogLevel NOTICE  = new SyslogLevel("NOTICE",  850);
    public static SyslogLevel INFO    = new SyslogLevel("INFO",    800);
    public static SyslogLevel DEBUG   = new SyslogLevel("DEBUG",   300);

    protected SyslogLevel (String name, int value, String resourceBundleName)
    {
        super (name, value, resourceBundleName);
    }

    protected SyslogLevel (String name, int value)
    {
        super (name, value);
    }
}

logging

import java.util.logging.Logger;
import java.util.logging.Level;
import java.util.logging.FileHandler;
import java.util.logging.SimpleFormatter;

public class logging
{
    public static void main(String[] args)
    {
        Logger logger = Logger.getLogger ("MyLog");
        FileHandler fh;
        try {
            fh = new FileHandler ("logging.log", true);
            logger.addHandler (fh);
            logger.setLevel (Level.ALL);
            fh.setFormatter (new SimpleFormatter());
            logger.log (SyslogLevel.NOTICE, "Run!");
        }
        catch (java.io.IOException e) {
            e.printStackTrace ();
        }
    }
}

2 Comments

Note you forgot the "CRITICAL" level in this example. Thanks though, was very useful!
For a better understanding you could add the numeric levels of the original Levels as comments in between...
1

You can extend the Level class and provide more capabilties.

1 Comment

The question was whether and how, but you only answer the first of the two.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.