Reputation: 39
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. I followed a tutorial to use the basic configuration but when I call 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...");
}
Upvotes: 1
Views: 195
Reputation: 44980
You should configure Log4j with either command line options or a configuration file to enable the info
level logging to an appender e.g. system console. Assuming you are using Log4j2 check out the Configuration docs.
The order of processing configuration in Log4j2 is:
- 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. Note that this is not restricted to a location on the local file system and may contain a URL.
- If no system property is set the properties ConfigurationFactory will look for log4j2-test.properties in the classpath.
- If no such file is found the YAML ConfigurationFactory will look for log4j2-test.yaml or log4j2-test.yml in the classpath.
- If no such file is found the JSON ConfigurationFactory will look for log4j2-test.json or log4j2-test.jsn in the classpath.
- If no such file is found the XML ConfigurationFactory will look for log4j2-test.xml in the classpath.
- If a test file cannot be located the properties ConfigurationFactory will look for log4j2.properties on the classpath.
- If a properties file cannot be located the YAML ConfigurationFactory will look for log4j2.yaml or log4j2.yml on the classpath.
- If a YAML file cannot be located the JSON ConfigurationFactory will look for log4j2.json or log4j2.jsn on the classpath.
- If a JSON file cannot be located the XML ConfigurationFactory will try to locate log4j2.xml on the classpath.
- If no configuration file could be located the DefaultConfiguration will be used. 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. This will only log error
level and higher to console while you are using info
level, which is lower. As per docs:
Log4j will provide a default configuration if it cannot locate a configuration file. The default configuration, provided in the DefaultConfiguration class, will set up:
- A ConsoleAppender attached to the root logger.
- A PatternLayout set to the pattern "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" attached to the ConsoleAppender
Note that by default Log4j assigns the root logger to Level.ERROR.
Upvotes: 2