java.util.logging学习
本文灵感来自,果然爽
https://fdx321.github.io/2015/12/20/%E7%BB%88%E4%BA%8E%E6%90%9E%E6%B8%85%E6%A5%9AJava%E7%9A%84%E6%97%A5%E5%BF%97%E4%BA%86
http://tutorials.jenkov.com/java-logging/formatters.html
LogRecord
LogRecord objects 可以看成是一条日志
import java.util.logging.Logger;
这是一个工厂方法
Logger logger = Logger.getLogger(“myLogger”);
你可以调用logger的方法进行输出日志,方法的签名可以看源码
logger.info(“hello wolrd”)
Adding and Removing Handlers
logger.addHandler(new ConsoleHandler());
你也可以查看handlers
logger.getHandlers()
Setting a Log Filter
@FunctionalInterface
public interface Filter {
/**
* Check if a given log record should be published.
* @param record a LogRecord
* @return true if the log record should be published.
*/
public boolean isLoggable(LogRecord record);
}
Setting the Log Level
Java Logging: Logger Hierarchy
日志名字是通过.进行划分的, 会逐层向上找父日志
message的处理流程
When a message is passed to a Logger, the message is passed through the Logger’s Filter, if the Logger has a Filter set. The Filter can either accept or reject the message. If the message is accepted, the message is forwarded to the Handler’s set on the Logger. If no Filter is set, the message is always accepted.
日志handler
ConsoleHandler
FileHandler
StreamHandler
SocketHandler 【这里我有想法,我怎么生成一个监听服务呢】
MemoryHandler
日志格式
ConsoleHandler handler = new ConsoleHandler();
handler.setFormatter(new SimpleFormatter());
Formatter formatter = handler.getFormatter();
学习完这篇文章之后我的独特使用!
把日志写入socket的一个演示
http://calvin1978.blogcn.com/articles/btrace1.html
看了白衣这篇文章才知道,原来软件还可以这么玩