java.util.logging Help

C

cppaddict

I've read a couple of online tutorials about the java.util.logging
API, and have gone over the examples here:

http://java.sun.com/j2se/1.4.2/docs/guide/util/logging/overview.html#2.0

I am still unclear on some basic points, and would love to see a few
more detailed examples -- ideally, a sample app made up of a handful
of collaborating classes. However, I cannot find anything like this
on google. Can anyone point me in the right direction?

I only need to use very simple logging. I want to be able to control
output using levels, output to console or file or both (depending on
my config). I still don't understand why each class needs its own
logger, and I don't know how to control output using config files (or
how to set the location of a config file).

Thanks for any help,
cpp
 
B

Betty

cppaddict said:
I've read a couple of online tutorials about the java.util.logging
API, and have gone over the examples here:

http://java.sun.com/j2se/1.4.2/docs/guide/util/logging/overview.html#2.0

I am still unclear on some basic points, and would love to see a few
more detailed examples -- ideally, a sample app made up of a handful
of collaborating classes. However, I cannot find anything like this
on google. Can anyone point me in the right direction?

I only need to use very simple logging. I want to be able to control
output using levels, output to console or file or both (depending on
my config). I still don't understand why each class needs its own
logger, and I don't know how to control output using config files (or
how to set the location of a config file).

Thanks for any help,
cpp

static Logger logger = Logger.getLogger("global"); // standard java logging
capability
static Handler fh = null; // file handler
static Handler ch = null; // console handler


try
{
fh = new FileHandler("LOGFILE-NewDraw%g.log", 50000, 10, false); //
default level = ALL
ch = new ConsoleHandler(); // default level = INFO
}
catch (SecurityException e1)
{
System.err.println("Security Exception"); // should not happen
e1.printStackTrace();
System.exit(1);
}
catch (IOException e1)
{
System.err.println("IO Exception");
e1.printStackTrace();
System.exit(1);
}
// default handler for console is SimpleFormatter()
fh.setFormatter(new SimpleFormatter()); // default handler for file is XML
fh.setLevel(Level.ALL); // change level here if you want to change it
ch.setLevel(Level.ALL);
logger.addHandler(fh);
logger.addHandler(ch);
logger.setLevel(Level.ALL); // null
// default is one parent handler and that is console at level INFO, so kill
it
// since we want to have control over all handlers
logger.setUseParentHandlers(false);

// a couple of test messages, both go into log file
System.out.println("testing level \"fine\""); // should not see (ch level is
INFO)
System.out.println("testing level \"warning\""); // should see this msg
 

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,992
Messages
2,570,220
Members
46,805
Latest member
ClydeHeld1

Latest Threads

Top