英文:
Why my Log File is Not Been Created In The Local Explorer?
问题
我只想记录运行时出现的异常。我有一个 Application_Logger.class、Application.properties、log4j2.properties 文件,当然还有主类。有人可以帮我创建一个日志文件吗?
App_Logger.Class
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class ApplicationLogger {
static Logger cat = LogManager.getLogger(ApplicationLogger.class);
public static final boolean TRACE_EMPTY_EXCEPTION_CALL = true;
/**
* 记录消息
*/
public static void log(String message){
cat.debug(message);
}
public static void log(String message, UserDTO loginUser) {
cat.debug(getLoginUserDetails(loginUser) + message);
}
/**
* 记录消息和异常
*/
public static void log(String message, Exception e){
log(message, e, null);
}
public static void log(String message, Exception e, UserDTO loginUser) {
if(e != null)
cat.error(getLoginUserDetails(loginUser) + message, e);
else {
if(TRACE_EMPTY_EXCEPTION_CALL) {
try {
throw new Exception("遇到空异常");
} catch(Exception ex) {
log(message, ex, loginUser);
}
} else
cat.debug(getLoginUserDetails(loginUser) + message);
}
}
/**
* 记录消息作为错误
*
* @param message
*/
public static void logError(String message){
logError(message, null);
}
public static void logError(String message, UserDTO loginUser){
cat.error(getLoginUserDetails(loginUser) + message);
}
public static void log(String empNo, String operation, String operand){
cat.debug(empNo + ":" + operation + ":" + operand);
}
public static String getLoginUserDetails(UserDTO loginUser) {
if(loginUser == null)
return "";
return "[" + loginUser.getEmpId() + ", " + loginUser.getTenantId() + "] ";
}
}
Application.properties File
log4j2.debug=true
InitApplication.class
@SpringBootApplication
@CrossOrigin("http://localhost:3000")
public class InitApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(InitApplication.class, args);
}
}
英文:
I only want to log my Exceptions which I get at the time of Run Time. I have An Application_Logger.class, Application.properties,log4j2. properties file. and of course the main class. Can anyone help me creating a log file?
App_Logger.Class
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class ApplicationLogger {
transient static Logger cat = (Logger) LogManager.getLogger(ApplicationLogger.class);
public static final boolean TRACE_EMPTY_EXCEPTION_CALL = true;
/**
* Logs the message
*/
public static void log(String message){
cat.debug(message);
}
public static void log(String message, UserDTO loginUser) {
cat.debug(getLoginUserDetails(loginUser) + message);
}
/**
* Logs the message and the exception
*/
public static void log(String message, Exception e){
log(message, e, null);
}
public static void log(String message, Exception e, UserDTO loginUser) {
if(e != null)
cat.error(getLoginUserDetails(loginUser) + message, e);
else {
if(TRACE_EMPTY_EXCEPTION_CALL) {
try {
throw new Exception("Empty exception encountered");
}catch(Exception ex) {
log(message, ex, loginUser);
}
}
else
cat.debug(getLoginUserDetails(loginUser) + message);
}
}
/** logs the message as an error
*
* @param message
*/
public static void logError(String message){
logError(message, null);
}
public static void logError(String message, UserDTO loginUser){
cat.error(getLoginUserDetails(loginUser) + message);
}
public static void log(String empNo, String operation, String operand){
cat.debug(empNo + ":" + operation + ":" + operand);
}
public static String getLoginUserDetails(UserDTO loginUser) {
if(loginUser == null)
return "";
return "[" + loginUser.getEmpId() + ", " + loginUser.getTenantId() + "] ";
}
}
Application.properties File
log4j2.debug=true
InitApplication.class
@SpringBootApplication
@CrossOrigin("http://localhost:3000")
public class InitApplication implements CommandLineRunner{
public static void main(String[] args) {
SpringApplication.run(InitApplication.class, args);
}
}
答案1
得分: 0
这只是一个在 pom.xml 文件中的错误。
之前是这样的,
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xyz</groupId>
<artifactId>ABC</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ABC</name>
<description>LOG4j2</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180813</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
然后我添加了这些行,基本上是排除了 Spring Boot 默认的启动器日志记录器,并通过添加其依赖项添加了 log4j2 记录器。像这样,
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
英文:
It was just an error in pom.xml file.
Earlier it was like,
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xyz</groupId>
<artifactId>ABC</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ABC</name>
<description>LOG4j2</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180813</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Then I added these lines, basically, I excluded Spring boot default starter logger and added log4j2 logger by adding its dependencies.
Like This.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论