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

📄 logwriter.java

📁 java 编写的程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
* File   : $Source: /usr/local/cvs/opencms/src/source/org/apache/java/io/LogWriter.java,v $
* Date   : $Date: 2001/07/31 15:50:20 $
* Version: $Revision: 1.6 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (C) 2001  The OpenCms Group
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* For further information about OpenCms, please see the
* OpenCms Website: http://www.opencms.org 
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
* Copyright (c) 1997-1999 The Java Apache Project.  All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in
*    the documentation and/or other materials provided with the
*    distribution.
*
* 3. All advertising materials mentioning features or use of this
*    software must display the following acknowledgment:
*    "This product includes software developed by the Java Apache 
*    Project for use in the Apache JServ servlet engine project
*    <http://java.apache.org/>."
*
* 4. The names "Apache JServ", "Apache JServ Servlet Engine" and 
*    "Java Apache Project" must not be used to endorse or promote products 
*    derived from this software without prior written permission.
*
* 5. Products derived from this software may not be called "Apache JServ"
*    nor may "Apache" nor "Apache JServ" appear in their names without 
*    prior written permission of the Java Apache Project.
*
* 6. Redistributions of any form whatsoever must retain the following
*    acknowledgment:
*    "This product includes software developed by the Java Apache 
*    Project for use in the Apache JServ servlet engine project
*    <http://java.apache.org/>."
*    
* THIS SOFTWARE IS PROVIDED BY THE JAVA APACHE PROJECT "AS IS" AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE JAVA APACHE PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Java Apache Group. For more information
* on the Java Apache Project and the Apache JServ Servlet Engine project,
* please see <http://java.apache.org/>.
*
*/

package source.org.apache.java.io;

import java.io.*;
import java.text.*;
import java.util.*;
import source.org.apache.java.util.*;

/**
 * A <code>LogWriter</code> allows an application to incapsulate an output
 * stream and dumps its logs on it.
 * <p>
 * To control the different kinds of log messages they are separated into
 * <i>channels</i> that can be activated setting to <i>true</i> the right
 * property.
 * <p>
 * The configurations that control this writer are:
 * <ul>
 *  <li>"identifier"   a boolean value that enables the whole logging
 *  <li>"identifier".dateFormat   the date format used to time stamp
 *  <li>"identifier".timestamp   a boolean value that enables time stamping
 *  <li>"identifier".channel."channelName"  a boolean value that enables
 *  the specified channel
 *
 * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
 * @author <a href="mailto:vt@freehold.crocodile.org">Vadim Tkachenko</a>
 * @version $Revision: 1.6 $ $Date: 2001/07/31 15:50:20 $
 */

public class LogWriter 
  implements Logger {

    /**
     * This string identifies this log writer and its used to
     * differentiate between configurations.
     */
    public static final String DEFAULT_IDENTIFIER = "log";

    /**
     * This string control the standard date format used
     * used for log timestamping.
     */
    public static final String DEFAULT_DATEFORMAT = "[dd/MM/yyyy HH:mm:ss zz] ";

    public static final String KEYWORD_DATEFORMAT = "dateFormat";
    
    public static final String C_TOKEN_CHANNELFORMAT = "%channel";

    public static final String C_DEFAULT_CHANNELFORMAT = "<%channel>";
    
    public static final String C_KEYWORD_CHANNELFORMAT = "channelFormat";
    
    public static final String C_DEFAULT_SEPERATOR = " ";
    
    public static final String C_KEYWORD_SEPERATOR = "channelSeperator";
    
    public static final String KEYWORD_TIMESTAMP = "timestamp";

    public static final String KEYWORD_MEMORY = "memory";
    
    public static final String KEYWORD_CHANNEL = "channel";

    public static final String KEYWORD_FILE = "file";
    
    public static final String KEYWORD_QUEUE_MAXAGE = "queue.maxage";
    
    public static final String KEYWORD_QUEUE_MAXSIZE = "queue.maxsize";

    public static final String CH_QUEUE_STATUS = "info";
        
    /**
     * String identifier for the exception tracing channel.
     */
    public static final String CH_EXCEPTION_TRACING = "servletException";
    
    /**
     * Configuration parameters.
     */
    public Configurations configurations;

    /**
     * Tells if this log is active
     */
    public boolean active = false;

    /**
     * Maximum age of a queued log record, in milliseconds.
     * <p>
     * 
     * When the {@link #logDaemon log daemon} detects the record older than
     * this, it flushes the queue, just to make the observer happy, even if
     * that is a single message in the queue.
     *
     * <p>
     * Default is hardcoded to 5000 ms.
     */
    private long queue_maxage;

    /**   
     * Maximum size of a log queue.
     * <p>
     *
     * When one of the <code>log(...)</code> messages detects that the log
     * queue size is more than this variable permits, it flushes the queue,
     * to keep the memory clean.
     * <p>
     *
     * Default is 1000.
     */
    private long queue_maxsize;

    /**
     * This string identifies this log writer and its used to
     * discriminate between configuration parameters.
     */
    private String identifier;

    /**
     * Tells if the log message should be started by a time stamp.
     * Default is false.
     */
    private boolean timestamp;

    /**
     * Tells if the log message should contain the current memory state.
     * Default is false.
     */
    private boolean memory;
        
    /**
     * The timestamp formatter.
     */
    private SimpleDateFormat formatter;

    /**
     * Format of the channels.
     */
    private String m_channelStart;
    private String m_channelEnd;
    
    /**
     * Seperator token.
     */
    private String m_channelSeperator;
    
    /**
     * The writer encapsulated.
     */
    private PrintWriter writer;

    /**
     * Background logger thread.
     */
    private Agent logDaemon;
    
    /**
     * Tells if the log message should contain a channel.
     * Default is false.
     */
    private boolean logChannel;
    /**
     * Log message queue.
     *
     * Supposed to keep the messages until they get processed by the
     * background logger thread.
     */
    protected SimpleQueue logQueue = new SimpleQueue();

    /**
     * Class implementing the background logging.
     */
    protected class Agent extends Thread {

        /**
         * Wait for the messages in the log message queue and pass 
         * them to the log media, whatever it is.
         */
        public void run() {

            Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

            while (true) {
                try {
                    LogRecord lr = (LogRecord) logQueue.waitObject();
                    write(lr);
                    
                    if ((System.currentTimeMillis() - lr.date) > queue_maxage) {
                    
                        Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
                        
                        // This record goes with the time out of sync, hope noone will notice
                        write(new LogRecord(CH_QUEUE_STATUS, "Log queue age limit exceeded", null));
                        flush();
                        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
                    }
                } catch (InterruptedException iex) {
                    if (!logQueue.isEmpty()) {
                        write(new LogRecord(CH_EXCEPTION_TRACING, "Logger thread interrupted, flushing...", iex));
                        flush();
                        write(new LogRecord(CH_EXCEPTION_TRACING, "Flushed, logging stopped.", null));
                    }
                    return;
                }

⌨️ 快捷键说明

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