简体   繁体   English

如何使用自定义记录器在spring boot中记录访问日志

[英]How to use custom logger to log access log in spring boot

Currently in spring boot 1.3, we could only log access log to a file in the filesystem. 目前在spring boot 1.3中,我们只能将访问日志记录到文件系统中的文件中。 Is there any way to actually use the custom logger (like log4j2) to log the access log? 有没有办法实际使用自定义记录器(如log4j2)来记录访问日志?

I am currently using undertow with spring boot, but after checking the spring boot source code, the undertow logger is initialized with DefaultAccessLogReceiver which is writing to file. 我目前正在使用带有spring boot的下载,但是在检查了spring boot源代码之后,使用DefaultAccessLogReceiver初始化了underow logger,它正在写入文件。 I would like to use the AccessLogHandler if possible, and avoid writing a web filter which logs the access. 我想尽可能使用AccessLogHandler,并避免编写记录访问权限的Web过滤器。

Is there any easy way around this? 这有什么简单的方法吗? (except writing a pull request) (写拉取请求除外)

A trick for this kind of hard-coded-thus-not-customizeable problem is to hide the class to kick out with a new one with the same package and name. 这种硬编码因此不可定制的问题的一个技巧是隐藏类以使用具有相同包和名称的新类。 All you have to do is to provide a log4j based DefaultAccessLogReceiver and make sure it can be search by the classloader before the one in the undertow library. 您所要做的就是提供一个基于log4j的DefaultAccessLogReceiver ,并确保它可以在转发器库中的类加载器之前进行搜索。

package io.undertow.server.handlers.accesslog;

public class DefaultAccessLogReceiver implements AccessLogReceiver {

    public void logMessage(final String message) {
        // TODO: log with log4j
    }
}

Spring Boot has no mandatory logging dependency, except for the commons-logging API, of which there are many implementations to choose from. 除了commons-logging API之外,Spring Boot没有强制的日志记录依赖性,其中有许多实现可供选择。 To use Logback you need to include it, and some bindings for commons-logging on the classpath. 要使用Logback,您需要包含它,以及在类路径上记录commons的一些绑定。 The simplest way to do that is through the starter poms which all depend on spring-boot-starter-logging. 最简单的方法是通过启动器poms,它们都依赖于spring-boot-starter-logging。 For a web application you only need spring-boot-starter-web since it depends transitively on the logging starter. 对于Web应用程序,您只需要spring-boot-starter-web,因为它依赖于日志记录启动器。 For example, using Maven: 例如,使用Maven:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Spring Boot has a LoggingSystem abstraction that attempts to configure logging based on the content of the classpath. Spring Boot有一个LoggingSystem抽象,它试图根据类路径的内容配置日志记录。 If Logback is available it is the first choice. 如果Logback可用,则它是第一选择。

Spring Boot also supports either Log4j or Log4j 2 for logging configuration, but only if one of them is on the classpath. Spring Boot还支持Log4j或Log4j 2进行日志记录配置,但前提是其中一个在类路径中。 If you are using the starter poms for assembling dependencies that means you have to exclude Logback and then include your chosen version of Log4j instead. 如果您使用starter poms来组合依赖项,则意味着您必须排除Logback,然后包含您选择的Log4j版本。 If you aren't using the starter poms then you need to provide commons-logging (at least) in addition to your chosen version of Log4j. 如果您没有使用启动器poms,那么除了选择的Log4j版本之外,还需要提供commons-logging(至少)。

The simplest path is probably through the starter poms, even though it requires some jiggling with excludes, .eg in Maven: 最简单的路径可能是通过启动器poms,即使它需要在Maven中使用排除,.eg进行一些抖动:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</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-log4j</artifactId>
</dependency>

To use Log4j 2, simply depend on spring-boot-starter-log4j2 rather than spring-boot-starter-log4j. 要使用Log4j 2,只需依赖spring-boot-starter-log4j2而不是spring-boot-starter-log4j。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM