排除 Spring Boot 日志

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

Exclude Spring Boot logs

问题

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MailService {
    private static final Logger logger = LoggerFactory.getLogger(MailService.class);

    void sendMail() {
            logger.trace("已发送电子邮件!");
            logger.debug("已发送电子邮件!");
            logger.info("已发送电子邮件!");
            logger.warn("已发送电子邮件!");
    }
}
英文:

I'm trying to configure my log4j2 logger to write logs INFO to file, but there are 20+ logs from Spring Boot, which i don't need.
My pom.xml

 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
    <exclusions>
        <exclusion>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-to-slf4j</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.7</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.7</version>
</dependency>

log4j.properties

log4j.rootLogger=INFO, file

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=mylogs.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{dd-MM-yyyy HH:mm:ss} %-5p %c{1}:%L - %m%n

MailService.class

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MailService {
    private static final Logger logger = LoggerFactory.getLogger(MailService.class);

    void sendMail() {
            logger.trace("Email sent!!");
            logger.debug("Email sent!!");
            logger.info("Email sent!!");
            logger.warn("Email sent!!");
    }
}

答案1

得分: 1

尝试在你的 applcation.properties 中添加:

logging.level.org.springframework=OFF

logging.level.root=OFF

但要注意,我不确定这是否是最佳解决方案。

英文:

Try to add in your applcation.properties:

logging.level.org.springframework=OFF

logging.level.root=OFF

But watch out, I'm not sure it's the best solution.

答案2

得分: 0

你需要添加属性以禁用特定模块的日志记录。
log4j2.properties 文件应包含以下属性以隐藏 glassfish 日志。您可以根据需要进行适当的更改。

logger.glassfish.name = org.glassfish
logger.glassfish.level = off
英文:

You need to add properties to disable logs for specific module.
log4j2.properties should contain below prop to hide glassfish logs. You can make appropriate changes to match your requirement.

logger.glassfish.name = org.glassfish
logger.glassfish.level = off

huangapple
  • 本文由 发表于 2020年9月11日 21:30:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63848091.html
匿名

发表评论

匿名网友

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

确定