如何配置log4j2 Web应用程序 [英] how to configure log4j2 webapplication

查看:87
本文介绍了如何配置log4j2 Web应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Web应用程序来说,它还有些陌生,最近我需要采用一种日志记录机制,为此,我选择Log4J2,在此阅读了该指南,并下载了所需的库.到目前为止,这是我所做的.

Am little new to web applications, recently I was in need to employ a logging mechanism and for that I choose Log4J2, I went through there guide, and downloaded required libraries. This is what so far I did.

1. Added following jars to web-inf/lib
   --  log4j-core2.1.jar
   --  log4j-api-2.1.jar
   --  log4j-web-2.1.jar


2. Added below xml as, log4j2.xml in java/src directory

<?xml version="1.0" encoding="UTF-8"?>
<configuration name="NONPROD" status="OFF">


    <Properties>

        <Property name="log-path">logs</Property>
    </Properties>


    <Appenders>

        <Console name="console-log" target="SYSTEM_OUT">
            <!-- <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n -->
            <!-- </pattern> -->

            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>


        <RollingFile name="info-log" fileName="${log-path}/web-info.log"
            filePattern="${log-path}/web-info-%d{yyyy-MM-dd}.log">
            <PatternLayout>
                <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
                </pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1"
                    modulate="true" />

                <OnStartupTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="20 MB" />
            </Policies>
        </RollingFile>

        <RollingFile name="error-log" fileName="${log-path}/web-error.log"
            filePattern="${log-path}/web-error-%d{yyyy-MM-dd}.log">
            <PatternLayout>
                <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
                </pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1"
                    modulate="true" />

                <OnStartupTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="20 MB" />
            </Policies>
        </RollingFile>


        <RollingFile name="debug-log" fileName="${log-path}/web-debug.log"
            filePattern="${log-path}/web-debug-%d{yyyy-MM-dd}.log">
            <PatternLayout>
                <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
                </pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1"
                    modulate="true" />

                <OnStartupTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="20 MB" />
            </Policies>
        </RollingFile>



        <RollingFile name="trace-log" fileName="${log-path}/web-trace.log"
            filePattern="${log-path}/web-trace-%d{yyyy-MM-dd}.log">
            <PatternLayout>
                <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
                </pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1"
                    modulate="true" />

                <OnStartupTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="20 MB" />
            </Policies>
        </RollingFile>

    </Appenders>

    <Loggers>

        <Logger name="com.demo.web.log4j2.file" level="debug"
            additivity="false">

            <appender-ref ref="trace-log" level="trace" />

            <appender-ref ref="error-log" level="error" />

            <appender-ref ref="debug-log" level="debug" />

            <appender-ref ref="info-log" level="info" />
        </Logger>

        <Logger name="com.demo.web.log4j2.console" level="all"
            additivity="false">
            <AppenderRef ref="console-log" />
        </Logger>


        <Root level="info" additivity="true">

            <AppenderRef ref="console-log" />

        </Root>

    </Loggers>


</configuration>

3. third and last i wrote an small web service to test logging

@Path("/register")
public class Register {

    private DataManager mManager;
     private static final Logger LOG = LogManager.getLogger(Register.class);

    public Register(){
        mManager = DataManager.getInstance();
    }


    @GET
    @Path("/{param}")
    public Response getMsg(@PathParam("param") String msg) {

        LOG.error("Not supported operations");
        return Response.status(Response.Status.BAD_REQUEST)
                .entity("Not supported").build();

    }

但是在控制台中,一旦触发请求,这就是我打印的全部内容.

But in console, upon trigger get request this is all i got printed.

INFO: Server startup in 35959 ms
Not supported operations // this is not in pattern i supplied in log4j2.xml

// this is my pattern
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />

我发现这可能是由于某些问题出了问题,并且正在寻找真正的原因,这是Web编程的新手,请帮我配置我的记录器.

I figure out this is probably due to some thing gone wrong, and am struggling to find the actual cause, being totally new to web programming, please help me out to configure my logger.

谢谢, 技术人员

推荐答案

好的,我发现了这个原因,问题主要是由于两个原因.

Alright, I found the cause behind this, Issue was primarily happening due to two reasons.

  1. 首先我想从catilina属性的jarsToSkip属性中排除log4j *模式
  2. 第二,我在web-inf中保留了两个log4j2.xml,在java/src中保留了另一个,它应该只存在于java/src中.不需要两个文件.

第三次(但不是强制性的),在部署之前只需检查log4j2.xml是否包含在web-inf/classs中,如果没有,则只需添加它即可.

Third, but not mandatory, before deployment just check if log4j2.xml is included inside web-inf/classes or not, if not then simply add it.

就是这样,在解决了此问题之后,现在正在获取正确的日志.

that's it, after following this issue was resolved now am getting proper logs.

这篇关于如何配置log4j2 Web应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆