Java by API/java.util.logging/Level — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 17:43, 31 мая 2010

Level.ALL

  
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.StreamHandler;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
  public static void main(String args[]) {
    Logger log = Logger.getAnonymousLogger();
    setLoggerhandler(log);
    log.finest(new Exception().toString());
  }
  public static void setLoggerhandler(Logger logger) {
    Handler handler = new ExceptionHandler();
    logger.addHandler(handler);
    logger.setLevel(Level.ALL);
  }
}
class ExceptionHandler extends StreamHandler {
  public void publish(LogRecord record) {
    String msg = record.getMessage();
    int exceptionIndex = msg.indexOf("Exception");
    if (exceptionIndex > -1) {
      Pattern pattern = Pattern.rupile("(.*Exception.*)");
      Matcher matcher = pattern.matcher(msg);
      if (matcher != null && matcher.find()) {
        String err = "EXCEPTION FOUND " + matcher.group(1);
        System.out.println(err);
      }
    }
  }
}





Level.FINEST

  

import java.util.logging.Level;
import java.util.logging.Logger;
public class Main {
  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.abc : 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();
  }
}





Level.SEVERE

  

import java.util.logging.Level;
import java.util.logging.Logger;
public class Main {
  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.abc : 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();
  }
}





Level.WARNING

  

// : c15:SimpleFilter.java
// From "Thinking in Java, 3rd ed." (c) Bruce Eckel 2002
// www.BruceEckel.ru. See copyright notice in CopyRight.txt.
import java.util.logging.Filter;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
public class SimpleFilter {
  private static Logger logger = Logger.getLogger("SimpleFilter");
  static class Duck {
  };
  static class Wombat {
  };
  static void sendLogMessages() {
    logger.log(Level.WARNING, "A duck in the house!", new Duck());
    logger.log(Level.WARNING, "A Wombat at large!", new Wombat());
  }
  public static void main(String[] args) {
    sendLogMessages();
    logger.setFilter(new Filter() {
      public boolean isLoggable(LogRecord record) {
        Object[] params = record.getParameters();
        if (params == null)
          return true; // No parameters
        if (record.getParameters()[0] instanceof Duck)
          return true; // Only log Ducks
        return false;
      }
    });
    logger.info("After setting filter..");
    sendLogMessages();
  }
} ///:~