Java Tutorial/Log/Log Level — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 08:01, 1 июня 2010
Содержание
- 1 Comparing Log Levels: To compare the severity of two logging levels, use Level.intValue().
- 2 Creating a Custom Log Level
- 3 Determining If a Message Will Be Logged
- 4 Getting the Log Level of a Logger
- 5 Log finest, finer, config, warning ans severe
- 6 Setting the Log Level of a Logger
- 7 Use different logging level
Comparing Log Levels: To compare the severity of two logging levels, use Level.intValue().
<source lang="java">
import java.util.logging.Level; public class Main {
public static void main(String[] argv) throws Exception { Level level1 = Level.INFO; Level level2 = Level.CONFIG; if (level1.intValue() > level2.intValue()) { System.out.println("level1 is more severe"); } else if (level1.intValue() < level2.intValue()) { System.out.println("level2 is more severe"); } else { System.out.println("level1 == level2"); } }
}</source>
Creating a Custom Log Level
<source lang="java">
import java.util.logging.Level; import java.util.logging.Logger; class MyLevel extends Level {
public static final Level DISASTER = new MyLevel("DISASTER", Level.SEVERE.intValue() + 1); public MyLevel(String name, int value) { super(name, value); }
} public class Main {
public static void main(String[] argv) throws Exception { Logger logger = Logger.getLogger("com.mycompany"); logger.log(MyLevel.DISASTER, "my disaster message"); Level disaster = Level.parse("DISASTER"); logger.log(disaster, "my disaster message"); }
}</source>
Determining If a Message Will Be Logged
<source lang="java">
import java.util.logging.Level; import java.util.logging.Logger; public class Main {
public static void main(String[] argv) throws Exception { Logger logger = Logger.getLogger("com.mycompany.MyClass"); // Check if the message will be logged if (logger.isLoggable(Level.FINEST)) { logger.finest("my finest message"); } }
}</source>
Getting the Log Level of a Logger
<source lang="java">
import java.util.logging.Level; import java.util.logging.Logger; public class Main {
public static void main(String[] argv) throws Exception { } // Return the level of the specified logger. public static Level getLevel(Logger logger) { Level level = logger.getLevel(); while (level == null && logger.getParent() != null) { logger = logger.getParent(); level = logger.getLevel(); } return level; }
}</source>
Log finest, finer, config, warning ans severe
<source lang="java">
import java.util.logging.Logger; public class Logging {
public static void main(String[] args) { Logger log = Logger.getLogger("global"); log.finest("A"); log.finer("B"); log.fine("C"); log.config("D"); log.info("E"); log.warning("O"); log.severe("A"); }
}</source>
Setting the Log Level of a Logger
<source lang="java">
import java.util.logging.Level; import java.util.logging.Logger; public class Main {
public static void main(String[] argv) throws Exception { // Get a logger Logger logger = Logger.getLogger("com.mycompany"); // Set the level to a particular level logger.setLevel(Level.INFO); // Set the level to that of its parent logger.setLevel(null); // Turn off all logging logger.setLevel(Level.OFF); // Turn on all logging logger.setLevel(Level.ALL); }
}</source>
Use different logging level
<source lang="java">
import java.util.logging.Level; import java.util.logging.Logger; public class MainClass {
private static Logger lgr = Logger.getLogger("com"), lgr2 = Logger.getLogger("com.jexp"), util = Logger.getLogger("com.jexp.util"), test = Logger.getLogger("com.jexp.test"), rand = Logger.getLogger("random"); private static void logMessages() { lgr.info("com : info"); lgr2.info("com.bruceeckel : info"); util.info("util : info"); test.severe("test : severe"); rand.info("random : info"); } public static void main(String[] args) { lgr.setLevel(Level.SEVERE); System.out.println("com level: SEVERE"); logMessages(); util.setLevel(Level.FINEST); test.setLevel(Level.FINEST); rand.setLevel(Level.FINEST); System.out.println("individual loggers set to FINEST"); logMessages(); lgr.setLevel(Level.SEVERE); System.out.println("com level: SEVERE"); logMessages(); }
}</source>