Java Logging - 如何将输出重定向到记录器的自定义日志文件? [英] Java Logging - how to redirect output to a custom log file for a logger?

查看:20
本文介绍了Java Logging - 如何将输出重定向到记录器的自定义日志文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 jdk 日志配置的问题.我有一个 EJB(部署到 glassfish 中),它使用 JDK 日志记录来输出消息.因此,我使用带有如下代码的命名记录器:

I have a question about the jdk logging configuration. I have a EJB (deployed into glassfish) which uses JDK Logging to output messages. Therefor I use a named logger with a code like this:

private static Logger logger = Logger.getLogger("org.imixs.workflow");
.....
       logger.fine(" some info...");
....

我知道我可以通过将以下行添加到 Glassfish 的 logging.properties 文件中来为我的记录器配置日志级别:

I know that I can configure the loglevel for my logger by adding the following line into the logging.properties File from Glassfish:

.....
org.imixs.workflow.level=FINE

但是我如何为我的记录器指定输出文件?我想将来自名为org.imixs.workflow"的记录器的所有消息放入一个单独的文件中.这可能吗?

But how can I specify the output file for my logger? I would like to put all messages from the logger named 'org.imixs.workflow' into a separate file. Is this possible?

感谢您的帮助

推荐答案

FileHandler 的稍微混乱的模式属性可以用于此

the slightly confusing pattern property of FileHandler can be used for this

handlers=java.util.logging.FileHandler
# Default global logging level. 
.level=INFO

#logging level for the foo.bar package
foo.bar.level=CONFIG 
java.util.logging.FileHandler.pattern=%h/java%u.log

模式由一个字符串组成,该字符串包含以下将在运行时替换的特殊组件:

A pattern consists of a string that includes the following special components that will be replaced at runtime:

"/" 本地路径名分隔符

"/" the local pathname separator

"%t" 系统临时目录

"%t" the system temporary directory

%h"user.home"系统属性的值

"%h" the value of the "user.home" system property

"%g" 用于区分轮转日志的代号

"%g" the generation number to distinguish rotated logs

"%u" 解决冲突的唯一编号

"%u" a unique number to resolve conflicts

%%"翻译成一个百分号%"

"%%" translates to a single percent sign "%"

如果你想记录到多个文件,那么你可以通过为多个命名记录器设置多个处理程序来实现

If you want to log to multiple files then you can do it by set up multiple handlers for multiple named loggers

#FileHandler for file1    
java.util.logging.FileHandler.pattern = logging_property_test.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter

#FileHandler for file2
logging.FileHandler2.pattern = logging_property_test2.log
logging.FileHandler2.limit = 50000
FileHandler.count = 1
logging.FileHandler2.formatter = java.util.logging.SimpleFormatter


#setting handler for logger1
logging.PropertyTestingLogger.handlers=java.util.logging.FileHandler

#setting handler for logger2
logging.PropertyTestingLogger2.handlers=logging.FileHandler2

正如你所看到的,有一个 logging.FileHandler2 是一个自定义类,除了扩展 FileHandler 之外什么都不做

as you can see the trick is that there is a logging.FileHandler2 that is a custom class and does nothing but extends the FileHandler

package logging;

import java.io.IOException;
import java.util.logging.FileHandler;

public class FileHandler2 extends FileHandler {

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

}

背景:不幸的是,Java 的创建者并不期望任何人登录多个文件.如果查看 java.util.logging.FileHandler 的源代码,您会发现模式属性是通过类名加载的:

Background : unfortunately the creators of Java were not expecting anyone to log into multiple files. If you look at the source of java.util.logging.FileHandler you will find, that the pattern property is loaded by the class name :

public class FileHandler extends StreamHandler {

   private String pattern;

   private void configure() {

        String cname = getClass().getName();
        pattern = manager.getStringProperty(cname + ".pattern", "%h/java%u.log");

这篇关于Java Logging - 如何将输出重定向到记录器的自定义日志文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆