如何配置log4j.xml以获取“javax.net.debug”的调试信息

时间:2014-02-25 14:28:09

标签: java debugging log4j

我想获取LDAP连接ssl握手调试信息,所以我将系统属性“javax.net.debug”设置为“ssl,握手”,如下所示:

System.property("javax.net.debug" , "ssl,handshake"); 但我没有得到ssl调试信息。在我的项目中,使用log4j.xml来控制调试信息,所以我想知道如何配置log4j.xml以获取“javax.net.debug”的调试信息?谢谢你的帮助!

4 个答案:

答案 0 :(得分:3)

javax.net包中的类不会写入Log4J appenders(事实上它们根本不使用Log4J库)。最有可能的是,它们会将调试消息输出到正在运行的java进程的STDOUTSTDERR个流。将这些流重定向到磁盘上的文件,然后您可以获取文件中记录的那些消息。如果您正在使用某个服务器(例如Tomcat或JBoss),那么请阅读服务器的文档,了解如何从这些进程控制I / O流。

Log4j.xml此处无关。

编辑:如果您使用的是Tomcat(或Jboss),则可能需要检查ConsoleAppenderlog4j.xml的日志阈值。默认情况下,它设置为INFO并重定向到日志目录中的console.log文件。

答案 1 :(得分:0)

SunJSSE有一个内置的调试工具,由System属性 javax.net.debug (-Djavax.net.debug = all)激活。调试消息打印到STDOUT。

我扩展了java.io.PrintStream类,并通过将所有System.out.print方法调用转发到logger,我在log4j配置的应用程序文件日志中获取了JSSE调试消息。

我只激活重定向,然后应用程序的记录器处于调试级别:

if (logger.isDebugEnabled())
    StdOutLogger.enable();

通过调用以下方式停用重定向:

StdOutLogger.disable();

请查看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(); 
    }
} 

}

答案 2 :(得分:0)

如果您使用的是JBoss,可以配置jboss-log4j.xml将所有STDOUT消息打印到stdout.log。

来自https://access.redhat.com/solutions/68949

的解决方案摘录
  

将以下内容添加到$ 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:不要在上面的代码片段中包含分隔符文本 ...(剪辑)... 。它只是告诉您在配置文件的正确部分插入配置。

答案 3 :(得分:-1)

您是否配置了记录器? (除了你的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>