参考资料:http://nudtgk2000.iteye.com/blog/1716379
首先,在http://www.apache.org/dyn/closer.cgi/logging/log4j/1.2.17/log4j-1.2.17.zip下载log4j包
然后,在原工程里导入包
按照参考资料的提示,一步步来
首先,新建package appender, new 一个class ThreadSeperateDailyRollingFileAppender,该class继承自DailyRollingFileAppender ,下面是源代码
package appender; import java.io.File; import java.io.IOException; import org.apache.log4j.DailyRollingFileAppender; import org.apache.log4j.Layout; /** * @author Kevin Kwok */ public class ThreadSeperateDailyRollingFileAppender extends DailyRollingFileAppender { public ThreadSeperateDailyRollingFileAppender() {} public ThreadSeperateDailyRollingFileAppender(Layout layout, String datePattern) throws IOException { // 改动只有这点:以线程名命名日志文件 super(layout, "log" + File.separator + Thread.currentThread().getName(), datePattern); } }
然后,新建一个package threadlogger, new 一个class ThreadLogger, 下面是源代码
package threadlogger; import java.io.IOException; import org.apache.log4j.ConsoleAppender; import org.apache.log4j.Level; import org.apache.log4j.Logger; import org.apache.log4j.PatternLayout; import appender.ThreadSeperateDailyRollingFileAppender; /** * @author Kevin Kwok */ public class ThreadLogger { ThreadLogger() {}; public static Logger getLogger() { Logger logger = null; // 创建一个Logger实例, 就以线程名命名 logger = Logger.getLogger(Thread.currentThread().getName()); PatternLayout layout = new PatternLayout("%-4r %-5p [%d{yyyy-MM-dd HH:mm:ss,SSS}] %l%t: %m%n"); // 控制台输出 ConsoleAppender concoleAppender = new ConsoleAppender(layout, "System.out"); // 文件输出 ThreadSeperateDailyRollingFileAppender R = null; try { R = new ThreadSeperateDailyRollingFileAppender(layout, "'.'yyyy-MM-dd'.log'"); } catch (IOException e) { e.printStackTrace(); } // 参数配置, 因为没有找到仅靠配置文件的办法, 只好放在这里设 R.setAppend(false); R.setImmediateFlush(true); R.setThreshold(Level.WARN); // 绑定到Logger logger.setLevel(Level.DEBUG); logger.addAppender(concoleAppender); logger.addAppender(R); return logger; } }
最后,在原来建立线程的类里调用刚刚写的函数就可以了:
导入
import org.apache.log4j.Logger;
import threadlogger.ThreadLogger;
import testlog.ThreadBody;
之后,最关键的就是对run函数的改造,因为独立的Logger实例要在run方法内实现,下面是源代码
private Socket client; public LocalSocketServer(Socket c) { this.client=c; } public void run() { try{ BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream())); PrintWriter out=new PrintWriter(client.getOutputStream()); String str=in.readLine(); System.out.println(str); out.println(indexSearch(str)); out.flush(); client.close(); Logger logger = ThreadLogger.getLogger(); logger.warn(Thread.currentThread().getName() + " started!"); logger.debug("this is debug"); logger.info("this is info"); logger.warn("this is warn"); logger.error("this is error"); logger.warn(Thread.currentThread().getName() + " finished!"); }catch(IOException ex){ }finally{ } } static Logger logger = ThreadLogger.getLogger(); public static void main(String[] args) throws IOException { logger.warn(LocalSocketServer.class + " started!"); ThreadBody threadBody = new ThreadBody(); ServerSocket server=new ServerSocket(5678); while (true) { LocalSocketServer lss=new LocalSocketServer(server.accept()); lss.start(); logger.debug("this is debug"); logger.info("this is info"); logger.warn("this is warn"); logger.error("this is error"); } } }
最后的结果就是,在工程里的log文件夹下,出现了以各自线程名为名的日志文件,记录了各自线程的信息。