Logger inputs only to STDOUT?

S

Spare Brain

Hi,

Even though the code defines an output file when initializing the
java.util.Logger, the output always goes to the stdout. The application is
part of a WebLogic application. What needs to be done to change this
behavior? Any suggestions/hints?

Here's how the code looks:

protected final Logger logger =
Logger.getLogger(System.getProperty("java.logger.logDir", "./logs") +
System.getProperty("file.separator") + getClass().getName() + ".log");
....
....
....

if (logger.isLoggable(Level.INFO)) {
logger.info("Executing static SQL query '" + sql + "'");
}
....
....
 
L

Liz

Spare Brain said:
Hi,

Even though the code defines an output file when initializing the
java.util.Logger, the output always goes to the stdout. The application is
part of a WebLogic application. What needs to be done to change this
behavior? Any suggestions/hints?

Here's how the code looks:

protected final Logger logger =
Logger.getLogger(System.getProperty("java.logger.logDir", "./logs") +
System.getProperty("file.separator") + getClass().getName() + ".log");
...
...
...

if (logger.isLoggable(Level.INFO)) {
logger.info("Executing static SQL query '" + sql + "'");
}
...
...
Here is what I do and it works in my vanilla application.
--------------------------------------------------
static Logger logger = Logger.getLogger("global");
// default is one parent handler and that is console at level INFO
logger.setUseParentHandlers(false); // turn off the default handler
// create a 'file' handler (default level = ALL)
fh = new FileHandler("myAppLogFile%g.log", 50000, 10, false);
fh.setFormatter(new SimpleFormatter()); // set the formatter
fh.setLevel(Level.INFO); // change level if you want to
logger.addHandler(fh); // add the handler
 
R

Rogan Dawes

Spare said:
Hi,

Even though the code defines an output file when initializing the
java.util.Logger, the output always goes to the stdout. The application is
part of a WebLogic application. What needs to be done to change this
behavior? Any suggestions/hints?

Here's how the code looks:

protected final Logger logger =
Logger.getLogger(System.getProperty("java.logger.logDir", "./logs") +
System.getProperty("file.separator") + getClass().getName() + ".log");

The parameter for Logger.getLogger(String) is NOT a filename. It is an
hierarchical string (dot-separated like a class name) that allows you to
use different loggers for each sub-package in your application, but use
the default logging mechanism for the top-level package by default.

If you want to configure how and when the logger writes its output, you
need to add an appropriate Handler (ConsoleHandler, FileHandler, etc),
and maybe a Formatter.

Please look at the API docs again, more carefully this time.

Regards,

Rogan
 
A

Arvind

One other way of doing it is to use appenders - appenders come in
different flavours - the default i guess is ConsoleAppender - as the
name indicates is stdout logger...

there is one more appender called FileAppender (or something like
that) - which then defines the file path where you want the log to be
directed to....

needless to say there is a property file which acts as the config and
gets loaded by the log4j package on startup and this property file
needs to be in the classpath....

Arvind
"I dont have any quotes..."
 

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

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,252
Latest member
MeredithPl

Latest Threads

Top