【问题标题】:Why my Log File is Not Been Created In The Local Explorer?为什么我的日志文件没有在本地资源管理器中创建?
【发布时间】:2020-10-13 07:59:05
【问题描述】:

我只想记录我在运行时得到的异常。我有一个 Application_Logger.class、Application.properties、log4j2。属性文件。当然还有主要课程。谁能帮我创建一个日志文件?

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 文件

    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);
    }
    }

【问题讨论】:

    标签: java spring-boot maven logging log4j2


    【解决方案1】:

    这只是 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>  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-18
      • 2011-02-06
      • 2012-06-12
      • 2013-01-10
      • 1970-01-01
      • 2017-05-31
      • 2015-10-16
      相关资源
      最近更新 更多