Logback: How can i get the log file path?

余生长醉 提交于 2021-02-20 03:01:11

问题


I'm using Logback in my spring Boot aplication and it's working fine, but i need to get programmatically the absolute path of the file that i'm loggin/writing with the appender's name "FILE-AUDIT".

To be more clear, given the xml config file below:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
        <property name="LOG_ROOT" value="/home/sysadmin/logs" />

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>
                %d{yyyy-MM-dd HH:mm:ss} [%-5level] - %msg%n
            </Pattern>
        </encoder>
    </appender>

    <appender name="FILE-AUDIT"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_ROOT}/audit.log</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>
                %d{yyyy-MM-dd HH:mm:ss} [%-5level] - %msg%n
            </Pattern>
        </encoder>

        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- rollover daily -->
            <fileNamePattern>${LOG_ROOT}/archived/audit.%d{yyyy-MM-dd}.%i.log
                        </fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>10MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>

    </appender>

        <logger name="com.globant.corp.kit" level="info" additivity="false">
        <appender-ref ref="FILE-AUDIT" />
    </logger>

    <root level="info">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

I need to write a method that gives me in return "/home/sysadmin/logs/audit.log"


回答1:


You could share a properties file between spring and logback. Or you could access context scoped logback properties programatically. Or you could dig through your logback context looking for the appender.

logback.xml

<configuration>
    <property scope="context" name="abc" value="xyz"/>
</configuration>

java

import ch.qos.logback.classic.LoggerContext;

public static void main(String[] args) {
    LoggerContext context = (LoggerContext)LoggerFactory.getILoggerFactory();
    System.out.println(context.getProperty("abc"));
}

Alternatively to import a properties file off the classpath into logback you use:

<configuration>
    <property resource="resource1.properties" />
</configuration>

Both these solutions mean that you'd have to move /audit.log into a property. The properties file solution is likely easier to integrate with spring.

You could possibly get it directly from the appender but you need even more dodgy casts:

LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
Logger logger = context.getLogger("com.globant.corp.kit");
RollingFileAppender<ILoggingEvent> xyz = (RollingFileAppender<ILoggingEvent>) logger.iteratorForAppenders().next();
TimeBasedRollingPolicy rollingPolicy = (TimeBasedRollingPolicy) xyz.getRollingPolicy();
rollingPolicy.getFileNamePattern();


来源:https://stackoverflow.com/questions/41229521/logback-how-can-i-get-the-log-file-path

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!