java-log入门【目的把日志写入socket】

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lineuman/article/details/82917966

java.util.logging学习

本文灵感来自,果然爽
https://fdx321.github.io/2015/12/20/终于搞清楚Java的日志了

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
看了白衣这篇文章才知道,原来软件还可以这么玩

猜你喜欢

转载自blog.csdn.net/lineuman/article/details/82917966