Log4j config - 不同的日志到不同的文件 [英] Log4j config - different logs to different files

查看:74
本文介绍了Log4j config - 不同的日志到不同的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一些人来说,这可能是一个非常简单的问题,但我个人认为Log4j的配置非常困难,并且学习进行脑部手术可能不那么具有挑战性。

This might be a very easy question for some, but personally I find Log4j config to be nightmarishly difficult and that learning to perform brain surgery might be less challenging.

我正在尝试将多个记录器记录到不同的文件中。
以下是我在log4j.properties文件中的内容:

I am trying to lave multiple loggers logging into different files. Here is what I have in my log4j.properties file:

# Root logger option
log4j.rootLogger=INFO, file, admin

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/home/nick/logging/file.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1} - %m%n

log4j.appender.admin=org.apache.log4j.RollingFileAppender
log4j.appender.admin.File=/home/nick/logging/admin.log
log4j.appender.admin.MaxFileSize=1MB
log4j.appender.admin.MaxBackupIndex=1
log4j.appender.admin.layout=org.apache.log4j.PatternLayout
log4j.appender.admin.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1} - %m%n

这是我的(非常简单的)Java应用程序est配置:

And here is my (very simple) Java app used to test the config:

public static void main(String[] args) throws Exception {

    Properties resource = new Properties();
    InputStream in = new FileInputStream("/home/nick/logging/log4j.properties");
    resource.load(in);
    PropertyConfigurator.configure(resource);

    Logger admin = Logger.getLogger("admin");
    Logger file = Logger.getLogger("file");

    admin.info("hello admin");
    file.info("hello file");
}

我有2个问题:

一个问题我总是在行 PropertyConfigurator.configure(资源)中得到异常;

java.io.FileNotFoundException: /home/nick/logging (Is a directory)
 at java.io.FileOutputStream.open(Native Method)
 at java.io.FileOutputStream.<init>(FileOutputStream.java:212)
 at java.io.FileOutputStream.<init>(FileOutputStream.java:136)
 at org.apache.log4j.FileAppender.setFile(FileAppender.java:289)
 at org.apache.log4j.RollingFileAppender.setFile(RollingFileAppender.java:167)
 at org.apache.log4j.FileAppender.activateOptions(FileAppender.java:163)
 at org.apache.log4j.config.PropertySetter.activate(PropertySetter.java:256)

第二个问题是两条消息都写入两个日志。以下是实际结果:

The 2nd problem is that both messages are written to both logs. Here is the actual result:

文件管理员:日志:

2014-04-27 11:55:30 INFO  admin - hello admin
2014-04-27 11:55:30 INFO  file - hello file

文件file.log:

File file.log:

2014-04-27 11:55:30 INFO  admin - hello admin
2014-04-27 11:55:30 INFO  file - hello file

以下是必需结果:

文件管理员:日志:

2014-04-27 11:55:30 INFO  admin - hello admin

文件file.log:

File file.log:

2014-04-27 11:55:30 INFO  file - hello file

导致异常的原因是什么,以及如何达到要求的结果?

What is causing the exception, and how can I achieve the required result?

推荐答案

Log4J区分 loggers ,它们负责生成日志消息, appenders ,负责发送t软管消息某处(文件,控制台,数据库等)。记录器形成层次结构,根记录器是名为 admin 的记录器的父记录,它是 admin.component1 等等,您可以将appender附加到层次结构中的任何记录器。默认情况下,记录器会将消息发送到直接连接到它的所有appender,或者发送到层次结构中的任何祖先(这就是为什么logger通常被命名为Java类,例如你可以控制的日志记录com.example.Class1 com.example.subpkg.AnotherClass 通过配置 com.example logger)。

Log4J makes a distinction between loggers, which are responsible for generating log messages, and appenders, which are responsible for sending those messages somewhere (a file, the console, a database, etc.). Loggers form a hierarchy, the root logger is the parent of the logger named admin, which is the parent of admin.component1, etc., and you can attach appenders to any logger in the hierarchy. By default a logger will send messages to all appenders that are attached directly to it, or to any of its ancestors in the hierarchy (this is why loggers are conventionally named like Java classes, e.g. you can control logging for com.example.Class1 and com.example.subpkg.AnotherClass by configuring the com.example logger).

记录器和追加器形成不同的命名空间,这是你混淆的根源 - 记录器名为 admin 和名为 admin 的appender是两个独立的实体。

Loggers and appenders form separate namespaces and this is the source of your confusion - the logger named admin and the appender named admin are two separate entities.

您在问题中给出的配置定义了一个logger(根记录器),它将生成的所有消息发送到两个单独的 appenders ,每个文件对应一个。然后,您的代码会请求两个不同的记录器,并为每个记录器生成一条日志消息。这两个记录器都从根记录器继承了appender配置,因此它们都将消息发送到配置的appender的两个

The configuration you have given in the question defines one logger (the root logger) which sends all the messages it generates to two separate appenders, one for each of the two files. Your code then requests two different loggers and generates one log message with each logger. Both these loggers inherit the appender configuration from the root logger, so they both send their messages to both of the configured appenders.

而不是将两个appender附加到root logger,你应该将文件 appender附加到文件 logger和 admin appender到 admin 记录器:

Instead of attaching the two appenders to the root logger, you should attach the file appender to the file logger and the admin appender to the admin logger:

log4j.rootLogger=INFO
log4j.logger.file=INFO, file
log4j.logger.admin=INFO, admin

这样文件记录器只会向 file.log 发送消息, admin 仅记录到 admin.log ,来自其他记录器的所有消息将被静默丢弃,因为没有附加到根的appender。

This way the file logger will send messages only to file.log, the admin logger only to admin.log, and all messages from other loggers will be silently discarded, as there are no appenders attached to the root.

additivity 标志是此规则的例外 - 设置记录器的对false的可加性实际上将箭头从记录器断开连接到其父级,因此该记录器生成的消息(或从其中一个子级流入它的消息)将不再向上移动到树上,它们只会转到附加的appender 直接到相关记录器。

The additivity flag is the exception to this rule - setting a logger's additivity to false essentially disconnects the arrow from a logger up to its parent, so messages generated by that logger (or flowing into it from one of its children) will not go any further up the tree, they will only go to appenders attached directly to the logger in question.

这篇关于Log4j config - 不同的日志到不同的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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