My log4j does not record any log file. I am not sure if this is a problem from the settings inside log4j.properties
or the location of this file.
The way I built the project:
git clone https://github.com/spring-guides/gs-maven.git
Then modified: .\initial\pom.xml
and added support for log4j
:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-bom</artifactId>
<version>2.17.2</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
</dependencies>
just before:
</project>
Added log commands to .\initial\src\main\java\hello\HelloWorld.java
as follows:
package hello;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class HelloWorld {
private static Logger logger = LogManager.getLogger(HelloWorld.class);
public static void main(String[] args) {
logger.error("Application status : {} ", "start");
Greeter greeter = new Greeter();
System.out.println(greeter.sayHello());
logger.error("Application status : {} ", "terminated");
}
}
Then added log4j.properties
:
appender.stdout.type = Console
# ... other appender properties
appender.file.type = File
# ... other appender properties
logger.app = INFO, stdout, file
# logger.app.name = com.example.app
# is equivalent to:
# appender.stdout.type = Console
# appender.stdout.name = stdout
# ...
appender.file.type = File
appender.file.name = mylog
# ...
# logger.app.name = com.example.app
logger.app.level = INFO
logger.app.appenderRef.$1.ref = stdout
logger.app.appenderRef.$2.ref = file
I kept changing the location of this file to
.\initial\log4j.properties
.\initial\target\log4j.properties
.\initial\target\classes\log4j.properties
.\initial\target\classes\hello\log4j.properties
None of them have ever worked at all. I do not see mylog
or any other log files.
The way I run the project is as follows:
cd initial
mvn compile
mvn package
java -jar target/gs-maven-0.1.0.jar
How should I fix this?
CodePudding user response:
There are some problems with your configuration:
- As Gopinath remarked, the configuration file should be in
src/main/resources
. Maven will copy it totarget/classes
, - Log4j 1.x uses the name
log4j.properties
, Log4j 2.x useslog4j2.properties
, - The properties configuration format is the most difficult to master. I would advise you to use any of the other format (e.g. XML, which does not require additional dependencies).
If you insist on using the properties format, here are some hints:
every configuration requires a root logger, which is the ancestor of each logger and provides the default values for the other logger configurations. You can use the shorthand notation:
rootLogger = INFO, stdout, file
introduced in version 2.17.2 or the long notation:
rootLogger.level = INFO rootLogger.appenderRef.$1.ref = stdout rootLogger.appenderRef.$2.ref = file
The names of the appender references must match the
name
properties of the appenders:appender.stdout.name = stdout appender.file.name = file
there is no shorthand for that.
The
FileAppender
needs afileName
:appender.file.fileName = mylog