在系统中必不可缺少就是记录系统交互之间的日志信息,以及其他相关的其他的相关的服务信息。那么就必须在QuickServer服务启动的时候,加载相关的信息,常用的场景就是初始化日志等。
QuickServer的源代码如下:
package org.quickserver.net; import org.quickserver.net.server.QuickServer; public abstract interface InitServerHook { public abstract String info(); public abstract void handleInit(QuickServer paramQuickServer) throws Exception; }
如果日志采用JDK的Logger日志类实现,那么可以直接在QuickServer中可以直接使用org.quickserver.util.logging.SimpleJDKLoggingHook.
源代码如下:
package org.quickserver.util.logging; import java.io.File; import java.io.IOException; import java.io.PrintStream; import java.util.logging.FileHandler; import java.util.logging.Level; import java.util.logging.Logger; import org.quickserver.net.InitServerHook; import org.quickserver.net.server.QuickServer; public class SimpleJDKLoggingHook implements InitServerHook { private QuickServer quickserver; public String info() { return "Init Server Hook to setup logging."; } public void handleInit(QuickServer quickserver) throws Exception { Logger logger = null; FileHandler txtLog = null; File log = new File("./log/"); if (!(log.canRead())) log.mkdir(); try { String level = System.getProperty("org.quickserver.util.logging.SimpleJDKLoggingHook.Level"); logger = Logger.getLogger(""); logger.setLevel(Level.FINEST); int count = 20; String temp = System.getProperty("org.quickserver.util.logging.SimpleJDKLoggingHook.Count"); if (temp != null) try { count = Integer.parseInt(temp); } catch (Exception e) { } txtLog = new FileHandler("log/" + quickserver.getName() + "_%u%g.txt", 1048576, count, true); txtLog.setFormatter(new SimpleTextFormatter()); setLevel(txtLog, level); logger.addHandler(txtLog); logger = Logger.getLogger("filesrv"); quickserver.setAppLogger(logger); } catch (IOException e) { System.err.println("Could not create txtLog FileHandler : " + e); throw e; } } private static void setLevel(FileHandler target, String temp) { if (temp == null) { target.setLevel(Level.FINE); return; } temp = temp.toUpperCase(); if (temp.equals("SEVERE")) target.setLevel(Level.SEVERE); else if (temp.equals("WARNING")) target.setLevel(Level.WARNING); else if (temp.equals("INFO")) target.setLevel(Level.INFO); else if (temp.equals("CONFIG")) target.setLevel(Level.CONFIG); else if (temp.equals("FINE")) target.setLevel(Level.FINE); else if (temp.equals("FINER")) target.setLevel(Level.FINER); else if (temp.equals("FINEST")) target.setLevel(Level.FINEST); } }
如果采用Log4j,那么在classpath目录中添加相关的log4j.xml或者log4j.properties.
在public void handleInit(QuickServer quickserver) throws Exception 中添加相关的配置。
采用log必须定制自己的相关的信息
package com.easway.quickserver.object; import java.io.File; import java.io.IOException; import java.util.logging.FileHandler; import java.util.logging.Level; import java.util.logging.Logger; import org.quickserver.net.InitServerHook; import org.quickserver.net.server.QuickServer; import org.quickserver.util.logging.SimpleTextFormatter; /** * 用于在QuickServer启动的时候,同时启动其他的相关的服务 * * @author longgangbai */ public class CustomLoggingHook implements InitServerHook { public String info() { return "初始服务器时初始化日志处理器"; } /** * 初始化QuickServer服务的时候启动相关的日志信息 * */ public void handleInit(QuickServer quickserver) throws Exception { Logger logger = null; FileHandler txtLog = null; File log = new File("./log/"); if (!log.canRead()) log.mkdir(); try { logger = Logger.getLogger("easyway.quickServer"); //设置相关的日志文件处理器 txtLog = new FileHandler("log/easywayQuickServer_%u%g.txt", 1024 * 1024 * 30, 500, true); //设置自定义的文件处理格式 //这个可以自定义类似log4j txtLog.setFormatter(new SimpleTextFormatter()); //设置日志的等级 txtLog.setLevel(Level.INFO); //设置日志Logger的处理器 logger.addHandler(txtLog); //添加相关的日志管理器 quickserver.setAppLogger(logger); } catch (IOException e) { System.err.println("不能创建日志文件 : " + e); throw e; } } }
在quickServer中配置如下:
<init-server-hooks> <class-name> com.easway.quickserver.object.CustomLoggingHook </class-name> </init-server-hooks>