0

I'm trying to take a value from spring profile set in web.xml. I've already encountered a question that solves my problem but the implementation of the solution to the problem does not seem to be working. That's are my source code:

web.xml:

<context-param>
    <param-name>spring.profiles.default</param-name>
    <param-value>local-jboss</param-value>
</context-param>

logback.xml:

<insertFromJNDI env-entry-name="java:comp/env/spring.profiles.default" as="spring.profiles.default" />

<appender name="STASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
    <destination>x.x.x.x:yyyy</destination>
    <encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
        <providers>
            <mdc /> <!-- MDC variables on the Thread will be written as JSON fields -->
            <context /> <!-- Outputs entries from logback's context -->
            <version /> <!-- Logstash json format version, the @version field in the output -->
            <logLevel />
            <loggerName />
            <pattern>
                <pattern>
                {
                    "APP": "MyApp",
                    "PROFILE": "${spring.profiles.default}"
                }
                </pattern>
            </pattern>
            <threadName />
            <message />
            <logstashMarkers /> <!-- Useful so we can add extra information for specific log lines as Markers -->
            <arguments /> <!-- or through StructuredArguments -->
            <stackTrace />
        </providers>
    </encoder>
</appender>

Does anyone know how I get the value from my web.xml and put in logback.xml?

Isdeniel
  • 196
  • 10

1 Answers1

0

1st Solution :

If you are on spring 3.1+ then those context params will available as env variable and in logback you can easily refer them like below :

<springProperty scope="context" name="fluentHost" source="myapp.fluentd.host"
        defaultValue="localhost"/>

source is the name of your key. Further Documentation.

2nd Solution : You can use this as well where we will define context-param as spring property then refer the same in logback as 1st solution above.

@SpringBootApplication
class DemoApp extends SpringBootServletInitializer {
    private ServletContext servletContext;
    public static void main(String[] args){SpringApplication.run(DemoApp.class,args);}
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        builder = builder.properties("test.property:" + servletContext.getInitParameter("test.property"));
        return builder.sources(DemoApp.class);
    }
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        this.servletContext = servletContext;
        super.onStartup(servletContext);
    }
}

Hope this helps.

bittu
  • 786
  • 7
  • 14