下面列出了java.util.logging.LogRecord#setSourceMethodName ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Reports a warning to the specified listeners.
*
* @param listeners the listeners where to report the warning.
* @param caller the caller class to report, preferably a public class.
* @param method the caller method to report, preferable a public method.
* @param exception the exception that occurred, or {@code null} if none.
* @param resources the resources bundle for {@code key} and {@code arguments}, or {@code null} for {@link Resources}.
* @param key one or {@link Resources.Keys} constants.
* @param arguments values to be formatted in the {@link java.text.MessageFormat} pattern.
*/
static void warning(final StoreListeners listeners, final Class<?> caller, final String method,
final Exception exception, IndexedResourceBundle resources, final short key, final Object... arguments)
{
if (resources == null) {
resources = Resources.forLocale(listeners.getLocale());
}
final LogRecord record = resources.getLogRecord(Level.WARNING, key, arguments);
record.setLoggerName(Modules.NETCDF);
record.setSourceClassName(caller.getCanonicalName());
record.setSourceMethodName(method);
if (exception != null) {
// TODO: avoid reporting the full exception stack trace (maybe leverage QuietLogRecord).
record.setThrown(exception);
}
listeners.warning(record);
}
@Test
public void testFormat() {
LogRecord record = new LogRecord( Level.FINER, "This is an error log message." );
record.setLoggerName( "my.logger.name" );
record.setSourceClassName( "my.package.Class" );
record.setSourceMethodName( "myMethod" );
record.setMillis( new Date().getTime());
String s = new RoboconfLogFormatter().format( record );
Assert.assertEquals( 2, s.split( "\n" ).length );
Assert.assertTrue( s.contains( "\n" + record.getMessage()));
Assert.assertTrue( s.startsWith( "[" ));
String fullName = record.getSourceClassName() + "#" + record.getSourceMethodName() + "\n";
Assert.assertTrue( s.contains( fullName ));
}
private LogRecord eventToRecord(LoggingEvent event, Level julLevel) {
String format = event.getMessage();
Object[] arguments = event.getArgumentArray();
FormattingTuple ft = MessageFormatter.arrayFormat(format, arguments);
if (ft.getThrowable() != null && event.getThrowable() != null) {
throw new IllegalArgumentException("both last element in argument array and last argument are of type Throwable");
}
Throwable t = event.getThrowable();
if (ft.getThrowable() != null) {
t = ft.getThrowable();
throw new IllegalStateException("fix above code");
}
LogRecord record = new LogRecord(julLevel, ft.getMessage());
record.setLoggerName(event.getLoggerName());
record.setMillis(event.getTimeStamp());
record.setSourceClassName(EventConstants.NA_SUBST);
record.setSourceMethodName(EventConstants.NA_SUBST);
record.setThrown(t);
return record;
}
/**
* Logs the given exception before to abort installation. We use logging service instead than
* propagating the exception because OpenOffice does not report the exception message.
*/
private static void fatalException(final String method, final String message, final Throwable exception) {
final Logger logger = Logger.getLogger(LOGGER);
final LogRecord record = new LogRecord(Level.SEVERE, message);
record.setSourceClassName(Registration.class.getName());
record.setSourceMethodName(method);
record.setLoggerName(LOGGER);
record.setThrown(exception);
logger.log(record);
}
/**
* Reports a warning.
*
* @param source the source class, either {@code MetadataSource} or {@code MetadataWriter}.
* @param method the method to report as the warning emitter.
* @param record the warning to report.
*/
final void warning(final Class<? extends MetadataSource> source, final String method, final LogRecord record) {
record.setSourceClassName(source.getCanonicalName());
record.setSourceMethodName(method);
record.setLoggerName(Loggers.SQL);
final Filter filter = logFilter;
if (filter == null || filter.isLoggable(record)) {
CachedStatement.LOGGER.log(record);
}
}
@Override
public void log( final StackTraceElement caller, final MessageLevel level, final String id, final String message, final Throwable thrown ) {
LogRecord record = new LogRecord( toLevel( level ), message );
record.setSourceClassName( caller.getClassName() );
record.setSourceMethodName( caller.getMethodName() );
if( thrown != null ) {
record.setThrown( thrown );
}
logger.log( record );
}
@SuppressWarnings("deprecation") // setMillis is deprecated in JDK 9
protected Object writeReplace() {
LogRecord serialized = new LogRecord(getLevel(), getMessage());
serialized.setLoggerName(getLoggerName());
serialized.setResourceBundle(getResourceBundle());
serialized.setResourceBundleName(getResourceBundleName());
serialized.setSourceClassName(getSourceClassName());
serialized.setSourceMethodName(getSourceMethodName());
serialized.setSequenceNumber(getSequenceNumber());
serialized.setParameters(getParameters());
serialized.setThreadID(getThreadID());
serialized.setMillis(getMillis());
serialized.setThrown(getThrown());
return serialized;
}
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());
}
}
public void logrb(Level level, String sourceClass, String sourceMethod,
String bundleName, String msg, Object params[]) {
if (isLoggable(level)) {
LogRecord lr = new LogRecord(level, msg);
lr.setSourceClassName(sourceClass);
lr.setSourceMethodName(sourceMethod);
lr.setParameters(params);
doLog(lr, bundleName);
}
}
@Deprecated
public void logrb(Level level, String sourceClass, String sourceMethod,
String bundleName, String msg, Throwable thrown) {
if (isLoggable(level)) {
LogRecord lr = new LogRecord(level, msg);
lr.setSourceClassName(sourceClass);
lr.setSourceMethodName(sourceMethod);
lr.setThrown(thrown);
doLog(lr, bundleName);
}
}
public void throwing(final String sourceClass, final String sourceMethod, final Throwable thrown) {
if (isLoggable(Level.FINER)) {
final LogRecord lr = new LogRecord(Level.FINER, "THROW");
lr.setSourceClassName(sourceClass);
lr.setSourceMethodName(sourceMethod);
lr.setThrown(thrown);
doLog(lr);
}
}
/**
* {@inheritDoc}
* <p>
* Overridden to create a {@link LogRecord} based on the log information.
* </p>
*
* @see Log#log(Level, Throwable, String, Object[])
*/
@Override
protected final void doLog(final Level level, final Throwable thrown,
final String template, final Object... args) {
if (myDelegate.isLoggable(level)) {
final Thread currentThread = Thread.currentThread();
final LogRecord record = new LogRecord(level,
format(template, args));
record.setLoggerName(myDelegate.getName());
record.setThrown(thrown);
record.setThreadID((int) Thread.currentThread().getId());
// Note the name of the class is the AbstractLog which is where all
// of the public log methods are implemented.
boolean lookingForThisClass = true;
for (final StackTraceElement element : currentThread
.getStackTrace()) {
final String className = element.getClassName();
// Find this method (and maybe others from this class).
if (lookingForThisClass) {
lookingForThisClass = !CLASS_NAME.equals(className);
}
else {
// And back until we are past this class.
if (!CLASS_NAME.equals(className)) {
record.setSourceClassName(className);
record.setSourceMethodName(element.getMethodName());
break;
}
}
}
// Finally - log it.
myDelegate.log(record);
}
}
static void logOnly(InternalLogId logId, Level logLevel, String msg) {
if (logger.isLoggable(logLevel)) {
LogRecord lr = new LogRecord(logLevel, "[" + logId + "] " + msg);
// No resource bundle as gRPC is not localized.
lr.setLoggerName(logger.getName());
lr.setSourceClassName(logger.getName());
// Both logger methods are called log in ChannelLogger.
lr.setSourceMethodName("log");
logger.log(lr);
}
}
public void logp(final Level level, final String sourceClass, final String sourceMethod, final String msg, final Object[] params) {
if (isLoggable(level)) {
final LogRecord lr = new LogRecord(level, msg);
lr.setSourceClassName(sourceClass);
lr.setSourceMethodName(sourceMethod);
lr.setParameters(params);
doLog(lr);
}
}
public void throwing(String sourceClass, String sourceMethod, Throwable thrown) {
if (isLoggable(Level.FINER)) {
LogRecord lr = new LogRecord(Level.FINER, "THROW");
lr.setSourceClassName(sourceClass);
lr.setSourceMethodName(sourceMethod);
lr.setThrown(thrown);
doLog(lr);
}
}
/**
* Invoked after we finished to create all tables. This method flush the warnings
* (omitting duplicated warnings), then returns all tables including dependencies.
*/
final Collection<Table> finish() throws DataStoreException {
for (final Table table : tables.values()) {
table.setDeferredSearchTables(this, tables);
}
for (final ResourceInternationalString warning : warnings) {
final LogRecord record = warning.toLogRecord(Level.WARNING);
record.setSourceClassName(SQLStore.class.getName());
record.setSourceMethodName("components"); // Main public API trigging the database analysis.
listeners.warning(record);
}
return tables.values();
}
public void logrb(final Level level, final String sourceClass, final String sourceMethod,
final String bundleName, final String msg, final Object[] params) {
if (isLoggable(level)) {
final LogRecord lr = new LogRecord(level, msg);
lr.setSourceClassName(sourceClass);
lr.setSourceMethodName(sourceMethod);
lr.setParameters(params);
doLog(lr, bundleName);
}
}
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());
}
}
/**
* No formatter is needed
*
* @param level level of log record priority
* @param message message of log record
* @return A LogRecord instance
*/
public LogRecord generateLogRecord(Level level, String message)
{
LogRecord record = new LogRecord(Level.INFO, "null");
record.setSourceClassName(this.srcClassName);
record.setSourceMethodName(this.srcMethodName);
return record;
}
@Test
public void requireThatJdk14PropertiesAreAvailableThroughServiceReference() {
MyLogService logService = new MyLogService();
Logger log = newLogger(logService);
LogRecord record = new LogRecord(Level.INFO, "message");
record.setLoggerName("loggerName");
record.setInstant(Instant.ofEpochMilli(69));
Object[] parameters = new Object[0];
record.setParameters(parameters);
ResourceBundle resouceBundle = new MyResourceBundle();
record.setResourceBundle(resouceBundle);
record.setResourceBundleName("resourceBundleName");
record.setSequenceNumber(69);
record.setSourceClassName("sourceClassName");
record.setSourceMethodName("sourceMethodName");
record.setThreadID(69);
Throwable thrown = new Throwable();
record.setThrown(thrown);
log.log(record);
ServiceReference<?> ref = logService.lastServiceReference;
assertNotNull(ref);
assertTrue(Arrays.equals(new String[] { "LEVEL",
"LOGGER_NAME",
"MESSAGE",
"MILLIS",
"PARAMETERS",
"RESOURCE_BUNDLE",
"RESOURCE_BUNDLE_NAME",
"SEQUENCE_NUMBER",
"SOURCE_CLASS_NAME",
"SOURCE_METHOD_NAME",
"THREAD_ID",
"THROWN" },
ref.getPropertyKeys()));
assertEquals(Level.INFO, ref.getProperty("LEVEL"));
assertEquals("loggerName", ref.getProperty("LOGGER_NAME"));
assertEquals("message", ref.getProperty("MESSAGE"));
assertEquals(69L, ref.getProperty("MILLIS"));
assertSame(parameters, ref.getProperty("PARAMETERS"));
assertSame(resouceBundle, ref.getProperty("RESOURCE_BUNDLE"));
assertEquals("resourceBundleName", ref.getProperty("RESOURCE_BUNDLE_NAME"));
assertEquals(69L, ref.getProperty("SEQUENCE_NUMBER"));
assertEquals("sourceClassName", ref.getProperty("SOURCE_CLASS_NAME"));
assertEquals("sourceMethodName", ref.getProperty("SOURCE_METHOD_NAME"));
assertEquals(69, ref.getProperty("THREAD_ID"));
assertSame(thrown, ref.getProperty("THROWN"));
assertNull(ref.getProperty("unknown"));
}