Java Code Examples for java.util.logging.LogRecord#setSourceMethodName()

The following examples show how to use java.util.logging.LogRecord#setSourceMethodName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NamedElement.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 2
Source File: JDK14LoggerAdapter.java    From HttpSessionReplacer with MIT License 6 votes vote down vote up
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;
}
 
Example 3
Source File: RoboconfLogFormatterTest.java    From roboconf-platform with Apache License 2.0 6 votes vote down vote up
@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 ));
}
 
Example 4
Source File: JulLog.java    From mongodb-async-driver with Apache License 2.0 5 votes vote down vote up
/**
 * {@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);
    }
}
 
Example 5
Source File: SFFormatterTest.java    From snowflake-jdbc with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 6
Source File: LogWrapperBase.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
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());
    }
}
 
Example 7
Source File: AbstractDelegatingLogger.java    From tomee with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 8
Source File: Analyzer.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
}
 
Example 9
Source File: AbstractDelegatingLogger.java    From openwebbeans-meecrowave with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 10
Source File: AbstractDelegatingLogger.java    From tomee with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 11
Source File: ChannelTracer.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
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);
  }
}
 
Example 12
Source File: Registration.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 13
Source File: AbstractDelegatingLogger.java    From tomee with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 14
Source File: AbstractDelegatingLogger.java    From cxf with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 15
Source File: AbstractDelegatingLogger.java    From openwebbeans-meecrowave with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 16
Source File: LogWrapperBase.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
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());
    }
}
 
Example 17
Source File: LogFactory.java    From turbo-rpc with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 18
Source File: JdkMessageLogger.java    From knox with Apache License 2.0 5 votes vote down vote up
@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 );
}
 
Example 19
Source File: MetadataSource.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example 20
Source File: OsgiLogHandlerTestCase.java    From vespa with Apache License 2.0 4 votes vote down vote up
@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"));
}