java.util.logging.LogRecord#setSourceMethodName ( )源码实例Demo

下面列出了java.util.logging.LogRecord#setSourceMethodName ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: sis   文件: NamedElement.java
/**
 * 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 ));
}
 
源代码3 项目: HttpSessionReplacer   文件: JDK14LoggerAdapter.java
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;
}
 
源代码4 项目: sis   文件: Registration.java
/**
 * 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);
}
 
源代码5 项目: sis   文件: MetadataSource.java
/**
 * 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);
    }
}
 
源代码6 项目: knox   文件: JdkMessageLogger.java
@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 );
}
 
源代码7 项目: turbo-rpc   文件: LogFactory.java
@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;
}
 
源代码8 项目: jdk8u60   文件: LogWrapperBase.java
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);
    }
}
 
源代码10 项目: cxf   文件: AbstractDelegatingLogger.java
@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);
    }
}
 
源代码11 项目: tomee   文件: AbstractDelegatingLogger.java
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);
    }
}
 
源代码12 项目: mongodb-async-driver   文件: JulLog.java
/**
 * {@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);
    }
}
 
源代码13 项目: grpc-java   文件: ChannelTracer.java
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);
  }
}
 
源代码14 项目: tomee   文件: AbstractDelegatingLogger.java
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);
    }
}
 
源代码16 项目: sis   文件: Analyzer.java
/**
 * 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();
}
 
源代码17 项目: tomee   文件: AbstractDelegatingLogger.java
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);
    }
}
 
源代码18 项目: openjdk-8   文件: LogWrapperBase.java
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());
    }
}
 
源代码19 项目: snowflake-jdbc   文件: SFFormatterTest.java
/**
 * 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;
}
 
源代码20 项目: vespa   文件: OsgiLogHandlerTestCase.java
@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"));
}