The logging levels are:
Level | Description |
SEVERE | The highest value; intended for extremely important messages (e.g. fatal program errors). |
WARNING | Intended for warning messages. |
INFO | Informational runtime messages. |
CONFIG | Informational messages about configuration settings/setup. |
FINE | Used for greater detail, when debugging/diagnosing problems. |
FINER | Even greater detail. |
FINEST | The lowest value; greatest detail. |
So an example would look something like this:
ackage com.wownow;
import java.io.IOException;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import java.util.logging.XMLFormatter;
public class MyLoggingTest2 {
public static void main(String[] args) throws IOException {
Level consoleLogLevel = Level.CONFIG;
Level defaultLogLevel = Level.FINER;
ConsoleHandler ch = new ConsoleHandler();
ch.setLevel(consoleLogLevel);
FileHandler txtfh = new FileHandler("MyLogFile.txt");
txtfh.setFormatter(new SimpleFormatter());
txtfh.setLevel(defaultLogLevel);
FileHandler xmlfh = new FileHandler("MyLogFile.xml");
xmlfh.setFormatter(new XMLFormatter());
xmlfh.setLevel(defaultLogLevel);
Logger logger = Logger.getLogger("MyLogger");
logger.setUseParentHandlers(false);
logger.setLevel(defaultLogLevel);
logger.addHandler(ch);
logger.addHandler(txtfh);
logger.addHandler(xmlfh);
logger.severe("My Severe Message");
logger.warning("My Warning Message");
logger.info("My Info Message");
logger.config("My Config Message");
logger.fine("My Fine Message");
logger.finer("My Finer Message");
logger.finest("My Finest Message");
txtfh.close();
xmlfh.close();
}
}
import java.io.IOException;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import java.util.logging.XMLFormatter;
public class MyLoggingTest2 {
public static void main(String[] args) throws IOException {
Level consoleLogLevel = Level.CONFIG;
Level defaultLogLevel = Level.FINER;
ConsoleHandler ch = new ConsoleHandler();
ch.setLevel(consoleLogLevel);
FileHandler txtfh = new FileHandler("MyLogFile.txt");
txtfh.setFormatter(new SimpleFormatter());
txtfh.setLevel(defaultLogLevel);
FileHandler xmlfh = new FileHandler("MyLogFile.xml");
xmlfh.setFormatter(new XMLFormatter());
xmlfh.setLevel(defaultLogLevel);
Logger logger = Logger.getLogger("MyLogger");
logger.setUseParentHandlers(false);
logger.setLevel(defaultLogLevel);
logger.addHandler(ch);
logger.addHandler(txtfh);
logger.addHandler(xmlfh);
logger.severe("My Severe Message");
logger.warning("My Warning Message");
logger.info("My Info Message");
logger.config("My Config Message");
logger.fine("My Fine Message");
logger.finer("My Finer Message");
logger.finest("My Finest Message");
txtfh.close();
xmlfh.close();
}
}
In this example we:
- Create two level variables,one for the console and one for the files
- Then create a console handler, and set the level of this handler, in the case to "CONFIG"
- We then create a handler for the text file, set its formatter to SimpleFormatter, so the output is in plain text, then set its level
- Then we create a third handler for the xml and set its formater to XMLFormatter (this could be ommitted as it's the default), and again set its level
- The logger object is then created, we use setUseParentHandlers to false to stop duplicated messages appearing on the console
- The logger level is set, this assumes the defaultLogLevel is lower than the consoleLogLevel, otherwise you could use Level.ALL
- Next the three handlers are added to the logger
- Next we output a message for each logging level. Only the messages above the level "CONFIG" will be output to the console or "FINER" written to the file.
- We then close the file handlers.
My Done Message
No comments:
Post a Comment