Logging in Java
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;
开发者_Python百科/**
* 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.
You could have achieved desired configuration without even writing any code. Here is how: Browse to the directory where java is installed on your system.
Go to the jre/lib (or jre\lib in windows) subdirectory,and look for the file "logging.properties". You can create it if it doesn't exist.
Edit the line having content "handlers = " TO
-For Both System.err and File
"handlers = java.util.logging.ConsoleHandler, java.util.logging.FileHandler"
-For Neither System.err and File
Simply comment that line by adding # in the beginning of the line.
-For Just System.err
"handlers = java.util.logging.ConsoleHandler"
-For Just File
"handlers = java.util.logging.FileHandler"
Done! You are good to go and start logging :)
精彩评论