Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the right way to use log4j within jsp pages

Tags:

java

jsp

log4j

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.

like image 437
flybywire Avatar asked Oct 20 '09 14:10

flybywire


People also ask

How can I tell which application is using Log4j?

Syft is also able to discern which version of Log4j a Java application contains. The Log4j JAR can be directly included in our project, or it can be hidden away in one of the dependencies we include. For example, using Syft to scan this sample Java project shows that it includes Log4j version 2.14.

Can we use Log4j in Java?

Log4j can be configured through Java code or in a configuration file. Configuration files can be written in XML, JSON, YAML, or properties file format.

What is Log4j file used for?

The log4j. properties file is a log4j configuration file which stores properties in key-value pairs. The log4j properties file contains the entire runtime configuration used by log4j. This file will contain log4j appenders information, log level information and output file names for file appenders.

Can we use system out Println in JSP?

out. println() Since the System object is part of the core Java objects, it can be used everywhere without the need to install any extra classes. This includes Servlets, JSP, RMI, EJB's, ordinary Beans and classes, and standalone applications.


2 Answers

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.

like image 130
Pavan Dave Avatar answered Oct 04 '22 18:10

Pavan Dave


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?

like image 22
Alexander Pogrebnyak Avatar answered Oct 04 '22 19:10

Alexander Pogrebnyak