I am creating a logger in java by executing the following code:
private static final String logFile = "." + File.separator + "Log Files" + File.separator + "Log_" + Long.toString(System.currentTimeMillis());
private static Logger logger = Logger.getLogger("JCS_Logger");
static
{
try
{
logger.addHandler(new FileHandler(logFile ));
}
catch (Exception e)
{
System.err.println("Could not return a static logger");
}
}
If I use this logger, it writes to not only System.err, but also to the file. Ultimately I want to configure the logger to allow it to write to:'
I know that calling logger.setLevel(Level.OFF) will turn off all logging, but I am not sure about how to do it for the list above? Is it done through the Level class? Any ideas or suggestions would be greatly appreciated.
EDIT: Thanks for the responses. I solved it with this code:
/**
* If debug should be turned on
*/
static boolean debug = true;
/**
* If writing debug to file
*/
static boolean writeLogToFile = false;
/**
* If writing debug to System.err
*/
static boolean writeLogToStdErr = true;
/**
* Location for the temp file
*/
private static final String logFile = "." + File.separator + "Log Files" + File.separator + "Log_" + Long.toString(System.currentTimeMillis());
/**
* Instance of the logger
*/
private static Logger logger = Logger.getLogger("JCS_Logger");
/**
* Handler for System.err
*/
private static Handler consoleHandler;
/**
* Handler for the log file
*/
private static Handler fileHandler;
static
{
try
{ //if not debuggin at all
if(debug == false)
{
logger.setLevel(Level.OFF);
}
//if not writing to the file
if(writeLogToFile == true)
{
fileHandler = new FileHandler(BCApp.getLogFileHandlerPath());
logger.addHandler(fileHandler);
}
//if not writing to System.err
if(writeLogToStdErr == false)
{
consoleHandler = logger.getParent().getHandlers()[0];
logger.getParent().removeHandler(consoleHandler);
}
}
catch (Exception e)
{
System.err.println("Could not return a static logger");
}
}
Thanks for the help
You can either add handlers only for the wanted items, making your selection at logging start-up time; or, you can add all the handlers, and tune each Handler via logging thresholds.
To tune a Handler's logging threshold, use handler.setLevel(...);
Note that on start-up a default Handler is already configured. You might need to programmatically remove that handler, or tune it to handle nothing depending on which technique of log handler "handling" you wish to implement.
See the API documentation for that class: Logger (Java Platform SE 6).
You can remove handlers via the removeHandler(Handler) method, so if you want to only output to the console, for example, you can invoke this method to remove the FileHandler.
There are other ways as well. You could take a different approach and set filters for each handler, which would discard messages that are not supposed to be outputted. Maybe play around with the formatting.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With