• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Logging in servlet

 
Max Vaha
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good evening everyone!
I want to log some events in my "HelloWorld" servlet using java.util.logging.Logger (to write "init" to a text file). But I have found only SE examples using Google((.
Can anyone give me an advise?
 
K.Suresh Kumar
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The following code example will be useful for you.....

Writing Log Records to a Log File
To make a logger write log records to a file, you need to add a file handler to the logger.

try {
// Create a file handler that write log record to a file called my.log
FileHandler handler = new FileHandler("my.log");

// Add to the desired logger
Logger logger = Logger.getLogger("com.mycompany");
logger.addHandler(handler);
} catch (IOException e) {
}

By default, a file handler overwrites the contents of the log file each time it is created. This example creates a file handler that appends.

try {
// Create an appending file handler
boolean append = true;
FileHandler handler = new FileHandler("my.log", append);

// Add to the desired logger
Logger logger = Logger.getLogger("com.mycompany");
logger.addHandler(handler);
} catch (IOException e) {
}


Regards,
Suresh Kumar.K
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's already a log method in the GenericServlet class (which your servlet extends).
 
Max Vaha
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K.Suresh Kumar,
thank you very much! Works perfectly.

Christophe Verre, thanks for your answer, but I need my own log file, GenericServlet is unsuitable for me.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic