Java Tutorial/Log/Log Level

Материал из Java эксперт
Версия от 05:01, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Comparing Log Levels: To compare the severity of two logging levels, use Level.intValue().

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");
    }
  }
}





Creating a Custom Log Level

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");
  }
}





Determining If a Message Will Be Logged

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");
    }
  }
}





Getting the Log Level of a Logger

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;
  }
}





Log finest, finer, config, warning ans severe

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");
  }
}





Setting the Log Level of a Logger

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);
  }
}





Use different logging level

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();
  }
}