简体   繁体   English

Log4j 不记录消息

[英]Log4j Doesn't Log Messages

So I am programming a game and I decided I should switch from System.out.println to an actual logging api and decided to use Log4j.所以我正在编写一个游戏,我决定我应该从System.out.println切换到一个实际的日志 API 并决定使用 Log4j。 I followed a tutorial to use the basic configuration but when I call logger.info("string stuff");我按照教程使用基本配置,但是当我调用logger.info("string stuff"); it doesn't actually log anything.它实际上没有记录任何内容。 I have:我有:

public static final Logger logger = Logger.getLogger("Main.class");

And

public static void main(String[] args) {
     BasicConfigurator.configure();
     logger.info("Starting Game...");
}

You should configure Log4j with either command line options or a configuration file to enable the info level logging to an appender eg system console.您应该使用命令行选项或配置文件配置 Log4j,以启用info级别日志记录到附加程序,例如系统控制台。 Assuming you are using Log4j2 check out the Configuration docs .假设您使用的是 Log4j2,请查看 配置文档

The order of processing configuration in Log4j2 is: Log4j2中处理配置的顺序是:

  1. Log4j will inspect the "log4j2.configurationFile" system property and, if set, will attempt to load the configuration using the ConfigurationFactory that matches the file extension. Log4j 将检查“log4j2.configurationFile”系统属性,如果设置,将尝试使用与文件扩展名匹配的 ConfigurationFactory 加载配置。 Note that this is not restricted to a location on the local file system and may contain a URL.请注意,这不限于本地文件系统上的某个位置,并且可能包含一个 URL。
  2. If no system property is set the properties ConfigurationFactory will look for log4j2-test.properties in the classpath.如果未设置系统属性,则属性 ConfigurationFactory 将在类路径中查找 log4j2-test.properties。
  3. If no such file is found the YAML ConfigurationFactory will look for log4j2-test.yaml or log4j2-test.yml in the classpath.如果未找到此类文件,YAML ConfigurationFactory 将在类路径中查找 log4j2-test.yaml 或 log4j2-test.yml。
  4. If no such file is found the JSON ConfigurationFactory will look for log4j2-test.json or log4j2-test.jsn in the classpath.如果未找到此类文件,JSON ConfigurationFactory 将在类路径中查找 log4j2-test.json 或 log4j2-test.jsn。
  5. If no such file is found the XML ConfigurationFactory will look for log4j2-test.xml in the classpath.如果没有找到这样的文件,XML ConfigurationFactory 将在类路径中查找 log4j2-test.xml。
  6. If a test file cannot be located the properties ConfigurationFactory will look for log4j2.properties on the classpath.如果无法找到测试文件,则属性 ConfigurationFactory 将在类路径上查找 log4j2.properties。
  7. If a properties file cannot be located the YAML ConfigurationFactory will look for log4j2.yaml or log4j2.yml on the classpath.如果无法找到属性文件,YAML ConfigurationFactory 将在类路径上查找 log4j2.yaml 或 log4j2.yml。
  8. If a YAML file cannot be located the JSON ConfigurationFactory will look for log4j2.json or log4j2.jsn on the classpath.如果找不到 YAML 文件,JSON ConfigurationFactory 将在类路径上查找 log4j2.json 或 log4j2.jsn。
  9. If a JSON file cannot be located the XML ConfigurationFactory will try to locate log4j2.xml on the classpath.如果找不到 JSON 文件,XML ConfigurationFactory 将尝试在类路径上定位 log4j2.xml。
  10. If no configuration file could be located the DefaultConfiguration will be used.如果找不到配置文件,则将使用 DefaultConfiguration。 This will cause logging output to go to the console.这将导致日志输出进入控制台。

If you haven't configured anything you are most likely on point 10 and using the default configuration.如果您尚未配置任何内容,则很可能在第 10 点并使用默认配置。 This will only log error level and higher to console while you are using info level, which is lower.当您使用较低的info级别时,这只会将error级别和更高级别记录到控制台。 As per docs:根据文档:

Log4j will provide a default configuration if it cannot locate a configuration file.如果找不到配置文件,Log4j 将提供默认配置。 The default configuration, provided in the DefaultConfiguration class, will set up: DefaultConfiguration 类中提供的默认配置将设置:

  • A ConsoleAppender attached to the root logger.附加到根记录器的 ConsoleAppender。
  • A PatternLayout set to the pattern "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" attached to the ConsoleAppender PatternLayout 设置为模式 "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" 附加到 ConsoleAppender

Note that by default Log4j assigns the root logger to Level.ERROR.请注意,默认情况下 Log4j 将根记录器分配给 Level.ERROR。

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

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