⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 logwriter.java

📁 Java的面向对象数据库系统的源代码
💻 JAVA
字号:
// You can redistribute this software and/or modify it under the terms of// the Infozone Software License version 2 published by the Infozone Group// (http://www.infozone-group.org).//// Copyright (C) @year@ by The Infozone Group. All rights reserved.//// $Id: LogWriter.java,v 1.1 2002/05/10 08:59:12 per_nyfelt Exp $package org.infozone.tools.logger;import java.io.*;import java.util.*;/**@author <a href="http://www.softwarebuero.de/">SMB</a>@version $Revision: 1.1 $Date: 2002/05/10 08:59:12 $*/public final class LogWriter {    public static final int		INFO	= 1;    public static final int		WARN	= 2;    public static final int		ERROR	= 4;    public static final int		DEBUG	= 8;    public static final int		DEBUG2	= 16;    public static final int		DEBUG3	= 32;    protected static final String	INFO_PREFIX	= "[info] ";    protected static final String	WARN_PREFIX	= "[warn] ";    protected static final String	ERROR_PREFIX	= "[error]";    protected static final String	DEBUG_PREFIX	= "[debug]";    public static final int	LINE_WIDTH = 70;    protected Vector		logTargets;    protected int		allLevels;    public LogWriter() {        logTargets = new Vector();        }    /**    This method allows to quickly find out if there is any log target that    would receive entries of the specified level.    */    public boolean hasTarget (int level) {        return (allLevels & level) > 0;        }    public void addLogTarget (OutputStream _out, int _levels) {        // set also lower debug levels when higher levels are requested        _levels = ((_levels & DEBUG3) > 0) ? (_levels | DEBUG2) : _levels;        _levels = ((_levels & DEBUG2) > 0) ? (_levels | DEBUG) : _levels;        addLogTarget (new PrintWriter (_out), _levels);        }    public void addLogTarget (PrintWriter _writer, int _levels) {        // set also lower debug levels when higher levels are requested        _levels = ((_levels & DEBUG3) > 0) ? (_levels | DEBUG2) : _levels;        _levels = ((_levels & DEBUG2) > 0) ? (_levels | DEBUG) : _levels;        logTargets.addElement (new LogTarget (_writer, _levels));        allLevels = allLevels | _levels;        }    public void newEntry (Object sender, String msg, int levels) {        newEntry (sender, msg, null, levels);        }    public void newEntry (Object sender, String msg, Throwable e, int levels) {        //check if at least one of the specified levels is supported by        //any of the LogTargets        if ((allLevels & levels) > 0) {            for (int i=0; i<logTargets.size(); i++) {                LogTarget logTarget = (LogTarget)logTargets.elementAt (i);                if ((logTarget.levels & levels & INFO) > 0)                    printToWriter (logTarget.writer, sender, msg, INFO_PREFIX, e);                if ((logTarget.levels & levels & WARN) > 0)                    printToWriter (logTarget.writer, sender, msg, WARN_PREFIX, e);                if ((logTarget.levels & levels & ERROR) > 0)                    printToWriter (logTarget.writer, sender, msg, ERROR_PREFIX, e);                if ((logTarget.levels & levels & (DEBUG | DEBUG2 | DEBUG3)) > 0)                    printToWriter (logTarget.writer, sender, msg, DEBUG_PREFIX, e);                }            }        }    protected void printToWriter (PrintWriter writer, Object sender, String msg, String status, Throwable e) {        StringBuffer sb = new StringBuffer (120);        sb.append (status);        sb.append ('(');        String threadNum = String.valueOf(Thread.currentThread().hashCode());        sb.append (threadNum.substring (threadNum.length() - 3));        sb.append (") ");        sb.append (rawClassName (sender));        sb.append (": ");        sb.append (msg);        writer.println (sb);        writer.flush();        if (e != null) {           // writer.print ("    exception: " + e.getUserName());            printFormatedException (writer, e, LINE_WIDTH, "    ");            }        }    public static String rawClassName (Object obj) {        if (obj != null) {            String name = (obj instanceof Class)                ?((Class)obj).getName()                :obj.getClass().getName();            int index = name.lastIndexOf('.');            return name.substring (index+1);            }        else            return "(null)";        }    protected void printFormatedException (OutputStream out, Throwable e, int lineWidth, String pre) {        printFormatedException (new PrintWriter (out), e, lineWidth, pre);        }    protected void printFormatedException (PrintWriter out, Throwable e, int lineWidth, String pre) {        StringWriter buff = new StringWriter();        e.printStackTrace (new PrintWriter (buff));        StringTokenizer st = new StringTokenizer (buff.toString(), "\n");        while (st.hasMoreTokens()) {            String line = st.nextToken();            boolean firstSubLine = true;           // while (line.length() > lineWidth) {            do {                int subLineLength = Math.min (lineWidth, line.length());                String subLine = line.substring (0, subLineLength);                line = line.substring (subLineLength);                if (!firstSubLine)                    out.print ("        ");                out.print (pre);                out.println (subLine);                firstSubLine = false;                }            while (line.length() > 0);            }        out.flush();        }    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -