how to init log4j Logger instance

V

vaneric

hello guys,
I am trying to use log4j Logger in my web application.I decided to use
it in my servlet as given below.

import org.apache.log4j.Logger;
public class ReqViewerServlet extends HttpServlet {
private Logger log;
public ReqViewerServlet()
{ java.util.ResourceBundlebundle=java.util.ResourceBundle.getBundle
("include");
String logfile=bundle.getString("logfilename");
log=Logger.getLogger(logfile);
}
....
}
where I created an include.properties file containing
------------
logfilename=myapplog

and in log4j.properties ,
-----------
log4j.logger.myapplog=DEBUG,myLogfile
log4j.appender.myLogfile.File=mywebapp.log

--------------------
As expected this causes the log.debug(..) statements in the servlet
code to be written to mywebapplog.log

What bothers me is that ,I have a servlet constructor solely for
creating the Logger instance.It doesn't feel right.Is there a better
way to initialize the Logger instance in my code?

thanks in advance
eric
 
A

Arne Vajhøj

vaneric said:
I am trying to use log4j Logger in my web application.I decided to use
it in my servlet as given below.

import org.apache.log4j.Logger;
public class ReqViewerServlet extends HttpServlet {
private Logger log;
public ReqViewerServlet()
{ java.util.ResourceBundlebundle=java.util.ResourceBundle.getBundle
("include");
String logfile=bundle.getString("logfilename");
log=Logger.getLogger(logfile);
}
...
}
where I created an include.properties file containing
------------
logfilename=myapplog

and in log4j.properties ,
-----------
log4j.logger.myapplog=DEBUG,myLogfile
log4j.appender.myLogfile.File=mywebapp.log

--------------------
As expected this causes the log.debug(..) statements in the servlet
code to be written to mywebapplog.log

What bothers me is that ,I have a servlet constructor solely for
creating the Logger instance.It doesn't feel right.Is there a better
way to initialize the Logger instance in my code?

A very widely used convention is to use:

Logger log=Logger.getLogger(Foobar.class.getName());

in the Foobar class.

I do not understand what the code in your servlet constructor
is doing.

Arne
 
L

Lew

There's absolutely nothing wrong with creating a constructor for that purpose,
although as Arne points out, the way you construct the Logger is bizarre.

Another option with servlets, perhaps a better one, is to initialize the
Logger in the init() method. This will give you access to the ServletContext
for configuration items, e.g., those from the web.xml descriptor.
A very widely used convention is to use:

Logger log=Logger.getLogger(Foobar.class.getName());

in the Foobar class.

I do not understand what the code in your servlet constructor
is doing.

For log4j, another standard way to initialize the logger is:

Logger logger = Logger.getLogger( Foobar.class );
 
A

Arne Vajhøj

Lew said:
For log4j, another standard way to initialize the logger is:

Logger logger = Logger.getLogger( Foobar.class );

The difference is very small:

static
public
Logger getLogger(String name) {
return LogManager.getLogger(name);
}

static
public
Logger getLogger(Class clazz) {
return LogManager.getLogger(clazz.getName());
}

Arne
 
L

Lew

Arne said:
The difference is very small:

static
public
Logger getLogger(String name) {
return LogManager.getLogger(name);
}

static
public
Logger getLogger(Class clazz) {
return LogManager.getLogger(clazz.getName());
}

Given how many posts appear in this forum by self-styled "programmers" who
whine about Java's "excessive" typing requirements, it seemed worthwhile to
point out how to save the ten keystrokes (+3 for shift) for those folks.
 
V

vaneric

A very widely used convention is to use:
Logger log=Logger.getLogger(Foobar.class.getName());
in the Foobar class.


thanks for the reply..forgive me if this question sounds stupid..I am
a newbie to j2ee and log4j.

I modified the class like this

public class ReqViewerServlet extends HttpServlet {
private Logger log;
public void init(){
log=Logger.getLogger(ReqViewerServlet.class.getName());
}
public void doGet(HttpServletRequest request, HttpServletResponse
response)throws ServletException, java.io.IOException {
log.debug("doGet() called");
...
}

}

Then,if I wanted the log to be written to ,say 'mywebapp.log' ,how
should I modify the log4j.properties?

thanks
eric
 
A

Arne Vajhøj

Lew said:
Given how many posts appear in this forum by self-styled "programmers"
who whine about Java's "excessive" typing requirements, it seemed
worthwhile to point out how to save the ten keystrokes (+3 for shift)
for those folks.

:)

Arne
 
A

Arne Vajhøj

I modified the class like this

public class ReqViewerServlet extends HttpServlet {
private Logger log;
public void init(){
log=Logger.getLogger(ReqViewerServlet.class.getName());
}
public void doGet(HttpServletRequest request, HttpServletResponse
response)throws ServletException, java.io.IOException {
log.debug("doGet() called");
...
}

}

Then,if I wanted the log to be written to ,say 'mywebapp.log' ,how
should I modify the log4j.properties?

Just like you usually do for log4j.

Something like:

log4j.category.ReqViwerServlet = debug, logfile
log4j.appender.logfile.threshold = debug
log4j.appender.logfile = org.apache.log4j.FileAppender
log4j.appender.logfile.file = /logdir/mywebapp.log
log4j.appender.logfile.layout = org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern = %-30c %d %-5p %m%n

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top