简体   繁体   English

每个上下文的Log4J

[英]Log4J for each context

currently I'm using Log4J2 on WebApps, which run on Tomcat. 目前我在WebApps上使用Log4J2,它运行在Tomcat上。 Each webapp has to push its logs to an individual folder, named like the webapp's context. 每个webapp都必须将其日志推送到单个文件夹,名称类似于webapp的上下文。

In the webapp I implemented an ServletContextListener, which get's noticed when the Webapp Context is ready. 在webapp中,我实现了一个ServletContextListener,当Webapp Context准备就绪时会引起注意。 It sets the logging path to the system property like this: 它设置系统属性的日志记录路径,如下所示:

public void contextInitialized(ServletContextEvent sce) {
    context = sce.getServletContext().getContextPath();
    if(context == null || context.isEmpty()){
        context = "ROOT";
    }
    System.setProperty("WebappContext", context);
    log.info("Context \"" + context + "\" erstellt");
}

Then I use this property in log4j2.xml: 然后我在log4j2.xml中使用这个属性:

<Properties>
    <Property name="log-path">/srv/tomcat/logs/${sys:WebappContext}</Property>
</Properties>
<Appenders>
    <RollingFile name="rollingLogFile" fileName="${log-path}/out.log"
                 filePattern="${log-path}/out-%d{MMM-dd--HH-mm}.log" >
     ...
    </RollingFile>

This works fine when I deploy the first webapp. 当我部署第一个webapp时,这很好用。 But as soon as I deploy two or more, all logs from all contexts go to the newest folder, because the system property is global for all instances. 但是,只要我部署了两个或更多,所有来自所有上下文的日志都会转到最新的文件夹,因为系统属性对于所有实例都是全局的。 Sometimes I even get a folder named "{sys:WebappContext}", where some logs are written. 有时我甚至会得到一个名为“{sys:WebappContext}”的文件夹,其中会写入一些日志。

What is the right way to achieve the loggin that I want? 实现我想要的登录的正确方法是什么?

Try using the web context lookup instead of system lookup. 尝试使用Web上下文查找而不是系统查找。

Source: https://logging.apache.org/log4j/2.0/manual/lookups.html#WebLookup 资料来源: https//logging.apache.org/log4j/2.0/manual/lookups.html#WebLookup

The WebLookup allows applications to retrieve variables that are associated with the ServletContext. WebLookup允许应用程序检索与ServletContext关联的变量。 In addition to being able to retrieve various fields in the ServletContext, WebLookup supports looking up values stored as attributes or configured as initialization parameters. 除了能够在ServletContext中检索各种字段之外,WebLookup还支持查找存储为属性的值或配置为初始化参数。 The following table lists various keys that can be retrieved: 下表列出了可以检索的各种键:

  • attr.name Returns the ServletContext attribute with the specified name attr.name返回具有指定名称的ServletContext属性
  • contextPath The context path of the web application contextPath Web应用程序的上下文路径
  • effectiveMajorVersion Gets the major version of the Servlet specification that the application represented by this ServletContext is based on. effectiveMajorVersion获取此ServletContext所代表的应用程序所基于的Servlet规范的主要版本。
  • effectiveMinorVersion Gets the minor version of the Servlet specification that the application represented by this ServletContext is based on. effectiveMinorVersion获取此ServletContext所代表的应用程序所基于的Servlet规范的次要版本。
  • initParam.name Returns the ServletContext initialization parameter with the specified name initParam.name返回具有指定名称的ServletContext初始化参数
  • majorVersion Returns the major version of the Servlet API that this servlet container supports. majorVersion返回此servlet容器支持的Servlet API的主要版本。
  • minorVersion Returns the minor version of the Servlet API that this servlet container supports. minorVersion返回此servlet容器支持的Servlet API的次要版本。
  • rootDir Returns the result of calling getRealPath with a value of "/". rootDir返回使用值“/”调用getRealPath的结果。
  • serverInfo Returns the name and version of the servlet container on which the servlet is running. serverInfo返回运行servlet的servlet容器的名称和版本。
  • servletContextName Returns the name of the web application as defined in the display-name element of the deployment descriptor servletContextName返回部署描述符的display-name元素中定义的Web应用程序的名称

Example

<Appenders>
  <File name="ApplicationLog" fileName="${web:rootDir}/app.log"/>
</Appenders>

I typically just use a separate log4j2.xml for each web app with 我通常只为每个Web应用程序使用单独的log4j2.xml

<properties>
    <property name="LOG_DIR">${sys:catalina.home}/logs/AppName</property>
</properties>

However, if you want a single log4j2.xml, following alan7678's advice you should be able to add 但是,如果你想要一个log4j2.xml,按照alan7678的建议你应该能够添加

<context-param>
    <param-name>applicationName</param-name>
    <param-value>AccountService</param-value>
</context-param>

to web.xml and then use 到web.xml然后使用

<properties>
    <property name="LOG_DIR">${sys:catalina.home}/logs/${web:applicationName}</property>
</properties>

However, I haven't tried this myself. 但是,我自己没试过。

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

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