Make the logging context (the function we were in when we generated the
message) optional. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1786 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -37,6 +37,24 @@ import java.util.logging.SimpleFormatter;
|
||||
*/
|
||||
public class OneLineLogFormatter extends Formatter
|
||||
{
|
||||
/**
|
||||
* Creates a log formatter that will include the function from which a log
|
||||
* entry was generated.
|
||||
*/
|
||||
public OneLineLogFormatter ()
|
||||
{
|
||||
this(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a log formatter that will optionally include or not include the
|
||||
* function from which a log entry was generated.
|
||||
*/
|
||||
public OneLineLogFormatter (boolean showWhere)
|
||||
{
|
||||
_showWhere = showWhere;
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
public String format (LogRecord record)
|
||||
{
|
||||
@@ -50,6 +68,7 @@ public class OneLineLogFormatter extends Formatter
|
||||
buf.append(" ");
|
||||
buf.append(record.getLevel().getLocalizedName());
|
||||
|
||||
if (_showWhere) {
|
||||
// append the log method call context
|
||||
buf.append(" ");
|
||||
String where = record.getSourceClassName();
|
||||
@@ -68,6 +87,7 @@ public class OneLineLogFormatter extends Formatter
|
||||
buf.append(".");
|
||||
buf.append(record.getSourceMethodName());
|
||||
}
|
||||
}
|
||||
|
||||
// append the message itself
|
||||
buf.append(": ");
|
||||
@@ -94,15 +114,25 @@ public class OneLineLogFormatter extends Formatter
|
||||
* formatter when formatting messages.
|
||||
*/
|
||||
public static void configureDefaultHandler ()
|
||||
{
|
||||
configureDefaultHandler(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the default logging handler to use an instance of this
|
||||
* formatter when formatting messages.
|
||||
*/
|
||||
public static void configureDefaultHandler (boolean showWhere)
|
||||
{
|
||||
Logger logger = LogManager.getLogManager().getLogger("");
|
||||
Handler[] handlers = logger.getHandlers();
|
||||
OneLineLogFormatter formatter = new OneLineLogFormatter();
|
||||
OneLineLogFormatter formatter = new OneLineLogFormatter(showWhere);
|
||||
for (int ii = 0; ii < handlers.length; ii++) {
|
||||
handlers[ii].setFormatter(formatter);
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean _showWhere;
|
||||
protected Date _date = new Date();
|
||||
protected SimpleDateFormat _format =
|
||||
new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SSS");
|
||||
|
||||
Reference in New Issue
Block a user