简体   繁体   English

来自web.xml的登录值

[英]logback value from web.xml

I'm trying to take a value from spring profile set in web.xml. 我试图从web.xml中设置的spring配置文件中获取一个值。 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: web.xml中:

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

logback.xml: 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? 有谁知道我如何从web.xml中获取值并将其放入logback.xml中?

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 : 如果您使用的是Spring 3.1+,则这些上下文参数将作为env变量提供,并且在logback中,您可以像下面这样轻松地引用它们:

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

source is the name of your key. source是密钥的名称。 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. 第二解决方案:您也可以在将context-param定义为spring属性,然后在logback中引用与上述第一解决方案相同的情况下使用此方法。

@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. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM