what is the right way to use log4j within jsp pages

浪子不回头ぞ 提交于 2019-12-21 03:38:14

问题


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.


回答1:


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.jsp is better, as you can set up logging rules for JSPS logger, that would later be applied to all sub-loggers.

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




回答2:


Firstly, import the required package i.e.

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

then,

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

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

<% 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.




回答3:


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:

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.




回答4:


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.

<%@ 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>



回答5:


Use the appropriate ConversionPattern when configuring log4j, e.g:

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

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




回答6:


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

<%@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")); %> 



回答7:


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

1. Use file name directly but replace dot with underscore

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) ....".

2. Use request 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

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.

4. Programatically get jsp file name

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.

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



来源:https://stackoverflow.com/questions/1594931/what-is-the-right-way-to-use-log4j-within-jsp-pages

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!