简体   繁体   English

如何使用log4j捕获第三方控制台日志

[英]How to catch third party console log using log4j

I'm using log4j for logging actions in my application. 我正在使用log4j在应用程序中记录操作。 I use following file appender: 我使用以下文件附加程序:

 private FileAppender getFileAppender(String name) {
  PatternLayout layout = new PatternLayout("%d{ABSOLUTE} %5p %t %c{1}:%M:%L - %m%n");
  StringBuilder filePath = new StringBuilder();
  filePath.append("logs/")
        .append(name)
        .append(".log");
  FileAppender fileAppender = null;

  try {
     File file = new File(filePath.toString());
     file.getParentFile().mkdirs();
     file.createNewFile();

     fileAppender = new FileAppender(layout, filePath.toString(), true);
     fileAppender.setName("FileAppender");
     fileAppender.setAppend(true);
  } catch (IOException e) {
     LogWrapper.LOG.get(this.getClass().getSimpleName()).trace(e);
  }

  return fileAppender;
  }

My application uses third party, what logs web services calls into console. 我的应用程序使用第三方(将Web服务调用记录到控制台中)。 Then I run application I see those logs in my console in Intellij Idea, but not on the generated log file. 然后,我运行应用程序,在Intellij Idea中的控制台中看到了这些日志,但是在生成的日志文件中却没有。

How to redirect those logs into the file, generated by File Appender? 如何将这些日志重定向到File Appender生成的文件中?

Update The answer is correct, but there is one more third party what use logback with Console Appender, properties looks like: 更新答案是正确的,但是还有另一个第三方使用Console Appender使用logback,其属性如下所示:

<configuration status="OFF">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n
        </pattern>
    </encoder>
</appender>
<root level="WARN">
    <appender-ref ref="STDOUT" />
</root>

Because of this catching console output stacks. 因此,捕获了控制台输出堆栈。 How could I catch and re-write Logback properties and make it log to file or disable logs at all? 如何捕获和重写Logback属性,使其记录到文件或完全禁用日志?

You can try this to log STDOUT as INFO and STDERR as WARNING for example: 您可以尝试将STDOUT作为INFO登录,将STDERR作为WARNING登录,例如:

final Logger myLog4jLogger = Logger.getLogger(getClass());

System.setOut(new PrintStream(new ByteArrayOutputStream() {
    @Override
    public synchronized void write(byte[] pB, int pOff, int pLen) {
        super.write(pB, pOff, pLen);
        myLog4jLogger.info(toString());
        reset();
    }
}));

System.setErr(new PrintStream(new ByteArrayOutputStream() {
    @Override
    public synchronized void write(byte[] pB, int pOff, int pLen) {
        super.write(pB, pOff, pLen);
        myLog4jLogger.warn(toString());
        reset();
    }
}));

Do this only once after Log4j initialization and before calling your third party tool. 在Log4j初始化之后和调用第三方工具之前,仅执行一次此操作。 Be careful, it means that the STDOUT and STDERR of your process will be redirected to your logs, so all the text sent to STDOUT/STDERR by your process will go to logs, including your own "System.out.println()" . 注意,这意味着您的进程的STDOUT和STDERR将被重定向到您的日志,因此您的进程发送到STDOUT / STDERR的所有文本都将进入日志,包括您自己的"System.out.println()"

Also, using this, you may want to avoid logging to the console, because I'm not sure what will happen if you log to the console and that the console is redirected to the logs... (endless loop? stack overflow? OutOfMemoryError?) 另外,使用此方法,您可能希望避免登录到控制台,因为我不确定登录到控制台并将控制台重定向到日志时会发生什么……(无限循环?堆栈溢出?OutOfMemoryError ?)

Concerning the third party that uses Logback, you can send its logs to a file instead of STDOUT by adding this to its config file: 关于使用Logback的第三方,可以将其日志发送到文件而不是STDOUT,方法是将其添加到其配置文件中:

<appender name="FILE" class="ch.qos.logback.core.FileAppender">
   <file>myApp.log</file>

   <encoder>
     <pattern>%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</pattern>
   </encoder>
</appender>

And by replacing: 并通过替换:

<appender-ref ref="STDOUT" />

...with: ...具有:

<appender-ref ref="FILE" />

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

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