/*
* 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 = "";
}
}
final Logger logger = LogManager.getLogger(getClass());
cap = new DesiredCapabilities();
cap.setCapability("noReset", "false");
//Build the Appium service
builder = new AppiumServiceBuilder();
builder.withIPAddress("127.0.0.1");
builder.usingPort(4723);
builder.withCapabilities(cap);
builder.withArgument(GeneralServerFlag.SESSION_OVERRIDE);
builder.withArgument(GeneralServerFlag.LOG_LEVEL,"debug");
//Start the server with the builder
service = AppiumDriverLocalService.buildService(builder);
OutputStream outputStream = IoBuilder
.forLogger(logger)
.buildOutputStream();
service.addOutPutStream(outputStream);
service.start();
public class LoggingOutputStream extends OutputStream {
/**
* Default number of bytes in the buffer.
*/
private static final int DEFAULT_BUFFER_LENGTH = 2048;
/**
* Indicates stream state.
*/
private boolean hasBeenClosed = false;
/**
* Internal buffer where data is stored.
*/
private byte[] buf;
/**
* The number of valid bytes in the buffer.
*/
private int count;
/**
* Remembers the size of the buffer.
*/
private int curBufLength;
/**
* The logger to write to.
*/
private Logger log;
/**
* The log level.
*/
private Level level;
/**
* Creates the Logging instance to flush to the given logger.
*
* @param log the Logger to write to
* @param level the log level
* @throws IllegalArgumentException in case if one of arguments
* is null.
*/
public LoggingOutputStream(final Logger log,
final Level level)
throws IllegalArgumentException {
if (log == null || level == null) {
throw new IllegalArgumentException(
"Logger or log level must be not null");
}
this.log = log;
this.level = level;
curBufLength = DEFAULT_BUFFER_LENGTH;
buf = new byte[curBufLength];
count = 0;
}
/**
* Writes the specified byte to this output stream.
*
* @param b the byte to write
* @throws IOException if an I/O error occurs.
*/
public void write(final int b) throws IOException {
if (hasBeenClosed) {
throw new IOException("The stream has been closed.");
}
// don't log nulls
if (b == 0) {
return;
}
// would this be writing past the buffer?
if (count == curBufLength) {
// grow the buffer
final int newBufLength = curBufLength +
DEFAULT_BUFFER_LENGTH;
final byte[] newBuf = new byte[newBufLength];
System.arraycopy(buf, 0, newBuf, 0, curBufLength);
buf = newBuf;
curBufLength = newBufLength;
}
buf[count] = (byte) b;
count++;
}
/**
* Flushes this output stream and forces any buffered output
* bytes to be written out.
*/
public void flush() {
if (count == 0) {
return;
}
final byte[] bytes = new byte[count];
System.arraycopy(buf, 0, bytes, 0, count);
String str = new String(bytes);
log.log(level, str);
count = 0;
}
/**
* Closes this output stream and releases any system resources
* associated with this stream.
*/
public void close() {
flush();
hasBeenClosed = true;
}
}
import java.io.OutputStream;
import org.slf4j.Logger;
public class LogOutputStream extends OutputStream {
private final Logger logger;
/**The internal memory for the written bytes. */
private StringBuffer mem;
public LogOutputStream( final Logger logger ) {
this.logger = logger;
mem = new StringBuffer();
}
@Override
public void write( final int b ) {
if ( (char) b == '\n' ) {
flush();
return;
}
mem = mem.append( (char) b );
}
@Override
public void flush() {
logger.info( mem.toString() );
mem = new StringBuffer();
}
}
Looking through the answers here, it seems that none of them account clearly for decoding of bytes into Strings (extends CharSequence). byte s are char s are not equivalent (see OutputStream vs Writer in Java). A simple non-latin character e.g. 羼 may be represented as a series of bytes: E7 BE BC (UTF-8 sequence of 羼). 其他人不考虑特定编码的原因:
(char) b will transform non-latin characters without interpreting UTF-8, so 羼 becomes ç¾¼ , or "Róbert" becomes "Róbert" (oh, home many times I've seen this). You might be more familiar with this beauty:  (UTF-8 BOM)
new String(bytes)使用平台的默认字符集”“创建一个String“”,这取决于您运行代码的位置,因此您可能会在服务器和本地计算机上获得不同的行为。这比(char)b好,因为您至少可以指定编码。
(no字节数组、索引、副本、字符串构造函数等) 注意:我使用这个命令将Selenium ChromeDriver的输出(默认为stderr)重定向到SLF 4J over Log4J 2:
val service = ChromeDriverService.createServiceWithConfig(options).apply {
sendOutputTo(loggerStream(LoggerFactory.getLogger(ChromeDriver::class.java)::info))
}
val driver = ChromeDriver(service, options)
7条答案
按热度按时间33qvvth11#
我的建议是,你为什么不写你的输出流呢?!我正准备为你写一个,但是我在网上找到了这个很好的例子,看看吧!
LogOutputStream.java
unhi4e5o2#
您可以使用Log4j IOStreams
IOStreams组件是一个Log4j API扩展,它提供了来自www.example.com的许多类java.io,这些类可以在写入另一个OutputStream或Writer的同时写入Logger,或者InputStream或Reader读取的内容可以被Logger窃听。
您可以使用下列方式建立OutputStream:
下面是Appium的一个示例,以编程方式启动它,并使用log4j控制其日志。
希望这对你有帮助!
r6l8ljro3#
来源:http://sysgears.com/articles/how-to-redirect-stdout-and-stderr-writing-to-a-log4j-appender/
块引用
Log4j不允许捕捉现成的stdout和stderr消息,但是,如果您使用第三方组件,并且必须记录它们刷新到流中的消息,那么您可以做一些小技巧,实现支持日志记录的自定义输出流。
Jim摩尔已经完成了这一点(请参阅log4j源代码中的LoggingOutputStream)。唯一的问题是,JimMoore的LoggingOutputStream需要org.apache.log4j.Category和org.apache.log4j.Priority,它们现在部分被弃用。
下面是修改后的LoggingOutputStream,它避免了不赞成使用的方法:
现在,您可以通过以下方式捕获刷新到stderr或stdout的消息:
log4j.properties配置:
系统齿轮公司Dmitriy Pavlenko
块引用
ctzwtxfj4#
在Arthur Neves答案的基础上,我将其转换为Slf4J。我还使用StringBuffer对它进行了一些改进,并直接将字节转换为字符:
jvlzgdj95#
Looking through the answers here, it seems that none of them account clearly for decoding of bytes into Strings (extends CharSequence).
byte
s arechar
s are not equivalent (see OutputStream vs Writer in Java). A simple non-latin character e.g. 羼 may be represented as a series of bytes: E7 BE BC (UTF-8 sequence of 羼).其他人不考虑特定编码的原因:
(char) b
will transform non-latin characters without interpreting UTF-8, so 羼 becomesç¾¼
, or "Róbert" becomes "Róbert" (oh, home many times I've seen this). You might be more familiar with this beauty:
(UTF-8 BOM)new String(bytes)
使用平台的默认字符集”“创建一个String“”,这取决于您运行代码的位置,因此您可能会在服务器和本地计算机上获得不同的行为。这比(char)b
好,因为您至少可以指定编码。log4j-iostreams
的IoBuilder默认也使用平台的默认字符集,但是是可配置的。而且这不是一个通用的解决方案,只有当你在Log4j 2上使用SLF 4J时才起作用。尽管这是OP的问题😅。使用此解决方案:
charset = Charsets.UTF_8
,必要时可更改)(no字节数组、索引、副本、字符串构造函数等)
注意:我使用这个命令将Selenium ChromeDriver的输出(默认为stderr)重定向到SLF 4J over Log4J 2:
jmo0nnb36#
在阅读了https://stackoverflow.com/a/6996147/1773490的答案之后,我开始查看现有的
OutputStream
实现,我偶然发现了org.apache.commons.exec.LogOutputStream
。您只需要将它包含到项目中,就像使用Maven时一样:
这是我在一个项目中使用的一个实现示例,该项目使用Selenium和Chrome驱动程序以及Google的Flogger:
通常不应该有任何奇怪的
logLevel
,但这种极端情况是通过找到最接近的已知日志级别来管理的。希望这对你有帮助😊
y0u0uwnf7#
由于上一个指向Log4J的Writer示例已停止:http://www.opensource.apple.com/source/JBoss/JBoss-737/jboss-all/common/src/main/org/jboss/logging/util/LoggerWriter.java