java utillogging.properties:如何记录到两个不同的文件

发布于 2024-09-18 08:54:44 字数 149 浏览 9 评论 0原文

我将logging.properties 放置在tomcat 的WEB-INF/classes 目录中,

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

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

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

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

国粹 2024-09-25 08:54:44

我终于明白了这一点。在 tomcat 中,他们扩展了 java util 日志记录(“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

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......:

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
不可一世的女人 2024-09-25 08:54:44

说到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();
   }
}

还有我的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
...

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.

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

...
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
...
看透却不说透 2024-09-25 08:54:44

没有简单的方法可以使用具有不同参数的 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:

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"));
    }
}

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:

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:

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"));
    }
}
落在眉间の轻吻 2024-09-25 08:54:44

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

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

public class JULTestingFileHandler extends FileHandler {

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

用户属性文件;

com.xxx.handlers = com.xxx.JULXXXFileHandler

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

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

Just create custom file handler; use that similar to "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
子栖 2024-09-25 08:54:44

我自己在 java.util.logging 上遇到了同样的问题,并且对给定的答案不太满意,我刚刚在 文档

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);
        ...
    }

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

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

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文