通过自定制LogManager实现程序完全自定义的logger

 更新时间:2022年03月24日 14:32:40   作者:qingkangxu  
本章主要阐述怎么完全定制化LogManager来实现应用程序完全自定制的logger,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

前一篇博文介绍了JDK logging基础知识 

博文中也提到LogManager,本章主要阐述怎么完全定制化LogManager来实现应用程序完全自定制的logger,其实对于大多数开发者来说,很少有需要定制LogManager的时候,只有是需要单独开发一个产品,需要完全独立的logger机制时才有可能需要定制LogManager,比如:

1,希望自由定制log的输出路径 

2,希望完全定制log的format  

3,希望日志中的国际化信息采用自己定义的一套机制等

当然,对于大型的中间件而言,自定义LogManager则是非常有必要的。

引言

对tomcat熟悉的读者,有可能会注意到tomcat的启动脚本catalina.bat中也使用定制的LogManager,如下:

if not exist "%CATALINA_HOME%\bin\tomcat-juli.jar" goto noJuli
set JAVA_OPTS=%JAVA_OPTS% -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file="%CATALINA_BASE%\conf\logging.properties"

当tomcat的bin路径下存在tomcat-juli.jar文件(也就是存在定制的LogManager)时,那么会强制在JVM系统属性中指定org.apache.juli.ClassLoaderLogManager作为整个JVM的LogManager,以此来完成一些特殊操作。

websphere的启动脚本startServer.bat中也定义了自己的LogManager,如下:

java.util.logging.manager=com.ibm.ws.bootstrap.WsLogManager

怎么实现自定义的LogManager

首先要实现一个继承自java.util.logging.LogManager的类:

子类覆盖java.util.logging.LogManager的addLogger方法,在成功添加logger之后对logger做定制化操作,从代码中可以看出addLogger方法调用了子类的internalInitializeLogger方法,internalInitializeLogger方法中先清空logger的所有handler,然后再增加一个自定义的Handler

需要说明一下:internalInitializeLogger方法中的操作(给logger增设我们自定义的handler)是我们自定义LogManager的一大目的。

package com.bes.logging;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
public class ServerLogManager extends LogManager {
	private static ServerFileHandler handlerSingleton;
	private static ServerLogManager thisInstance;
	private Object lockObj = new Object();
	public ServerLogManager() {
		super();
	}
	public static synchronized ServerLogManager getInstance() {
		if (thisInstance == null) {
			thisInstance = new ServerLogManager();
		}
		return thisInstance;
	}
	public boolean addLogger(Logger logger) {
		boolean result = super.addLogger(logger);
		 //initialize Logger
		if (logger.getResourceBundleName() == null) {
			try {
				Logger newLogger = Logger.getLogger(logger.getName(),
						getLoggerResourceBundleName(logger.getName()));
				assert (logger == newLogger);
			} catch (Throwable ex) {
				//ex.printStackTrace();
			}
		}
		synchronized (lockObj) {
			internalInitializeLogger(logger);
		}
		return result;
	}
	/**
	 * Internal Method to initialize a list of unitialized loggers.
	 */
	private void internalInitializeLogger(final Logger logger) {
		// Explicitly remove all handlers.
		Handler[] h = logger.getHandlers();
		for (int i = 0; i < h.length; i++) {
			logger.removeHandler(h[i]);
		}
		logger.addHandler(getServerFileHandler());
		logger.setUseParentHandlers(false);
		logger.setLevel(Level.FINEST);// only for test
	}
	private static synchronized Handler getServerFileHandler() {
		if (handlerSingleton == null) {
			try {
				handlerSingleton = ServerFileHandler.getInstance();
				handlerSingleton.setLevel(Level.ALL);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return handlerSingleton;
	}
	public String getLoggerResourceBundleName(String loggerName) {
		String result = loggerName + "." + "LogStrings";
		return result;
	}
}

自定义的LogManager中使用到的ServerFileHandler

如下:

该ServerFileHandler是一个把logger日志输出到文件中的handler,可以通过com.bes.instanceRoot系统属性来指定日志文件跟路径;其次,ServerFileHandler也指定了自己的UniformLogFormatter;最后是需要覆盖父类的publish方法,覆盖的publish方法在做真正的日志输入之前会检查日志文件是否存在,然后就是创建一个和日志文件对应的输出流,把该输出流设置为ServerFileHandler的输出流以至日志输出的时候能输出到文件中。另外,WrapperStream仅仅是一个流包装类。

这里也需要说一下:ServerFileHandler构造方法中的setFormatter(new UniformLogFormatter());操作是我们自定义LogManager的第二大目的。

package com.bes.logging;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.logging.LogRecord;
import java.util.logging.StreamHandler;
public class ServerFileHandler extends StreamHandler {
  private WrapperStream wrappedStream;
  private String absoluteFileName = null;
  static final String LOG_FILENAME_PREFIX = &quot;server&quot;;
  static final String LOG_FILENAME_SUFFIX = &quot;.log&quot;;
  private String logFileName = LOG_FILENAME_PREFIX + LOG_FILENAME_SUFFIX;
  public static final ServerFileHandler thisInstance = new ServerFileHandler();
  public static synchronized ServerFileHandler getInstance() {
    return thisInstance;
  }
  protected ServerFileHandler() {
    try {
      setFormatter(new UniformLogFormatter());
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  public synchronized void publish(LogRecord record) {
    if (wrappedStream == null) {
      try {
        absoluteFileName = createFileName();
        openFile(absoluteFileName);
      } catch (Exception e) {
        throw new RuntimeException(
            &quot;Serious Error Couldn't open Log File&quot; + e);
      }
    }
    super.publish(record);
    flush();
  }
  public String createFileName() {
    String instDir = &quot;&quot;;
    instDir = System.getProperty(&quot;com.bes.instanceRoot&quot;);
    if(instDir == null || &quot;&quot;.equals(instDir)){
      instDir = &quot;.&quot;;
    }
    return instDir + &quot;/&quot; + getLogFileName();
  }
  /**
   * Creates the file and initialized WrapperStream and passes it on to
   * Superclass (java.util.logging.StreamHandler).
   */
  private void openFile(String fileName) throws IOException {
    File file = new File(fileName);
    if(!file.exists()){
      if(file.getParentFile() != null &amp;&amp; !file.getParentFile().exists()){
        file.getParentFile().mkdir();
      }
      file.createNewFile();
    }
    FileOutputStream fout = new FileOutputStream(fileName, true);
    BufferedOutputStream bout = new BufferedOutputStream(fout);
    wrappedStream = new WrapperStream(bout, file.length());
    setOutputStream(wrappedStream);
  }
  private class WrapperStream extends OutputStream {
    OutputStream out;
    long written;
    WrapperStream(OutputStream out, long written) {
      this.out = out;
      this.written = written;
    }
    public void write(int b) throws IOException {
      out.write(b);
      written++;
    }
    public void write(byte buff[]) throws IOException {
      out.write(buff);
      written += buff.length;
    }
    public void write(byte buff[], int off, int len) throws IOException {
      out.write(buff, off, len);
      written += len;
    }
    public void flush() throws IOException {
      out.flush();
    }
    public void close() throws IOException {
      out.close();
    }
  }
  protected String getLogFileName() {
    return logFileName;
  }
}

实现Formatter

之前已经提到过,使用logger日志输出的时候,handler会自动调用自己的formatter对日志做format,然后输出格式化之后的日志。自定义的Formatter只需要覆盖public String format(LogRecord record)便可。这个类本身很简单,就是日志输出时自动增加指定格式的时间,加上分隔符,对日志进行国际化处理等操作。 需要注意的是类中对ResourceBundle做了缓存以提高效率。

package com.bes.logging;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.ResourceBundle;
import java.util.logging.Formatter;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
public class UniformLogFormatter extends Formatter {
  private Date date = new Date();
  private HashMap loggerResourceBundleTable;
  private LogManager logManager;
  private static final char FIELD_SEPARATOR = '|';
  private static final String CRLF = System.getProperty(&quot;line.separator&quot;);
  private static final SimpleDateFormat dateFormatter = new SimpleDateFormat(
      &quot;yyyy-MM-dd'T'HH:mm:ss.SSSZ&quot;);
  public UniformLogFormatter() {
    super();
    loggerResourceBundleTable = new HashMap();
    logManager = LogManager.getLogManager();
  }
  public String format(LogRecord record) {
    return uniformLogFormat(record);
  }
  private String uniformLogFormat(LogRecord record) {
    try {
      String logMessage = record.getMessage();
      int msgLength = 150; // typical length of log record
      if (logMessage != null)
        msgLength += logMessage.length();
      StringBuilder recordBuffer = new StringBuilder(msgLength);
      // add date to log
      date.setTime(record.getMillis());
      recordBuffer.append(dateFormatter.format(date)).append(
          FIELD_SEPARATOR);
      // add log level and logger name to log
      recordBuffer.append(record.getLevel()).append(FIELD_SEPARATOR);
      recordBuffer.append(record.getLoggerName()).append(FIELD_SEPARATOR);
      if (logMessage == null) {
        logMessage = &quot;The log message is null.&quot;;
      }
      if (logMessage.indexOf(&quot;{0}&quot;) >= 0) {
        try {
          logMessage = java.text.MessageFormat.format(logMessage,
              record.getParameters());
        } catch (Exception e) {
          // e.printStackTrace();
        }
      } else {
        ResourceBundle rb = getResourceBundle(record.getLoggerName());
        if (rb != null) {
          try {
            logMessage = MessageFormat.format(
                rb.getString(logMessage),
                record.getParameters());
          } catch (java.util.MissingResourceException e) {
          }
        }
      }
      recordBuffer.append(logMessage);
      recordBuffer.append(CRLF);
      return recordBuffer.toString();
    } catch (Exception ex) {
      return &quot;Log error occurred on msg: &quot; + record.getMessage() + &quot;: &quot;
          + ex;
    }
  }
  private synchronized ResourceBundle getResourceBundle(String loggerName) {
    if (loggerName == null) {
      return null;
    }
    ResourceBundle rb = (ResourceBundle) loggerResourceBundleTable
        .get(loggerName);
    if (rb == null) {
      rb = logManager.getLogger(loggerName).getResourceBundle();
      loggerResourceBundleTable.put(loggerName, rb);
    }
    return rb;
  }
} 

 完成了定制的LogManager之后,在启动JVM的命令中增加系统属性便可

java -Djava.util.logging.manager=com.bes.logging.ServerLogManager

加上这个系统属性之后通过java.util.logging.Logger类获取的logger都是经过定制的LogManager作为初始化的,日志输出的时候便会使用上面的ServerFileHandler#publish()方法进行日志输出,并使用UniformLogFormatter对日志进行格式化。

以上就是通过自定制LogManager实现程序完全自定义的logger的详细内容,更多关于自定制LogManager实现自定义logger的资料请关注脚本之家其它相关文章!

相关文章

  • SpringBoot下载文件遇到文件损坏等问题解决方案

    SpringBoot下载文件遇到文件损坏等问题解决方案

    调用接口下载spring boot工程的resources目录下的excel模板文件,非常常见的一个文件下载功能,但是却容易遇到很多坑,下面总结记录下
    2023-10-10
  • 使用kotlin集成springboot开发的超详细教程

    使用kotlin集成springboot开发的超详细教程

    目前大多数都在使用java集成 springboot进行开发,本文演示仅仅将 java换成 kotlin,其他不变的情况下进行开发,需要的朋友可以参考下
    2021-09-09
  • ArrayList源码探秘之Java动态数组的实现

    ArrayList源码探秘之Java动态数组的实现

    这篇文章将带大家从ArrayList源码来探秘一下Java动态数组的实现,文中的示例代码讲解详细,对我们深入了解JavaScript有一定的帮助,需要的可以参考一下
    2023-08-08
  • 一个简单的Python名片管理系统

    一个简单的Python名片管理系统

    这篇文章主要为大家详细介绍了一个简单的Python名片管理系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01
  • Java实现的简单字符串反转操作示例

    Java实现的简单字符串反转操作示例

    这篇文章主要介绍了Java实现的简单字符串反转操作,结合实例形式分别描述了java遍历逆序输出以及使用StringBuffer类的reverse()方法两种字符串反转操作技巧,需要的朋友可以参考下
    2018-08-08
  • mybatis模糊查询之bind标签和concat函数用法详解

    mybatis模糊查询之bind标签和concat函数用法详解

    大家都知道bind 标签可以使用 OGNL 表达式创建一个变量井将其绑定到上下文中,接下来通过本文给大家介绍了mybatis模糊查询——bind标签和concat函数用法,需要的朋友可以参考下
    2022-08-08
  • SpringBoot中配置AOP详解

    SpringBoot中配置AOP详解

    这篇文章主要介绍了SpringBoot中配置AOP详解,Spring Boot 在Spring 的基础上对AOP的配置提供了自动化配置解决方案spring-boot-starter-aop,使开发者能够更加便捷地在Spring Boot项目中使用AOP,需要的朋友可以参考下
    2024-01-01
  • Mybatis 入门之MyBatis环境搭建(第一篇)

    Mybatis 入门之MyBatis环境搭建(第一篇)

    Mybatis的前身叫iBatis,本是apache的一个开源项目, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis。这篇文章主要介绍了Mybatis入门第一篇之MyBaits环境搭建,需要的朋友参考下
    2016-12-12
  • Intellij IDEA安装lombok插件及使用详解

    Intellij IDEA安装lombok插件及使用详解

    今天小编就为大家分享一篇关于Intellij IDEA安装lombok插件及使用详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • Java中的SynchronousQueue阻塞队列使用代码实例

    Java中的SynchronousQueue阻塞队列使用代码实例

    这篇文章主要介绍了Java中的SynchronousQueue阻塞队列使用代码实例,SynchronousQueue是无缓冲区的阻塞队列,即不能直接向队列中添加数据,会报队列满异常,需要的朋友可以参考下
    2023-12-12

最新评论