Java by API/java.util.logging/Logger
Содержание
- 1 Logger: addHandler(Handler handler)
- 2 Logger: config(String msg)
- 3 Logger: exiting(String sourceClass, String sourceMethod, Object result)
- 4 Logger: finer(String msg)
- 5 Logger: fine(String msg)
- 6 Logger: finest(String msg)
- 7 Logger: getAnonymousLogger()
- 8 Logger: getLogger(String name)
- 9 Logger: info(String msg)
- 10 Logger: isLoggable(Level level)
- 11 Logger: logp(Level level, String sourceClass, String sourceMethod, String msg)
- 12 Logger: setFilter(Filter newFilter)
- 13 Logger: setUseParentHandlers(boolean useParentHandlers)
- 14 Logger: severe(String msg)
- 15 Logger: warning(String msg)
Logger: addHandler(Handler handler)
import java.io.IOException;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Logger;
import java.util.logging.XMLFormatter;
public class MainClass {
public static void main(String args[]) {
Logger logger = Logger.getLogger("my.log");
Handler handler = null;
try {
handler = new FileHandler("messages.log");
} catch (IOException e) {
System.out.println("Could not create file. Using the console handler");
handler = new ConsoleHandler();
}
logger.addHandler(handler);
handler.setFormatter(new XMLFormatter());
logger.info("Our first logging message");
logger.severe("Something terrible happened");
}
}
Logger: config(String msg)
import java.util.logging.Logger;
public class Main {
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");
}
}
Logger: exiting(String sourceClass, String sourceMethod, Object result)
import java.util.logging.Level;
import java.util.logging.Logger;
//package com.mycompany;
class MyClass {
public boolean myMethod(int p1, Object p2) {
Logger logger = Logger.getLogger("com.mycompany.MyClass");
if (logger.isLoggable(Level.FINER)) {
logger.entering(this.getClass().getName(), "myMethod", new Object[] { new Integer(p1), p2 });
}
System.out.println("Method body");
boolean result = true;
if (logger.isLoggable(Level.FINER)) {
logger.exiting(this.getClass().getName(), "myMethod", new Boolean(result));
logger.exiting(this.getClass().getName(), "myMethod");
}
return result;
}
}
Logger: finer(String msg)
import java.util.logging.Logger;
public class Main {
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");
}
}
Logger: fine(String msg)
import java.util.logging.Logger;
public class Main {
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");
}
}
Logger: finest(String msg)
import java.util.logging.Logger;
public class Main {
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");
}
}
Logger: getAnonymousLogger()
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);
}
}
}
}
Logger: getLogger(String name)
import java.util.logging.Logger;
public class MainClass {
public static void main(String[] args) {
Logger logger = Logger.getLogger("DeviceLog");
logger.info("<Device id=\"99\"/>");
}
}
Logger: info(String msg)
import java.util.logging.Logger;
public class Main {
private static Logger logger = Logger.getLogger("InfoLogging");
public static void main(String[] args) {
logger.info("Logging an INFO-level message");
}
}
Logger: isLoggable(Level level)
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");
}
}
}
Logger: logp(Level level, String sourceClass, String sourceMethod, String msg)
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main {
private static Logger logger = Logger.getLogger("InfoLogging2");
public static void main(String[] args) {
logger.logp(Level.INFO, "InfoLogging2", "main", "Logging an INFO-level message");
}
}
Logger: setFilter(Filter newFilter)
import java.util.logging.Filter;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
public class Main {
private static Logger logger = Logger.getLogger("SimpleFilter");
static class A {
};
static class B {
};
static void sendLogMessages() {
logger.log(Level.WARNING, "message 1!", new A());
logger.log(Level.WARNING, "message 2!", new B());
}
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;
if (record.getParameters()[0] instanceof A)
return true;
return false;
}
});
logger.info("After setting filter..");
sendLogMessages();
}
}
Logger: setUseParentHandlers(boolean useParentHandlers)
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
public class MainClass {
private static Logger logger = Logger.getLogger("MultipleHandlers2");
public static void main(String[] args) throws Exception {
FileHandler logFile = new FileHandler("MultipleHandlers2.xml");
logger.addHandler(logFile);
logger.addHandler(new ConsoleHandler());
logger.setUseParentHandlers(false);
logger.warning("Output to multiple handlers");
}
}
Logger: severe(String msg)
import java.util.logging.Logger;
public class MainClass {
public static void main(String[] args) {
Logger logger = Logger.getLogger("com.jexp.log");
logger.severe("severe");
logger.warning("warning");
logger.info("info");
logger.config("config");
logger.fine("fine");
logger.finer("finer");
logger.finest("value ="+42);
}
}
Logger: warning(String msg)
import java.util.logging.Logger;
public class Main {
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");
}
}