java—是否可以从db添加logback.xml配置值?

vlurs2pr  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(363)

我想从数据库中动态配置logback.xml值。这是我的logback.xml文件:

<configuration>
    <appender name="KIWI" class="ch.qos.logback.classic.net.SyslogAppender">
        <syslogHost>localhost:8080</syslogHost>
        <facility>LOCAL0</facility>
        <suffixPattern>%thread: %-5level %logger{36} - %msg%n</suffixPattern>
    </appender>

    <logger name="com.javacodegeeks.examples.logbacksyslogexample.message.kiwi" level="INFO">
        <appender-ref ref="KIWI" />
    </logger>
</configuration>

我想从数据库中获取syslog主机名信息,而不是直接在这里硬编码。有可能吗?
先谢谢你。

holgip5t

holgip5t1#

正如我在评论中提到的,我认为在数据库中存储日志配置不是一个好主意。如果我理解正确,您关心的是如何根据某些变量更改appender配置。这可以通过使用mdc过滤器或从服务属性读取值来解决。
如果您想从application.yml或env vars中读取一些值,可以简单地使用spring表达式。
但是,如果您想要更大的灵活性和控制能力,可以使用mdc过滤器,在这里您可以设置任何您想要的内容并将其传递给logback配置。
http://logback.qos.ch/apidocs/ch/qos/logback/classic/turbo/mdcfilter.html
在下面的链接中,您可以看到如何Map和基本使用mdc进行logback:https://www.baeldung.com/mdc-in-log4j-2-logback
下面是我如何使用mdc过滤器的示例。基本上,根据请求到达时登录的组织,我解析了请求并将其设置在mdc中,然后在写入日志时可以通过logback读取它。
java配置:

@Component
@Order
public class MDCFilter implements Filter {

    @Override
    public void init(FilterConfig fc) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest sr, ServletResponse sr1, FilterChain fc) throws IOException, ServletException {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = "System admin";
        String organization = "System-Club";
        if (authentication != null) {
            if (authentication.getPrincipal() instanceof UserDetails) {
                UserDetails springSecurityUser = (UserDetails) authentication.getPrincipal();
                username = springSecurityUser.getUsername();
                String apd = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
                orgnization = apd.getOrganization().getName();
            } 
        }
        if(orgnization != null) {
            MDC.put("orgnization", orgnization.replaceAll("\\s+","_").toLowerCase());
        }
        try {
            fc.doFilter(sr, sr1);
        } finally {
            if (username != null) {
                MDC.remove("username");
            }
            if (orgnization != null) {
                MDC.remove("orgnization");
            }
        }

    }

    @Override
    public void destroy() {
    }

}

logback.xml:

<appender name="FILE" class="ch.qos.logback.classic.sift.SiftingAppender">
        <discriminator>
            <key>organization</key>
            <defaultValue>System</defaultValue>
        </discriminator>
        <sift>
            <!-- A standard RollingFileAppender, the log file is based on 'logFileName' at runtime  -->
            <appender name="FILE-${organization}"
                      class="ch.qos.logback.core.rolling.RollingFileAppender">
                <file>/home/demo/logs/${organization}/org.log</file>
                <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                    <pattern>%-5level %date{dd.MM.yyyy. HH:mm:ss} - Organization: %mdc{organization} - %logger{35} [%L]  - %msg%n</pattern>
                </encoder>
                <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                    <fileNamePattern>/home/demo/logs/${organization}/org.%d{dd.MM.yyyy}.%i.log</fileNamePattern>
                    <maxHistory>30</maxHistory>
                    <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                        <maxFileSize>10MB</maxFileSize>
                    </timeBasedFileNamingAndTriggeringPolicy>
                </rollingPolicy>
            </appender>
        </sift>
    </appender>

相关问题