简体   繁体   English

想让“System.out.println()”打印消息到 log4j

[英]Want to make “System.out.println()” to print message to log4j

our project uses System.out.println() or System.err.println() to output debugging message.我们的项目使用 System.out.println() 或 System.err.println() 到 output 调试消息。 Now we want to use log4j to direct these output to log files not just console.现在我们想使用 log4j 将这些 output 引导到日志文件而不仅仅是控制台。 To avoid impact or risk, we don't want to do replace all "System.out.println()" with "logger.debug" but just want to maybe write several code to make these output redirect to log4j log files that is no code change in each class still using System.out.println() (we know this is bad practice but we don't want to do global search and replace currently).为避免影响或风险,我们不想将所有“System.out.println()”替换为“logger.debug”,而只想编写几个代码使这些 output 重定向到 log4j 日志文件每个 class 中的代码更改仍在使用 System.out.println() (我们知道这是不好的做法,但我们目前不想进行全局搜索和替换)。 Is it possible?可能吗? Thanks.谢谢。

I guess it is possible, though it is definitely a bad idea.我想这是可能的,尽管这绝对是个坏主意。 You should bite the bullet and replace all of the System.out.printXXx() and System.err.printXXx() calls.您应该硬着头皮替换所有System.out.printXXx()System.err.printXXx()调用。

(You don't have to do this in one big change, and you don't have to do it using an automated search / replace... if these things make you feel uncomfortable.) (您不必在一次大的更改中执行此操作,也不必使用自动搜索/替换来执行此操作……如果这些事情让您感到不舒服。)


You can change where those streams go by using System.setOut and System.setErr to point them to a log4j log file.您可以更改这些流 go 的位置,方法是使用System.setOutSystem.setErr将它们指向 log4j 日志文件。 However:然而:

  • you may get problems with interleaving of output and/or log file truncation, depending on exactly how the log4j appenders are implemented您可能会遇到 output 交错和/或日志文件截断问题,具体取决于 log4j 附加程序的实现方式
  • you won't get log4j time stamps, etc你不会得到 log4j 时间戳等
  • you won't have the ability to control the logging via the log4j configs, because your redirection is going behind the back of log4j您将无法通过 log4j 配置控制日志记录,因为您的重定向在 log4j 后面
  • log4j logfile rotation won't work properly log4j 日志文件轮换无法正常工作

A more complicated alternative would to create special OutputStream object to intercept the output and turn it into calls to a log4j Logger.更复杂的替代方法是创建特殊的 OutputStream object 来拦截 output 并将其转换为对 log4j 记录器的调用。 But this also would have problems too:但这也会有问题:

  • it could be tricky figuring out where each "console log message" ends, especially if you want multiple lines of console output to be treated as one log event弄清楚每条“控制台日志消息”的结束位置可能会很棘手,特别是如果您希望将多行控制台 output 视为一个日志事件
  • you'll only have limited log4j configurability, because all "console log messages" will go through a single Logger (or maybe one for out and one for err )您将只有有限的 log4j 可配置性,因为所有“控制台日志消息”都将通过单个 Logger 进行 go (或者可能一个用于out一个用于err
  • performance will be a bigger and less tractable issue than with normal log4j logging.与正常的 log4j 日志记录相比,性能将是一个更大且更难处理的问题。

You should be able to call System.setOut(new SomeCustomLog4JAdapter());您应该能够调用System.setOut(new SomeCustomLog4JAdapter()); Where class SomeCustomLog4JAdapter extends PrintStream class SomeCustomLog4JAdapter extends PrintStream

Yes, it is.是的。 Use java.lang.System.setOut(java.io.PrintStream) and java.lang.System.setErr(java.io.PrintStream) to replace standard out and error streams. Use java.lang.System.setOut(java.io.PrintStream) and java.lang.System.setErr(java.io.PrintStream) to replace standard out and error streams.

Given your constraints, would it be sufficient to redirect stdout and stderr to files?鉴于您的限制,将stdoutstderr重定向到文件是否足够?

File outFile  = new File("/var/log/jiang/stdout.log");
PrintStream outStream = new PrintStream(new FileOutputStream(outFile));
System.setOut(outStream);

File errFile  = new File("/var/log/jiang/stderr.log");
PrintStream errStream = new PrintStream(new FileOutputStream(errFile));
System.setErr(errStream);

As per the other guys responses, yes it is possible with System.setOut(...) .根据其他人的回答,是的,可以使用System.setOut(...) But I'd really recommend you don't.但我真的建议你不要。 You are just creating a nightmare for a future developer and not really saving any time or money.您只是在为未来的开发人员制造一场噩梦,并没有真正节省任何时间或金钱。 Do the work now to get it done properly and it will pay off in the long run.现在就做这项工作以正确完成它,从长远来看它会得到回报。

Apart from that, routing text from System.out and System.err to log4j is (in my opinion) miss-using it because you will not be able to make use of the logging levels to seperate debug from info from errors.除此之外,从 System.out 和 System.err 到 log4j 的路由文本(在我看来)是错误的,因为您将无法利用日志记录级别将调试与错误信息分开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM