java util logging.properties:如何登录到两个不同的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3639694/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:47:18  来源:igfitidea点击:

java util logging.properties: How to log to two different files

javatomcatjava.util.logging

提问by David Parks

I am placing a logging.properties in the WEB-INF/classes dir of tomcat

我在 tomcat 的 WEB-INF/classes 目录中放置了一个 logging.properties

I would like to log to two different files. For example: org.pkg1 goes to one file and org.pkg2 goes to a separate file.

我想登录到两个不同的文件。例如:org.pkg1 转到一个文件,org.pkg2 转到一个单独的文件。

I can get one file configured, but not two. Is that possible?

我可以配置一个文件,但不能配置两个。那可能吗?

回答by David Parks

I finally figured this out. In tomcat they extend java util logging ("JULI") to enable this functionality. Here's a logging.properties file that I put in the WEB-INF directory that finally accomplished what I was after......:

我终于想通了这一点。在 tomcat 中,他们扩展了 java util logging(“JULI”)以启用此功能。这是我放在 WEB-INF 目录中的 logging.properties 文件,它最终完成了我所追求的......:

handlers=1console.java.util.logging.ConsoleHandler, 2jsp.org.apache.juli.FileHandler, 3financials.org.apache.juli.FileHandler
.handlers=1a.java.util.logging.ConsoleHandler

jsp.level=ALL
jsp.handlers=2jsp.org.apache.juli.FileHandler
org.apache.jasper.level = FINE
org.apache.jasper.handlers=2jsp.org.apache.juli.FileHandler
org.apache.jsp.level = FINE
org.apache.jsp.handlers=2jsp.org.apache.juli.FileHandler

com.paypal.level=ALL
com.paypal.handlers=3financials.org.apache.juli.FileHandler

3financials.org.apache.juli.FileHandler.level=ALL
3financials.org.apache.juli.FileHandler.directory=${catalina.base}/logs
3financials.org.apache.juli.FileHandler.prefix=financials.

2jsp.org.apache.juli.FileHandler.level=ALL
2jsp.org.apache.juli.FileHandler.directory=${catalina.base}/logs
2jsp.org.apache.juli.FileHandler.prefix=jsp.

1console.java.util.logging.ConsoleHandler.level=FINE
1console.java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

回答by Teodor Simchev

Speaking of logging.properties configuration, I did not found any mechanism to use more that one appender. I made simple workaround that works for me.

说到 logging.properties 配置,我没有发现任何机制可以使用多个 appender。我做了对我有用的简单解决方法。

public class CustomAFileHandler extends FileHandler {
   public DebugFileHandler() throws IOException, SecurityException {
      super();
   }
}

public class CustomBFileHandler extends FileHandler {
   public DebugFileHandler() throws IOException, SecurityException {
      super();
   }
}

And my logging.properties

还有我的 logging.properties

...
handlers=<pkg_name>.CustomAFileHandler, <pkg_name>.CustomBFileHandler, java.util.logging.ConsoleHandler

<pkg_name>.CustomAFileHandler.level=ALL
<pkg_name>.CustomAFileHandler.pattern=%h/A%u.log
<pkg_name>.CustomAFileHandler.limit=50000
<pkg_name>.CustomAFileHandler.count=1 
<pkg_name>.CustomAFileHandler.formatter=java.util.logging.SimpleFormatter


<pkg_name>.CustomBFileHandler.level=ALL
<pkg_name>.CustomBFileHandler.pattern=%h/B%u.log
<pkg_name>.CustomBFileHandler.limit=50000
<pkg_name>.CustomBFileHandler.count=1 
<pkg_name>.CustomBFileHandler.formatter=java.util.logging.SimpleFormatter
...

回答by ahawtho

There's no easy way to get two handlers of the same type with java.util.logging classes that have different arguments. Probably the simplest way to do this is to create a FileHandler subclass in your logging.properties that passes the appropriate arguments to enable your logging to take place, such as:

没有简单的方法可以使用具有不同参数的 java.util.logging 类来获取相同类型的两个处理程序。可能最简单的方法是在你的 logging.properties 中创建一个 FileHandler 子类,它传递适当的参数来启用你的日志记录,例如:

org.pkg1.handlers=java.util.logging.FileHandler
org.pkg2.handlers=org.pkg2.FileHandler
java.util.logging.FileHandler.pattern="org_pkg1_%u.%g.log"
org.pkg2.FileHandler.pattern="org_pkg2_%u.%g.log"

org/pkg2/FileHandler.java:

org/pkg2/FileHandler.java:

package org.pkg2;

import java.util.logging.*;

public class FileHandler extends java.util.logging.FileHandler {
    public FileHandler() {
        super(LogManager.getLogManager().getProperty("org.pkg2.FileHandler.pattern"));
    }
}

回答by user1276325

It is possible using pure jdk also (try with jdk 7 or jdk 8).

也可以使用纯 jdk(尝试使用 jdk 7 或 jdk 8)。

Just create custom file handler; use that similar to "java.util.logging.FileHandler".

只需创建自定义文件处理程序;使用类似于“java.util.logging.FileHandler”的。

public class JULTestingFileHandler extends FileHandler {

    public JULTestingFileHandler() throws IOException, SecurityException 
    {
        super();    
    }
}

user properties file;

用户属性文件;

com.xxx.handlers = com.xxx.JULXXXFileHandler

com.xxx.JULXXXFileHandler.pattern   = ./logs/test1_test2.%u.%g.log

回答by remi

Having the same problem myself with java.util.loggingand not quite satisfied with the given answers, I just found in the documentation:

我自己java.util.logging也遇到了同样的问题并且对给出的答案不太满意,我刚刚在文档中发现:

2.2 Changing the Configuration

Here's a small program that dynamically adjusts the logging configuration to send output to a specific file and to get lots of information on wombats. The pattern "%t" means the system temporary directory.

2.2 更改配置

这是一个小程序,它动态调整日志配置以将输出发送到特定文件并获取有关袋熊的大量信息。模式“%t”表示系统临时目录。

public static void main(String[] args) {
        Handler fh = new FileHandler("%t/wombat.log");
        Logger.getLogger("").addHandler(fh);
        Logger.getLogger("com.wombat").setLevel(Level.FINEST);
        ...
    }

So, it seems you can't do it just from the .properties file as can't instantiate several appenders, but you can do it programmatically. Also it should be possible using the LoggerManager

因此,似乎您不能仅从 .properties 文件中执行此操作,因为无法实例化多个 appender,但您可以通过编程方式执行此操作。也应该可以使用LoggerManager