aspose file tools*
The moose likes Servlets and the fly likes How get the Logger from a servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How get the Logger from a servlet?" Watch "How get the Logger from a servlet?" New topic
Author

How get the Logger from a servlet?

Robert Paris
Ranch Hand

Joined: Jul 28, 2002
Posts: 585
Is there a method to get the Logger used in the server? Like:
Servlet.getLogger()? (I know that one doesn't exist)
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

two ways:
'getServletContext.log(String);' or just 'log(String);' witch is in 'GenericServlet'
Rene


Regards, Rene Larsen
Dropbox Invite
Robert Paris
Ranch Hand

Joined: Jul 28, 2002
Posts: 585
no, that's not what I asked. That's how you log something. I don't want that. I want the Logger itself. Like I asked:
Something like "getLogger()"
boyet silverio
Ranch Hand

Joined: Aug 28, 2002
Posts: 173
you could probably look at the java.util.logging.Logger class it has a getLogger(String name) method which might be of use for your purposes.
Robert Paris
Ranch Hand

Joined: Jul 28, 2002
Posts: 585
No it won't. I want to get the logger from a servlet container. I'm thinking there must be something in the spec that says it's available through the API, right? It'd be dumb (in my opinion) not to make it available somewhere. i mean, put security on it if you want, but have an interface to get it.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Why would I, as a servlet container developer, want to give you, some programmer that I don't know from Adam, access to my logging object? You might do something to it that disables it. A logger logs. What else do you need it to do? I give you everything you need. You don't need direct access to the object. Principle of 'least privelege' and 'don't touch that you might break it!'.

But that's me as a servlet container programmer.

Me as a programmer also wonders: What do you need it for ? I'm just curious.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

As for the spec, I've searched through the Servlet 2.3 spec and nowhere does it mention that a servlet container need give access to the logger, only that it provide a logging service.

If I anticipate correctly, and you are wanting to do some funky logging type things on conditions or certain events (change where the logger logs to if condition 'a' is met, etc...) then while searching through the spec I had a couple good hints... use Filters and/or Context lifecycle listeners. (SRV 6.1.1 and SRV 10.4, respectively)

And finally, if the built-in logger is just not going to do it for you... that's what log4j was made for.
Robert Paris
Ranch Hand

Joined: Jul 28, 2002
Posts: 585
I'm writing a DataSource implementation (it is a connection pool manager that works with a ConnectionPoolDataSource "a la" JDBC 2.0 Optional Extension Spec). I was hoping there would be an easy way for people to direct my DataSource to log to the servlet container's logger. However, if you're right, I have no idea how I'll do that. But it seems there must be a way because otherwise, everyone's going to have a different log file for every component plugged in to the container - ugh.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How get the Logger from a servlet?
 
Similar Threads
Threads question.
Singleton logger for the whole application
Good logging system for WebApp (Struts)
Configure two log4j loggers in one application
Get all categories defined in log4j.xml