当前位置: 首页 > 面试题库 >

Log4J:如何将OutputStream或Writer重定向到记录器的writer?

伏建修
2023-03-14
问题内容

我有一个方法在启动后异步运行,使用OutputStream或Writer作为参数。

它充当OutputStream或Writer的记录适配器( 这是我无法更改的第三方API )。

如何将Log4J的内部OutputStream或Writer传递给该方法?
…因为Log4J吞下了System.out和System.err,所以我以前使用过。


问题答案:

我的建议是,为什么不编写OutputStream呢?我正准备为您编写一个,但是我在网上找到了这个很好的例子,请检查一下!

LogOutputStream.java

/*
 * Jacareto Copyright (c) 2002-2005
 * Applied Computer Science Research Group, Darmstadt University of
 * Technology, Institute of Mathematics & Computer Science,
 * Ludwigsburg University of Education, and Computer Based
 * Learning Research Group, Aachen University. All rights reserved.
 *
 * Jacareto is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * Jacareto 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with Jacareto; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

package jacareto.toolkit.log4j;


import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import java.io.OutputStream;

/**
 * This class logs all bytes written to it as output stream with a specified logging level.
 *
 * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
 * @version 1.0
 */
public class LogOutputStream extends OutputStream {
    /** The logger where to log the written bytes. */
    private Logger logger;

    /** The level. */
    private Level level;

    /** The internal memory for the written bytes. */
    private String mem;

    /**
     * Creates a new log output stream which logs bytes to the specified logger with the specified
     * level.
     *
     * @param logger the logger where to log the written bytes
     * @param level the level
     */
    public LogOutputStream (Logger logger, Level level) {
        setLogger (logger);
        setLevel (level);
        mem = "";
    }

    /**
     * Sets the logger where to log the bytes.
     *
     * @param logger the logger
     */
    public void setLogger (Logger logger) {
        this.logger = logger;
    }

    /**
     * Returns the logger.
     *
     * @return DOCUMENT ME!
     */
    public Logger getLogger () {
        return logger;
    }

    /**
     * Sets the logging level.
     *
     * @param level DOCUMENT ME!
     */
    public void setLevel (Level level) {
        this.level = level;
    }

    /**
     * Returns the logging level.
     *
     * @return DOCUMENT ME!
     */
    public Level getLevel () {
        return level;
    }

    /**
     * Writes a byte to the output stream. This method flushes automatically at the end of a line.
     *
     * @param b DOCUMENT ME!
     */
    public void write (int b) {
        byte[] bytes = new byte[1];
        bytes[0] = (byte) (b & 0xff);
        mem = mem + new String(bytes);

        if (mem.endsWith ("\n")) {
            mem = mem.substring (0, mem.length () - 1);
            flush ();
        }
    }

    /**
     * Flushes the output stream.
     */
    public void flush () {
        logger.log (level, mem);
        mem = "";
    }
}


 类似资料:
  • 我只定义了两个记录器:一个是根记录器,另一个是公共记录器。我想那么普通记录器就变成根记录器的直接子级了,对吧?如果是,那么如何防止普通记录器的追加器打印的日志冒泡到根记录器的追加器? 根据文档:

  • 这可能吗?我知道我可以以类似的方式将stdout定向到文件(请参见:将打印重定向到日志文件)

  • 问题内容: 我对jdk日志记录配置有疑问。我有一个使用JDK Logging输出消息的EJB(已部署到glassfish中)。因此,我使用具有以下代码的命名记录器: 我知道可以通过将以下行添加到Glassfish的logging.properties文件中来为记录器配置日志级别: 但是,如何为记录器指定输出文件?我想将来自名为“ org.imixs.workflow”的记录器的所有消息放入单独的文

  • 问题内容: 有没有一种简单的方法可以避免处理文本编码问题? 问题答案: 您确实无法避免处理文本编码问题,但是Apache Commons中已有一些解决方案: 至: 至: 您只需要选择所需的编码即可。

  • 问题内容: 我正在尝试在小型Web搜寻器中使用Selenium来获取页面源。我的输出日志受到selenium日志的入侵,有没有一种方法可以完全禁用日志记录,或者只是以某种方式将其重定向到/ dev / null? 日志消息是: 我通过以下方式调用驱动程序: 问题答案: 好的,我终于设法摆脱了无用的日志记录。这是我所做的。 使用: 摆脱chromedriver日志: 在端口1628上启动Chrome

  • 我想让我的程序记录给定对象的XML使用: 目前,我使用stdout作为输出流。但是stdout没有打印在log4j日志文件中。 如何将此方法的输出重定向到log4j日志文件? 谢啦