如何配置 log4j.xml 以获取“javax.net.debug”的调试信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22017119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to configure log4j.xml to get the debug info of "javax.net.debug"
提问by user3104773
I want to get the LDAP connect ssl handshake debug info ,so I set the system property "javax.net.debug" value "ssl,handshake" like this :
我想获取 LDAP 连接 ssl 握手调试信息,所以我设置系统属性“javax.net.debug”值“ssl,handshake”,如下所示:
System.property("javax.net.debug" , "ssl,handshake");
but I didn't get the ssl debug info.In my project,using log4j.xml to control the debug info, so I want to know how to configure log4j.xml to get the debug info of "javax.net.debug"? Thanks for your help!
System.property("javax.net.debug" , "ssl,handshake");
但是我没有得到ssl调试信息。在我的项目中,使用log4j.xml来控制调试信息,所以我想知道如何配置log4j.xml来获取“javax.net.debug”的调试信息?谢谢你的帮助!
回答by Manuel Spigolon
Do you have configured the logger? (in addition to your System.property)
您是否已配置记录器?(除了您的 System.property)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//LOGGER" "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %c.%M:%L - %m%n" />
</layout>
</appender>
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="/var/opt/log/mylogs.log" />
<param name="maxBackupIndex" value="5" />
<param name="maxFileSize" value="5MB" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd/MMM/yyyy HH:mm:ss} %-5p %C{2}.%M - %m%n" />
</layout>
</appender>
<logger name="javax.net.debug">
<level value="DEBUG" />
<appender-ref ref="STDOUT" />
<appender-ref ref="file" />
</logger>
</log4j:configuration>
回答by Bimalesh Jha
Classes inside javax.net
package would not write to Log4J appenders
(infact they do not use Log4J library at all). Most probably they would output debug messages to STDOUT
or STDERR
streams of the running java process. Redirect these streams to file(s) on the disk and then you can get those messages logged in the file(s). If you are using some server (e.g. Tomcat or JBoss) then read the server's document on how to control I/O streams from these processes.
javax.net
包内的类不会写入Log4J appenders
(事实上它们根本不使用 Log4J 库)。最可能的是他们会输出调试消息STDOUT
或STDERR
正在运行的Java程序的流。将这些流重定向到磁盘上的文件,然后您可以将这些消息记录在文件中。如果您正在使用某些服务器(例如 Tomcat 或 JBoss),请阅读有关如何控制来自这些进程的 I/O 流的服务器文档。
Log4j.xml
has nothing to do here.
Log4j.xml
这里无关。
EDIT:If you are using Tomcat (or Jboss) then you may need to check the Log threshold for ConsoleAppender
in log4j.xml
. By default it is set as INFO
and redirected to console.log
file inside logs directory.
编辑:如果您使用的是 Tomcat(或 Jboss),那么您可能需要检查ConsoleAppender
in的日志阈值log4j.xml
。默认情况下,它被设置为INFO
并重定向到console.log
日志目录中的文件。
回答by AllaG
SunJSSE has a built-in debug facility and is activated by the System property javax.net.debug(-Djavax.net.debug=all). The debug messages printed to STDOUT.
SunJSSE 有一个内置的调试工具,由系统属性javax.net.debug(-Djavax.net.debug=all)激活。打印到 STDOUT 的调试消息。
I extended java.io.PrintStream class and by forwarding all System.out.print methods calls to print to the logger, I get the JSSE debug messages in my application file log configured by log4j.
我扩展了 java.io.PrintStream 类,并通过将所有 System.out.print 方法调用转发到记录器来打印,我在由 log4j 配置的应用程序文件日志中获得了 JSSE 调试消息。
I activate the redirection only then application's logger is in debug level:
我只有在应用程序的记录器处于调试级别时才激活重定向:
if (logger.isDebugEnabled())
StdOutLogger.enable();
Deactivate the redirection by calling:
通过调用停用重定向:
StdOutLogger.disable();
Please review StdOutLogger class:
请查看 StdOutLogger 类:
package com.mydomain.util.log4j;
import java.io.PrintStream;
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class StdOutLogger extends PrintStream
{
private static Logger logger =
LoggerFactory.getLogger(StdOutLogger.class);
private static volatile StdOutLogger systemOutLogger;
private static PrintStream originalStream = System.out;
private static boolean isEnabled = false;
private StringBuilder builder;
private String lineSeparator = System.getProperty("line.separator");
private StdOutLogger(PrintStream original)
{
super(original);
builder = new StringBuilder();
logger.debug("Reassign standard output stream to log" + lineSeparator);
}
public static StdOutLogger getInstance()
{
if(systemOutLogger == null)
{
synchronized(StdOutLogger.class)
{
if(systemOutLogger == null)
systemOutLogger = new StdOutLogger(originalStream);
}
}
return systemOutLogger;
}
/**
* Enable forwarding System.out.println calls to the logger
*/
public static void enable()
{
if (isEnabled)
return;
systemOutLogger = StdOutLogger.getInstance();
try
{
System.setOut(systemOutLogger);
isEnabled = true;
}
catch (Exception e)
{
logger.error("Failed to reassign the standard output stream to log: " + e.getMessage());
}
}
/**
* Disable forwarding to the logger resetting the standard output to the console
*/
public static void disable()
{
if (!isEnabled)
return;
try
{
System.setOut(originalStream);
isEnabled = false;
}
catch (Exception e)
{
logger.error("Failed to reassign the standard output stream: " + e.getMessage());
}
systemOutLogger = null;
}
@Override
public void println()
{
originalStream.println();
printToLogger(lineSeparator, false);
}
@Override
public void print(final String s)
{
originalStream.print(s);
printToLogger(s, false);
}
@Override
public void print(final int i)
{
originalStream.print(i);
printToLogger(String.valueOf(i), false);
}
@Override
public void print(final long l)
{
originalStream.print(l);
printToLogger(String.valueOf(l), false);
}
@Override
public void print(final float f)
{
originalStream.print(f);
printToLogger(String.valueOf(f), false);
}
@Override
public void print(final double d)
{
originalStream.print(d);
printToLogger(String.valueOf(d), false);
}
@Override
public void print(final Object obj)
{
originalStream.print(obj);
printToLogger(String.valueOf(obj), false);
}
@Override
public void print(char c)
{
originalStream.print(c);
printToLogger(String.valueOf(c), false);
}
@Override
public void print(boolean b)
{
originalStream.print(b);
printToLogger(String.valueOf(b), false);
}
@Override
public void println(final String s)
{
originalStream.println(s);
printToLogger(s, true);
}
@Override
public void println(final int i)
{
originalStream.println(i);
printToLogger(String.valueOf(i), true);
}
@Override
public void println(final long l)
{
originalStream.println(l);
printToLogger(String.valueOf(l), true);
}
@Override
public void println(final float f)
{
originalStream.println(f);
printToLogger(String.valueOf(f), true);
}
@Override
public void println(final double d)
{
originalStream.println(d);
printToLogger(String.valueOf(d), true);
}
@Override
public void println(final Object obj) {
originalStream.println(obj);
printToLogger(String.valueOf(obj), true);
}
@Override
public void println(char c) {
originalStream.println(c);
printToLogger(String.valueOf(c), true);
}
@Override
public void println(boolean b) {
originalStream.println(b);
printToLogger(String.valueOf(b), true);
}
@Override
public PrintStream append(CharSequence csq, int start, int end)
{
CharSequence cs = (csq == null ? "null" : csq);
printToLogger(cs.subSequence(start, end).toString(), false);
return this;
}
@Override
public void write(int b)
{
originalStream.write(b);
printToLogger(String.valueOf((char)b), false);
}
@Override
public void write(byte buf[], int off, int len)
{
originalStream.write(buf, off, len);
if (buf == null)
{
throw new NullPointerException();
}
else if ((off < 0) || (off > buf.length) || (len < 0) ||
((off + len) > buf.length) || ((off + len) < 0)) {
throw new IndexOutOfBoundsException();
}
else if (len == 0) {
return;
}
byte[] pb = new byte[len];
for (int i = 0 ; i < len ; i++) {
pb[i] = (buf[off + i]);
}
String str = new String(pb);
printToLogger(str, false);
}
@Override
public PrintStream printf(String format, Object ... args)
{
return format(format, args);
}
@Override
public PrintStream format(String format, Object ... args)
{
originalStream.format(format, args);
printToLogger(String.format(format, args), false);
return this;
}
@Override
public PrintStream format(Locale l, String format, Object ... args)
{
originalStream.format(l, format, args);
printToLogger(String.format(format, args), false);
return this;
}
@Override
public void flush()
{
originalStream.flush();
if (builder.length() > 0)
{
logger.debug(builder.toString());
builder = new StringBuilder();
}
//builder.append(lineSeparator);
}
private void printToLogger(String str, boolean isNewLine)
{
if (str.endsWith(lineSeparator))
{
int endIndex = str.lastIndexOf(lineSeparator);
str = str.substring(0, endIndex);
isNewLine = true;
}
builder.append(str);
if (isNewLine)
{
logger.debug(builder.toString());
builder = new StringBuilder();
}
}
}
}
回答by Naren
If you are using JBoss you can configure jboss-log4j.xml to print all STDOUT messages to stdout.log.
如果您使用 JBoss,您可以配置 jboss-log4j.xml 将所有 STDOUT 消息打印到 stdout.log。
Excerpt of solution from : https://access.redhat.com/solutions/68949
解决方案摘录自:https: //access.redhat.com/solutions/68949
Add the following to $JBOSS_HOME/server/$PROFILE/conf/jboss-log4j.xml
将以下内容添加到 $JBOSS_HOME/server/$PROFILE/conf/jboss-log4j.xml
<appender name="STDOUTLOG" class="org.jboss.logging.appender.DailyRollingFileAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="File" value="${jboss.server.log.dir}/stdout.log"/>
<param name="Append" value="false"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<!-- The default pattern: Date Priority [Category] (Thread) Message\n -->
<param name="ConversionPattern" value="%d %-5p [%c] (%t) %m%n"/>
</layout>
</appender>
<appender name="STDERRLOG" class="org.jboss.logging.appender.DailyRollingFileAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="File" value="${jboss.server.log.dir}/stderr.log"/>
<param name="Append" value="false"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<!-- The default pattern: Date Priority [Category] (Thread) Message\n -->
<param name="ConversionPattern" value="%d %-5p [%c] (%t) %m%n"/>
</layout>
</appender>
...(snip)...
<category name="STDOUT" additivity="false">
<priority value="INFO" />
<appender-ref ref="STDOUTLOG"/>
</category>
<category name="STDERR" additivity="false">
<priority value="INFO" />
<appender-ref ref="STDERRLOG"/>
</category>
P.S: Don't include the separator text ...(snip)...in the above code snippet. It is only present to tell you to insert the config in the correct section of the config file.
PS:不要在上面的代码片段中包含分隔符文本...(snip)...。它只是告诉您将配置插入配置文件的正确部分。