从零实现一个日志框架(带源码)
共 11551字,需浏览 24分钟
·
2021-01-19 07:07
阅读本文大概需要 8.2 分钟。
输出内容 - LoggingEvent
日志时间戳
线程信息
日志名称(一般是全类名)
日志级别
日志主体(需要输出的内容,比如info(str))
public class LoggingEvent {
public long timestamp;//日志时间戳
private int level;//日志级别
private Object message;//日志主题
private String threadName;//线程名称
private long threadId;//线程id
private String loggerName;//日志名称
//getter and setters...
@Override
public String toString() {
return "LoggingEvent{" +
"timestamp=" + timestamp +
", level=" + level +
", message=" + message +
", threadName='" + threadName + '\'' +
", threadId=" + threadId +
", loggerName='" + loggerName + '\'' +
'}';
}
}
输出组件 - Appender
public interface Appender {
void append(LoggingEvent event);
}
public class ConsoleAppender implements Appender {
private OutputStream out = System.out;
private OutputStream out_err = System.err;
@Override
public void append(LoggingEvent event) {
try {
out.write(event.toString().getBytes(encoding));
} catch (IOException e) {
e.printStackTrace();
}
}
}
日志级别设计 - Level
ERROR > WARN > INFO > DEBUG > TRACE
public enum Level {
ERROR(40000, "ERROR"), WARN(30000, "WARN"), INFO(20000, "INFO"), DEBUG(10000, "DEBUG"), TRACE(5000, "TRACE");
private int levelInt;
private String levelStr;
Level(int i, String s) {
levelInt = i;
levelStr = s;
}
public static Level parse(String level) {
return valueOf(level.toUpperCase());
}
public int toInt() {
return levelInt;
}
public String toString() {
return levelStr;
}
public boolean isGreaterOrEqual(Level level) {
return levelInt>=level.toInt();
}
}
public class LoggingEvent {
public long timestamp;//日志时间戳
private Level level;//替换后的日志级别
private Object message;//日志主题
private String threadName;//线程名称
private long threadId;//线程id
private String loggerName;//日志名称
//getter and setters...
}
日志打印入口 - Logger
提供error/warn/info/debug/trace几个打印的方法
拥有一个name属性,用于区分不同的logger
调用appender输出日志
拥有自己的专属级别(比如自身级别为INFO,那么只有INFO/WARN/ERROR才可以输出)
public interface Logger{
void trace(String msg);
void info(String msg);
void debug(String msg);
void warn(String msg);
void error(String msg);
String getName();
}
public class LogcLogger implements Logger{
private String name;
private Appender appender;
private Level level = Level.TRACE;//当前Logger的级别,默认最低
private int effectiveLevelInt;//冗余级别字段,方便使用
@Override
public void trace(String msg) {
filterAndLog(Level.TRACE,msg);
}
@Override
public void info(String msg) {
filterAndLog(Level.INFO,msg);
}
@Override
public void debug(String msg) {
filterAndLog(Level.DEBUG,msg);
}
@Override
public void warn(String msg) {
filterAndLog(Level.WARN,msg);
}
@Override
public void error(String msg) {
filterAndLog(Level.ERROR,msg);
}
/**
* 过滤并输出,所有的输出方法都会调用此方法
* @param level 日志级别
* @param msg 输出内容
*/
private void filterAndLog(Level level,String msg){
LoggingEvent e = new LoggingEvent(level, msg,getName());
//目标的日志级别大于当前级别才可以输出
if(level.toInt() >= effectiveLevelInt){
appender.append(e);
}
}
@Override
public String getName() {
return name;
}
//getters and setters...
}
日志层级 - Hierarchy
private LogcLogger parent;//先给增加一个parent属性
private void filterAndLog(Level level,String msg){
LoggingEvent e = new LoggingEvent(level, msg,getName());
//循环向上查找可用的logger进行输出
for (LogcLogger l = this;l != null;l = l.parent){
if(l.appender == null){
continue;
}
if(level.toInt()>effectiveLevelInt){
l.appender.append(e);
}
break;
}
}
日志上下文 - LoggerContext
/**
* 一个全局的上下文对象
*/
public class LoggerContext {
/**
* 根logger
*/
private Logger root;
/**
* logger缓存,存放解析配置文件后生成的logger对象,以及通过程序手动创建的logger对象
*/
private MaploggerCache = new HashMap<>();
public void addLogger(String name,Logger logger){
loggerCache.put(name,logger);
}
public void addLogger(Logger logger){
loggerCache.put(logger.getName(),logger);
}
//getters and setters...
}
日志创建 - LoggerFactory
public interface ILoggerFactory {
//通过class获取/创建logger
Logger getLogger(Class> clazz);
//通过name获取/创建logger
Logger getLogger(String name);
//通过name创建logger
Logger newLogger(String name);
}
public class StaticLoggerFactory implements ILoggerFactory {
private LoggerContext loggerContext;//引用LoggerContext
@Override
public Logger getLogger(Class> clazz) {
return getLogger(clazz.getName());
}
@Override
public Logger getLogger(String name) {
Logger logger = loggerContext.getLoggerCache().get(name);
if(logger == null){
logger = newLogger(name);
}
return logger;
}
/**
* 创建Logger对象
* 匹配logger name,拆分类名后和已创建(包括配置的)的Logger进行匹配
* 比如当前name为com.aaa.bbb.ccc.XXService,那么name为com/com.aaa/com.aaa.bbb/com.aaa.bbb.ccc
* 的logger都可以作为parent logger,不过这里需要顺序拆分,优先匹配“最近的”
* 在这个例子里就会优先匹配com.aaa.bbb.ccc这个logger,作为自己的parent
*
* 如果没有任何一个logger匹配,那么就使用root logger作为自己的parent
*
* @param name Logger name
*/
@Override
public Logger newLogger(String name) {
LogcLogger logger = new LogcLogger();
logger.setName(name);
Logger parent = null;
//拆分包名,向上查找parent logger
for (int i = name.lastIndexOf("."); i >= 0; i = name.lastIndexOf(".",i-1)) {
String parentName = name.substring(0,i);
parent = loggerContext.getLoggerCache().get(parentName);
if(parent != null){
break;
}
}
if(parent == null){
parent = loggerContext.getRoot();
}
logger.setParent(parent);
logger.setLoggerContext(loggerContext);
return logger;
}
}
public class LoggerFactory {
private static ILoggerFactory loggerFactory = new StaticLoggerFactory();
public static ILoggerFactory getLoggerFactory(){
return loggerFactory;
}
public static Logger getLogger(Class> clazz){
return getLoggerFactory().getLogger(clazz);
}
public static Logger getLogger(String name){
return getLoggerFactory().getLogger(name);
}
}
配置文件设计
配置Appender
配置Logger
配置Root Logger
<configuration>
<appender name="std_plain" class="cc.leevi.common.logc.appender.ConsoleAppender">
appender>
<logger name="cc.leevi.common.logc">
<appender-ref ref="std_plain"/>
logger>
<root level="trace">
<appender-ref ref="std_pattern"/>
root>
configuration>
public interface Configurator {
void doConfigure();
}
public class XMLConfigurator implements Configurator{
private final LoggerContext loggerContext;
public XMLConfigurator(URL url, LoggerContext loggerContext) {
this.url = url;//文件url
this.loggerContext = loggerContext;
}
@Override
public void doConfigure() {
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
Document document = documentBuilder.parse(url.openStream());
parse(document.getDocumentElement());
...
}catch (Exception e){
...
}
}
private void parse(Element document) throws IllegalAccessException, ClassNotFoundException, InstantiationException {
//do parse...
}
}
public class ContextInitializer {
final public static String AUTOCONFIG_FILE = "logc.xml";//默认使用xml配置文件
final public static String YAML_FILE = "logc.yml";
private static final LoggerContext DEFAULT_LOGGER_CONTEXT = new LoggerContext();
/**
* 初始化上下文
*/
public static void autoconfig() {
URL url = getConfigURL();
if(url == null){
System.err.println("config[logc.xml or logc.yml] file not found!");
return ;
}
String urlString = url.toString();
Configurator configurator = null;
if(urlString.endsWith("xml")){
configurator = new XMLConfigurator(url,DEFAULT_LOGGER_CONTEXT);
}
if(urlString.endsWith("yml")){
configurator = new YAMLConfigurator(url,DEFAULT_LOGGER_CONTEXT);
}
configurator.doConfigure();
}
private static URL getConfigURL(){
URL url = null;
ClassLoader classLoader = ContextInitializer.class.getClassLoader();
url = classLoader.getResource(AUTOCONFIG_FILE);
if(url != null){
return url;
}
url = classLoader.getResource(YAML_FILE);
if(url != null){
return url;
}
return null;
}
/**
* 获取全局默认的LoggerContext
*/
public static LoggerContext getDefautLoggerContext(){
return DEFAULT_LOGGER_CONTEXT;
}
}
public class StaticLoggerFactory implements ILoggerFactory {
private LoggerContext loggerContext;
public StaticLoggerFactory() {
//构造StaticLoggerFactory时,直接调用配置解析的方法,并获取loggerContext
ContextInitializer.autoconfig();
loggerContext = ContextInitializer.getDefautLoggerContext();
}
}
完整代码
https://github.com/kongwu-/logc
推荐阅读:
一个基于 SpringBoot 开源的小说和漫画在线阅读网站,简洁大方 !强烈推荐 !
微信扫描二维码,关注我的公众号
朕已阅