Capture javax.net.debug to file

I need to save javax.net.debug = all the output that is generated to the file. I am using log4j and I tried to create a logon proxy, as in the code example below; however, he does not collect information. I am not sure where the javax.net.debug file is printed. I tried to capture system.out and system.err this way, but did not work. Thank you for your help.

public class StdOutErrLog { private static final Logger logger = Logger.getLogger(StdOutErrLog.class); public static void tieSystemOutAndErrToLog() { System.setOut(createLoggingProxy(System.out)); System.setErr(createLoggingProxy(System.err)); } public static PrintStream createLoggingProxy(final PrintStream realPrintStream) { return new PrintStream(realPrintStream) { public void print(final String string) { realPrintStream.print(string); logger.info(string); } }; } } 
+9
java log4j
source share
3 answers

Perhaps the subsystem makes its copy of the values, and you are too late when switching. Try to do this first in your core.

EDIT

OK - I completely missed your idiom. I think you should not use this inner class. You must define an instance of PrintStream in OutputStream that creates a new log entry on each "\ n". The way you do this now misses a lot of the ability to "print" your instance.

 package de.mit.stackoverflow; import java.io.IOException; import java.io.OutputStream; public class LogOutputStream extends OutputStream { private StringBuilder sb = new StringBuilder(); @Override public void write(int b) throws IOException { if (b == '\n') { log(sb.toString()); sb.setLength(0); } else { sb.append((char) b); } } }
package de.mit.stackoverflow; import java.io.IOException; import java.io.OutputStream; public class LogOutputStream extends OutputStream { private StringBuilder sb = new StringBuilder(); @Override public void write(int b) throws IOException { if (b == '\n') { log(sb.toString()); sb.setLength(0); } else { sb.append((char) b); } } } 

and then do

  OutputStream os = new LogOutputStream(); PrintStream ps = new PrintStream(os); System.setOut(ps); 

Perhaps you still want to include a link to the previous thread - on the left as an exercise :-)

+7
source share

This is a long snapshot, but it's possible that overriding print(String) not enough. For example, there is also print(Object) , etc., not to mention the various methods append() and format() .

+3
source share

You need to have a log4j.properties file in class-path (/ WEB-INF / classes /) with the following contents:

log4j.properties file

 datestamp=yyyy-MM-dd/HH:mm:ss.SSS/zzz roll.pattern.hourly=.MM-dd-yyyy.HH roll.pattern.daily=.MM-dd-yyyy log4j.rootLogger=INFO, Console log4j.logger=INFO, Console log4j.appender.Console=org.apache.log4j.DailyRollingFileAppender log4j.appender.Console.DatePattern=${roll.pattern.daily} log4j.appender.Console.file=${catalina.home}/logs/Console.log log4j.appender.Console.MaxFileSize=800KB log4j.appender.Console.MaxBackupIndex=5 log4j.appender.Console.layout=org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=%d{${datestamp}} [%t] %-5p %m%n log4j.appender.custom=org.apache.log4j.DailyRollingFileAppender log4j.appender.custom.DatePattern=${roll.pattern.daily} log4j.appender.custom.File=${catalina.home}/logs/custom.log log4j.appender.custom.MaxFileSize=800KB log4j.appender.custom.MaxBackupIndex=5 log4j.appender.custom.layout=org.apache.log4j.PatternLayout log4j.appender.custom.layout.ConversionPattern=%d{${datestamp}} [%t] %-5p %m%n log4j.logger.net.javax=DEBUG, custom 

This will write your entries to the tomcat / logs / custom.log home directory

Hope this helps you.

+2
source share

Source: https://habr.com/ru/post/650676/


All Articles