Logback recording SpringBoot class member variable

13 Views Asked by At

I am trying to log a member variable from a class in my SpringBoot app and I am playing with an approach that is probably very wrong. I have the following class...

package app.core.configuration;

@Configuration
@PropertySource("classpath:git.properties")
public class MyClass
{
    @Autowired public Environment env;

    // **this is what I would like to expose to logback**
    public String issue;

    @PostConstruct
    public void init()
    {
        this.issue = StringUtils.split(getBranch(), "-")[0];
    }

    void getBranch() { return env.getProperty("git.branch"); }
}

And I am attempting to utilize it in my logback-spring.xml file as follows.

<springProperty scope="context" name="issue" source="app.core.configuration.MyClass.issue"/>


<appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashAccessTcpSocketAppender">
    <destination>localhost:5000</destination>
    <encoder class="net.logstash.logback.encoder.LogstashEncoder">
        <customFields>{ "app.issue":"${issue}" }</customFields>
    </encoder>
</appender>

But of course this does not work. How can I get a member variable of a class to be a custom field? Or is there an alternative approach?

0

There are 0 best solutions below