为什么我的日志文件在本地资源管理器中没有被创建?

huangapple go评论93阅读模式
英文:

Why my Log File is Not Been Created In The Local Explorer?

问题

我只想记录运行时出现的异常我有一个 Application_Logger.classApplication.propertieslog4j2.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,

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; 
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 
https://maven.apache.org/xsd/maven-4.0.0.xsd">
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;parent&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
&lt;version&gt;2.3.2.RELEASE&lt;/version&gt;
&lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
&lt;/parent&gt;
&lt;groupId&gt;com.xyz&lt;/groupId&gt;
&lt;artifactId&gt;ABC&lt;/artifactId&gt;
&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
&lt;name&gt;ABC&lt;/name&gt;
&lt;description&gt;LOG4j2&lt;/description&gt;
&lt;properties&gt;
&lt;java.version&gt;1.8&lt;/java.version&gt;
&lt;/properties&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-security&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-data-mongodb&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;io.jsonwebtoken&lt;/groupId&gt;
&lt;artifactId&gt;jjwt&lt;/artifactId&gt;
&lt;version&gt;0.9.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.json&lt;/groupId&gt;
&lt;artifactId&gt;json&lt;/artifactId&gt;
&lt;version&gt;20180813&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;exclusions&gt;
&lt;exclusion&gt;
&lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
&lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
&lt;/exclusion&gt;
&lt;/exclusions&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.poi&lt;/groupId&gt;
&lt;artifactId&gt;poi-ooxml&lt;/artifactId&gt;
&lt;version&gt;4.1.2&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;io.springfox&lt;/groupId&gt;
&lt;artifactId&gt;springfox-swagger2&lt;/artifactId&gt;
&lt;version&gt;2.9.2&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;io.springfox&lt;/groupId&gt;
&lt;artifactId&gt;springfox-swagger-ui&lt;/artifactId&gt;
&lt;version&gt;2.9.2&lt;/version&gt;
&lt;/dependency&gt;	
&lt;/dependencies&gt;
&lt;build&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
&lt;/project&gt;

Then I added these lines, basically, I excluded Spring boot default starter logger and added log4j2 logger by adding its dependencies.
Like This.

&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;exclusions&gt;
&lt;exclusion&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-logging&lt;/artifactId&gt;
&lt;/exclusion&gt;
&lt;/exclusions&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-log4j2&lt;/artifactId&gt;
&lt;/dependency&gt;  

huangapple
  • 本文由 发表于 2020年10月13日 15:59:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/64331023.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定