简体   繁体   English

log4J2日志未写入文件

[英]log4J2 logs are not written to the file

I want to store logs in 2 different files. 我想将日志存储在2个不同的文件中。 On the one hand, my logs are not written to the files, but the files are created and on the other hand, only "error" and "fatal" -levels are displayed in the console. 一方面,我的日志未写入文件,但是创建了文件,另一方面,控制台中仅显示“错误”和“致命”级别。 property file: 属性文件:

name=PropertiesConfig
property.filename = logs
appenders = console, user, system

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n



appender.user.type = File
appender.user.name = LOGFILE
appender.user.fileName=${filename}/user.log
appender.user.layout.type=PatternLayout
appender.user.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] c{1} - %msg%n
appender.user.MaxFileSize=5120KB
appender.user.MaxBackupIndex=10


appender.system.type = File
appender.system.name = LOGFILE
appender.system.fileName=${filename}/system.log
appender.system.layout.type=PatternLayout
appender.system.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] c{1} - %msg%n
appender.system.MaxFileSize=5120KB
appender.system.MaxBackupIndex=10




Loggers = file
logger.file.name = de.pares.int_plan
logger.file.level = All
logger.file.appenderRefs = file
logger.file.appenderRef.file.ref  = LOGFILE

This is how I create the loggers: 这是我创建记录器的方式:

private static Logger userLog = LogManager.getLogger("user");
private static Logger systemLog = LogManager.getLogger("system");

Use theem like this: 像这样使用它们:

userLog.debug("UserLogger: This is a debug message");
userLog.info("UserLogger: This is an info message");
userLog.warn("UserLogger: This is a warn message");
userLog.error("UserLogger: This is an error message");
userLog.fatal("UserLogger: This is a fatal message");

systemLog.debug("SystemLogger: This is a debug message");
systemLog.info("SystemLogger: This is an info message");
systemLog.warn("SystemLogger: This is a warn message");
systemLog.error("SystemLogger: This is an error message");
systemLog.fatal("SystemLogger: This is a fatal message");

what im doing wrong? 我在做什么错?

There are several problems with the properties you have defined. 您定义的属性存在几个问题。 I will try to explain each of them one by one. 我将尝试逐一解释。 However, you should refer back to the log4j2 manual regarding properties configuration and do your own research to find examples that use log4j2 properties or other questions regarding log4j2 properties to help you when you get stuck and include this information in your question. 但是,您应该参考有关属性配置log4j2手册,并进行自己的研究以找到使用log4j2属性或其他有关log4j2属性的问题的示例,以在遇到问题时为您提供帮助,并在问题中包含此信息。

You said that only error and fatal levels are displayed on the console. 您说控制台上仅显示错误和致命级别。 This is due to the fact that you have not defined a root logger. 这是由于您尚未定义根记录器。 As the log4j2 manual states : 如log4j2手册所述

Every configuration must have a root logger. 每个配置都必须具有一个根记录器。 If one is not configured the default root LoggerConfig, which has a level of ERROR and has a Console appender attached, will be used. 如果未配置,将使用默认根LoggerConfig,它具有ERROR级别并且已附加控制台附加程序。

Since additivity is true by default and you have not disabled it, all log events are being sent to the default root logger as mentioned above. 由于可加性默认情况下为true ,并且您尚未禁用它,因此所有日志事件都将发送到默认的根记录器,如上所述。 The result is that only those events which are error or fatal are logged to console since the default root logger's level is ERROR 结果是,由于默认的根记录程序级别为ERROR ,因此只有那些错误或致命的事件才记录到控制台

Now, regarding your properties configuration here are the problems: 现在,关于您的属性配置,这里有问题:

  1. Your file appenders have invalid attributes "MaxBackupIndex", "MaxFileSize". 您的文件附加程序具有无效的属性“ MaxBackupIndex”,“ MaxFileSize”。 The following two lines are not applicable to a file appender: 以下两行不适用于文件追加器:

    appender.user.MaxFileSize=5120KB

    appender.user.MaxBackupIndex=10

    Perhaps you wanted a rolling file appender instead? 也许您想要一个滚动文件附加程序? It's not really clear from your question so I commented these out by adding '#' to the front of each line. 您的问题并不清楚,因此我在每行的开头添加了“#”以将其注释掉。

  2. If you're using log4j2 version 2.6 or greater then you don't need these lines: 如果您使用的是Log4j2 2.6版或更高版本,则不需要以下行:

    appenders = console, user, system

    Loggers = file

    You didn't specify which version you're using so I assumed you're using the latest in which case these lines are not needed. 您没有指定要使用的版本,所以我假设您使用的是最新版本,在这种情况下不需要这些行。 I commented these lines as well. 我也评论了这些内容。

  3. Both of your appenders use the same name of "LOGFILE". 您的两个追加程序都使用相同的名称“ LOGFILE”。 I changed the user appender name to "userLog" and the system appender name to "systemLog". 我将用户附加程序名称更改为“ userLog”,将系统附加程序名称更改为“ systemLog”。 (see full properties later in the answer) (请参阅稍后的完整属性)

  4. You only defined one logger and its name does not match the name you're using to retrieve it. 您仅定义了一个记录器,其名称与您用来检索它的名称不匹配。 This is why all of your log events go to the default root logger. 这就是为什么所有日志事件都进入默认的根记录器的原因。 You have logger.file.name = de.pares.int_plan but de.pares.int_plan is not one of the names you used in your java code to obtain the logger. 您具有logger.file.name = de.pares.int_plan但是de.pares.int_plan不是您在Java代码中用于获取记录器的名称之一。 You used user and system . 您使用了usersystem I corrected these in the properties (see full properties later in the answer). 我在属性中更正了这些(请参阅答案后面的完整属性)。

  5. You're missing the '%' before c{1} in the pattern of both file appenders. 在两个文件追加器的模式中,您都缺少c{1}之前的'%'。

  6. You have an extra line in your logger definition logger.file.appenderRefs = file that does not appear to be necessary based on the example in the log4j2 manual . 根据log4j2手册中的示例,在记录器定义logger.file.appenderRefs = file ,您似乎没有多余的一行。

Putting all of these corrections together I arrived at the following properties: 综合所有这些更正,我得出以下属性:

name=PropertiesConfig
property.filename = logs
#Removed the following line since latest log4j2 does not need it
#appenders = console, user, system

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

appender.user.type = File
appender.user.name = userLog
appender.user.fileName=${filename}/user.log
appender.user.layout.type=PatternLayout
appender.user.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
#Removed attributes that do not apply to file appender
#appender.user.MaxFileSize=5120KB
#appender.user.MaxBackupIndex=10

appender.system.type = File
appender.system.name = systemLog
appender.system.fileName=${filename}/system.log
appender.system.layout.type=PatternLayout
appender.system.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
#Removed attributes that do not apply to file appender
#appender.system.MaxFileSize=5120KB
#appender.system.MaxBackupIndex=10

#Removed the following line since latest log4j2 does not need it
#Loggers = file
logger.userFile.name = user
logger.userFile.level = All
logger.userFile.appenderRef.user.ref  = userLog

logger.systemFile.name = system
logger.systemFile.level = All
logger.systemFile.appenderRef.system.ref = systemLog

#Adding the following commented lines for reference - this is how you define the root logger in the properties.
#rootLogger.level = info
#rootLogger.appenderRef.console.ref = STDOUT

To test this configuration I used the following code: 为了测试此配置,我使用了以下代码:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class SomeClass {

    private static Logger userLog = LogManager.getLogger("user");
    private static Logger systemLog = LogManager.getLogger("system");

    public static void main(String[] args){

        systemLog.debug("This is the top of main method");
        if(userLog.isDebugEnabled())
            userLog.debug("This is some debug!");
        userLog.info("Here's some info!");
        userLog.error("Some error happened!");
        systemLog.info("This is the end of main method");
        systemLog.error("woops an error occurred");
    }
}

The result is two log files: 结果是两个日志文件:

system.log SYSTEM.LOG

[DEBUG] 2019-02-16 18:04:04.980 [main] system - This is the top of main method
[INFO ] 2019-02-16 18:04:04.980 [main] system - This is the end of main method
[ERROR] 2019-02-16 18:04:04.980 [main] system - woops an error occurred

user.log USER.LOG

[DEBUG] 2019-02-16 18:04:04.980 [main] user - This is some debug!
[INFO ] 2019-02-16 18:04:04.980 [main] user - Here's some info!
[ERROR] 2019-02-16 18:04:04.980 [main] user - Some error happened!

and console output: 和控制台输出:

[DEBUG] 2019-02-16 18:04:04.980 [main] system - This is the top of main method
[DEBUG] 2019-02-16 18:04:04.980 [main] user - This is some debug!
[INFO ] 2019-02-16 18:04:04.980 [main] user - Here's some info!
[ERROR] 2019-02-16 18:04:04.980 [main] user - Some error happened!
[INFO ] 2019-02-16 18:04:04.980 [main] system - This is the end of main method
[ERROR] 2019-02-16 18:04:04.980 [main] system - woops an error occurred

To fully understand the console output please refer to the log4j2 manual - the section titled Additivity . 要充分认识控制台输出请参考log4j2说明书-标题为加和

Hope this helps! 希望这可以帮助!

Couple of things is see with just a cursory pass at this. 随便一眼就可以看到几件事情。 I will intersperse comments in-line using # comments 我将使用#注释插入注释

appender.user.type = File
appender.user.name = LOGFILE
#                    ^^^^^^^ This must be distinct - it conflicts with appender.system
appender.user.fileName=${filename}/user.log
appender.user.layout.type=PatternLayout
appender.user.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] c{1} - %msg%n
#                                                         missing % here ^^^^
appender.user.MaxFileSize=5120KB
appender.user.MaxBackupIndex=10

appender.system.type = File
appender.system.name = LOGFILE
#                      ^^^^^^^ This must be distinct - it conflicts with appender.user
appender.system.fileName=${filename}/system.log
appender.system.layout.type=PatternLayout
appender.system.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] c{1} - %msg%n
#                                                           missing % here ^^^^
appender.system.MaxFileSize=5120KB
appender.system.MaxBackupIndex=10

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

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