本文整理匯總了Java中java.util.logging.LogRecord.setSourceMethodName方法的典型用法代碼示例。如果您正苦於以下問題:Java LogRecord.setSourceMethodName方法的具體用法?Java LogRecord.setSourceMethodName怎麽用?Java LogRecord.setSourceMethodName使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.logging.LogRecord
的用法示例。
在下文中一共展示了LogRecord.setSourceMethodName方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: logThrowing
import java.util.logging.LogRecord; //導入方法依賴的package包/類
/**
* Logs throwing of exceptions.
*
* @param loggerName
* @param thrown
*/
public static void logThrowing(String loggerName, Throwable thrown) {
if (loggerName != null) {
Logger logger = loggers.get(loggerName);
if (logger != null) {
Thread current = Thread.currentThread();
int totalStackOffset = 2;
StackTraceElement[] trace = current.getStackTrace();
LogRecord logRecord = new LogRecord(Level.ALL, "Exception thrown!");
if (trace.length >= totalStackOffset) {
StackTraceElement caller = trace[totalStackOffset];
logRecord.setSourceMethodName(caller.getMethodName());
logRecord.setSourceClassName(caller.getClassName());
int lineNumber = caller.getLineNumber();
logRecord.setParameters(new Object[] { lineNumber });
logRecord.setThrown(thrown);
}
logger.log(logRecord);
}
}
}
示例2: logMessage
import java.util.logging.LogRecord; //導入方法依賴的package包/類
/**
* Logs a message to the logger with the given name. If there is no such logger, the log-message is ignored.
*
* @param loggerName
* @param message
* @param level
*/
private static void logMessage(String loggerName, String message, Level level, int stackOffset) {
if (loggerName == null) {
return;
}
Logger logger = loggers.get(loggerName);
if (logger == null) {
System.out.println("Tried to log message to " + loggerName + " which isn't available");
return;
} else {
LogRecord logRecord = new LogRecord(level, message);
// if (!(level.intValue() < logger.getLevel().intValue() ||
// logger.getLevel().intValue() == Level.OFF.intValue())) {
// System.out.print();//Use breakpoint & debug mode to quickly find
// where a message was logged from
// }
// this method is called from another method -- and the logger would
// normally log "logMessage" as its caller, which is rather
// unhelpful
// -> go to the call stack and get the relevant message!
Thread current = Thread.currentThread();
int totalStackOffset = 2 + stackOffset;
StackTraceElement[] trace = current.getStackTrace();
if (trace.length >= totalStackOffset) {
StackTraceElement caller = trace[totalStackOffset];
logRecord.setSourceMethodName(caller.getMethodName() + " (Logger: " + loggerName + ")");
logRecord.setSourceClassName(caller.getClassName());
logRecord.setLoggerName(loggerName);
int lineNumber = caller.getLineNumber();
logRecord.setParameters(new Object[] { lineNumber });
}
logger.log(logRecord);
}
}
示例3: inferCaller
import java.util.logging.LogRecord; //導入方法依賴的package包/類
private void inferCaller( Class wrapperClass, LogRecord lrec )
{
// Private method to infer the caller's class and method names
// Get the stack trace.
StackTraceElement stack[] = (new Throwable()).getStackTrace();
StackTraceElement frame = null ;
String wcname = wrapperClass.getName() ;
String baseName = LogWrapperBase.class.getName() ;
// The top of the stack should always be a method in the wrapper class,
// or in this base class.
// Search back to the first method not in the wrapper class or this class.
int ix = 0;
while (ix < stack.length) {
frame = stack[ix];
String cname = frame.getClassName();
if (!cname.equals(wcname) && !cname.equals(baseName)) {
break;
}
ix++;
}
// Set the class and method if we are not past the end of the stack
// trace
if (ix < stack.length) {
lrec.setSourceClassName(frame.getClassName());
lrec.setSourceMethodName(frame.getMethodName());
}
}