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:'
- Both System.err and File
- Neither System.err and File
- Just System.err
- Just File
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