Java 在jsp页面中使用log4j的正确方法是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1594931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:33:57  来源:igfitidea点击:

what is the right way to use log4j within jsp pages

javajsplog4j

提问by flybywire

I mean, I want the logger name to reflect the source.jsp file, no matter if it is included in another file or compiled to a class or whatever.

我的意思是,我希望记录器名称反映 source.jsp 文件,无论它是包含在另一个文件中还是编译为类或其他任何文件。

采纳答案by Alexander Pogrebnyak

What's wrong with:

有什么问题:

Logger logger = Logger.getLogger( "source.jsp" );

You can prepend it with a better non-ambiguous prefix, of course. Actually, something along the lines JSPS.source.jspis better, as you can set up logging rules for JSPSlogger, that would later be applied to all sub-loggers.

当然,您可以在它前面加上一个更好的非歧义前缀。实际上,沿线的东西JSPS.source.jsp更好,因为您可以为JSPS记录器设置日志记录规则,然后将其应用于所有子记录器。

Having said this, why do you need to log from JSP directly?

说了这么多,为什么要直接从JSP登录呢?

回答by Ryan

The following is the code. All the configuration file placement and configuration are the same as how it is use in Servlet or other class.

以下是代码。所有配置文件的放置和配置与在 Servlet 或其他类中的使用方式相同。

<%@ page import="org.apache.log4j.Logger" %>
<html>
    <head>
        <title>Demonstration log4j usage in jsp</title>
    </head>
    <body>
        <% Logger log = Logger.getLogger("com.mobilefish.demo.test");
           log.debug("Show DEBUG message");
           log.info("Show INFO message");
           log.warn("Show WARN message");
           log.error("Show ERROR message");
           log.fatal("Show FATAL message"); %>
        <b>The log messages are shown in the Tomcat console and in the ${catalina.home}/logs/demo.log file.</b>
    </body>
</html>

回答by skaffman

You could write a factory method which takes the current request as a parameter, and which obtains a Logger based on the JSP name, something like this:

您可以编写一个工厂方法,将当前请求作为参数,并根据 JSP 名称获取 Logger,如下所示:

public static Logger getLogger(HttpServletRequest request) {
    String requestUri = request.getRequestURI();
    String jspName = requestUri.substring(requestUri.lastIndexOf('/'));
    return Logger.getLogger(jspName);
}

You might have to play with it a bit to make it work (I haven't tested the above code), but that's the gist of it.

您可能需要稍微尝试一下才能使其工作(我还没有测试过上面的代码),但这就是它的要点。

This could be used directly from the JSP, or from a bean or tag class which is used by the JSP, as long as it has access to the request object.

这可以直接从 JSP 使用,也可以从 JSP 使用的 bean 或标记类使用,只要它可以访问请求对象。

回答by NT_

Use the appropriate ConversionPatternwhen configuring log4j, e.g:

配置 log4j 时使用适当的ConversionPattern,例如:

%d [%C] %-5p %c - %m%n

Here, the %C outputs the fully qualified class name when you call any of the Logger class methods.

在这里,当您调用任何 Logger 类方法时, %C 会输出完全限定的类名。

回答by Pavan Dave

Firstly, import the required package i.e.

首先,导入所需的包即

<%@page import="org.apache.log4j.Logger"%>

then,

然后,

 <%! static Logger logger = Logger.getLogger(jsppagename_jsp.class); %>

the jsppagename_jspmay change, according to the server which you are using. And then, use anywhere inside jsp like:

jsppagename_jsp可根据您所使用的服务器更改。然后,在 jsp 中的任何地方使用,例如:

<% logger.info("This is test."); %>

The IDE may show an error message at the declaration of logger object. But, don't worry, the server like tomcat will automatically create the corresponding servlet class of each jsp page inside tomcat directly itself.

IDE 可能会在声明记录器对象时显示错误消息。不过不用担心,像tomcat这样的服务器会直接自己在tomcat里面自动创建每个jsp页面对应的servlet类。

回答by k3b

Since it is likely that you want to output the content of jsp-el-variables in log4net you can use this code

因为很可能你想在log4net中输出jsp-el-variables的内容你可以使用这个代码

<%@page import="org.apache.log4j.Logger"%>

<%-- output of the jsp-el variables "orderID" and "orderDate" in log4j --%>
<c:set var="Parameter4Log4J" value="orderID=${orderID} orderDate=${orderDate}" />
<% Logger.getLogger("jsp.order.orderconfirmation").info(
               pageContext.getAttribute("Parameter4Log4J")); %> 

回答by Scott Chu

Though an old question, I'd like to provide some options. Say jsp filename = for_example.jsp:

虽然是一个老问题,但我想提供一些选择。说 jsp 文件名 = for_example.jsp:

1. Use file name directly but replace dot with underscore

1.直接使用文件名但用下划线代替点

Logger log = Logger.getLogger("for_example_jsp");

(note: constantly mistake is to use 'for_example.jsp' directly, it will be treated as class name and then when AP server, say Tomcat, finds no such class path, it will output log message in catalina.out as "...(jsp) ....".

(注意:经常错误的是直接使用'for_example.jsp',它会被当作类名,然后当AP服务器,比如Tomcat,发现没有这样的类路径时,它会在catalina.out中输出日志信息为“.. .(jsp) ....”。

2. Use request URI

2.使用请求URI

Logger log = Logger.getLogger(request.getRequestURI());

Somebody likes this. I don't know why but I've seen this before in somebody's codes.

有人喜欢这个。我不知道为什么,但我以前在某人的代码中看到过这一点。

3. Use Class name

3.使用类名

Logger log = Logger.getLogger(this.getClass());

this will usually get 'for_example_jsp" except when for_example.jsp is included in some other servlet, say 'test_servlet', it will be 'including file's log name' + 'an ordered number', e.g. test_servlet_include_005.

这通常会得到“for_example_jsp”,除非for_example.jsp 包含在其他一些servlet 中,例如“test_servlet”,它将是“包括文件的日志名称”+“有序编号”,例如test_servlet_include_005。

4. Programatically get jsp file name

4. 以编程方式获取jsp文件名

String __jspName = this.getClass().getSimpleName(); // Get jsp program name
Logger log = Logger.getLogger(__jspName);
__jspName = __jspName.replaceAll("_","."); // get back true jsp file name

However, this is not necessarily right with respect to what AP server and version you are using.

但是,对于您使用的 AP 服务器和版本,这不一定是正确的。

I personally use method 1 as I think it's most reliable.

我个人使用方法1,因为我认为它最可靠。