Java Code Examples for java.util.logging.Filter#isLoggable()

The following examples show how to use java.util.logging.Filter#isLoggable() . 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: AbstractBackend.java    From flogger with Apache License 2.0 5 votes vote down vote up
void log(LogRecord record, boolean wasForced) {
  // Do the fast boolean check (which normally succeeds) before calling isLoggable().
  if (!wasForced || logger.isLoggable(record.getLevel())) {
    // Unforced log statements or forced log statements at or above the logger's level can be
    // passed to the normal log(LogRecord) method.
    logger.log(record);
  } else {
    // If logging has been forced for a log record which would otherwise be discarded, we cannot
    // call our logger's log(LogRecord) method, so we must simulate its behavior in one of two
    // ways.
    // 1: Simulate the effect of calling the log(LogRecord) method directly (this is safe if the
    //    logger provided by the log manager was a normal Logger instance).
    // 2: Obtain a "child" logger from the log manager which is set to log everything, and call
    //    its log(LogRecord) method instead (which should have the same overridden behavior and
    //    will still publish log records to our logger's handlers).
    //
    // In all cases we still call the filter (if one exists) even though we ignore the result.
    // Use a local variable to avoid race conditions where the filter can be unset at any time.
    Filter filter = logger.getFilter();
    if (filter != null) {
      filter.isLoggable(record);
    }
    if (logger.getClass() == Logger.class || cannotUseForcingLogger) {
      // If the Logger instance is not a subclass, its log(LogRecord) method cannot have been
      // overridden. That means it's safe to just publish the log record directly to handlers
      // to avoid the loggability check, which would otherwise discard the forced log record.
      publish(logger, record);
    } else {
      // Hopefully rare situation in which the logger is subclassed _and_ the log record would
      // normally be discarded based on its level.
      forceLoggingViaChildLogger(record);
    }
  }
}
 
Example 2
Source File: LoggerFilter.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean isLoggable(final @Nullable LogRecord record) {
	if (oldFilter != null && !oldFilter.isLoggable(record))
		return false;
	for (final Filter f : filters)
		if (!f.isLoggable(record))
			return false;
	return true;
}
 
Example 3
Source File: LogHandlerBase.java    From baratine with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Publishes the record.
 */
@Override
public final void publish(LogRecord record)
{
  if (! isLoggable(record)) {
    return;
  }

  Filter filter = getFilter();

  if (filter != null && ! filter.isLoggable(record))
    return;

  if (record == null) {
    System.out.println(this + ": no record");
    return;
  }

  //synchronized (this) {
    processPublish(record);
    //processFlush();
  //}

  /*
  _logQueue.offer(record);

  if (CurrentTime.isTest()) {
    waitForEmpty();
  }
  */
}
 
Example 4
Source File: AbstractDelegatingLogger.java    From openwebbeans-meecrowave with Apache License 2.0 5 votes vote down vote up
protected void internalLog(LogRecord record) {
    Filter filter = getFilter();
    if (filter != null && !filter.isLoggable(record)) {
        return;
    }
    String msg = formatMessage(record);
    internalLogFormatted(msg, record);
}
 
Example 5
Source File: EngineLogger.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
private void publishToLogger( Logger logger, LogRecord record )
{
	if ( !logger.isLoggable( record.getLevel( ) ) )
	{
		return;
	}
	synchronized ( logger )
	{
		Filter filter = logger.getFilter( );
		if ( filter != null && !filter.isLoggable( record ) )
		{
			return;
		}
	}
	// Post the LogRecord to all our Handlers, and then to
	// our parents' handlers, all the way up the tree.

	while ( logger != null )
	{
		Handler targets[] = logger.getHandlers( );

		if ( targets != null )
		{
			for ( int i = 0; i < targets.length; i++ )
			{
				targets[i].publish( record );
			}
		}

		if ( !logger.getUseParentHandlers( ) )
		{
			break;
		}

		logger = logger.getParent( );
	}
}
 
Example 6
Source File: AbstractDelegatingLogger.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected void internalLog(LogRecord record) {
    Filter filter = getFilter();
    if (filter != null && !filter.isLoggable(record)) {
        return;
    }
    String msg = formatMessage(record);
    internalLogFormatted(msg, record);
}
 
Example 7
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 8
Source File: AbstractDelegatingLogger.java    From tomee with Apache License 2.0 5 votes vote down vote up
protected void internalLog(final LogRecord record) {
    final Filter filter = getFilter();
    if (filter != null && !filter.isLoggable(record)) {
        return;
    }
    final String msg = formatMessage(record);
    internalLogFormatted(msg, record);
}
 
Example 9
Source File: CRS.java    From sis with Apache License 2.0 4 votes vote down vote up
/**
 * Replaces the given coordinate reference system by an authoritative description, if one can be found.
 * This method can be invoked after constructing a CRS in a context where the EPSG (or other authority)
 * code is suspected more reliable than the rest of the description. A common case is a <cite>Well Known
 * Text</cite> (WKT) string declaring wrong projection method or parameter values for the EPSG code that
 * it pretends to describe. For example:
 *
 * <blockquote>
 *   {@code PROJCS["WGS 84 / Pseudo-Mercator",}<br>
 *   {@code   }(…base CRS omitted for brevity…)<br>
 *   {@code   PROJECTION["Mercator (variant A)"],} — <em><b>wrong:</b> shall be "Popular Visualisation Pseudo Mercator"</em><br>
 *   {@code   }(…parameters and axes omitted for brevity…)<br>
 *   {@code   AUTHORITY["EPSG", "3857"]]}
 * </blockquote>
 *
 * In such cases, Apache SIS behavior in {@link #fromWKT(String)}, {@link #fromXML(String)} and other methods is
 * conform to the <a href="http://docs.opengeospatial.org/is/12-063r5/12-063r5.html">ISO 19162 specification</a>:
 *
 * <blockquote><cite>"Should any attributes or values given in the cited identifier be in conflict with attributes
 * or values given explicitly in the WKT description, the WKT values shall prevail."</cite></blockquote>
 *
 * In situations where the opposite behavior is desired (i.e. to make the authority identifier prevails),
 * this method can be invoked. This method performs the following actions:
 *
 * <ul>
 *   <li>If the given CRS has an {@linkplain AbstractIdentifiedObject#getIdentifiers() identifier} and if the authority factory can
 *     {@linkplain org.apache.sis.referencing.factory.GeodeticAuthorityFactory#createCoordinateReferenceSystem(String) create a CRS}
 *     for that identifier, then:
 *     <ul>
 *       <li>If the CRS defined by the authority is {@linkplain Utilities#equalsIgnoreMetadata equal, ignoring metadata},
 *         to the given CRS, then this method returns silently the <em>authoritative</em> CRS.</li>
 *       <li>Otherwise if the CRS defined by the authority is equal, ignoring axis order and units, to the given CRS,
 *         then this method returns a <em>new</em> CRS derived from the authoritative one but with same
 *         {@linkplain org.apache.sis.referencing.cs.AxesConvention axes convention} than the given CRS.
 *         A warning is emitted.</li>
 *       <li>Otherwise this method discards the given CRS and returns the <em>authoritative</em> CRS.
 *         A warning is emitted with a message indicating where a difference has been found.</li>
 *     </ul>
 *   </li>
 *   <li>Otherwise if the given CRS does not have identifier, then this method
 *       {@linkplain org.apache.sis.referencing.factory.IdentifiedObjectFinder searches for an equivalent CRS}
 *       defined by the authority factory. If such CRS is found, then:
 *     <ul>
 *       <li>If the CRS defined by the authority is {@linkplain Utilities#equalsIgnoreMetadata equal, ignoring metadata},
 *         to the given CRS, then this method returns silently the <em>authoritative</em> CRS.</li>
 *       <li>Otherwise if the CRS defined by the authority is equal, ignoring axis order and units, to the given CRS,
 *         then this method returns silently a <em>new</em> CRS derived from the authoritative one but with same
 *         {@linkplain org.apache.sis.referencing.cs.AxesConvention axes convention} than the given CRS.</li>
 *     </ul>
 *   </li>
 *   <li>Otherwise this method silently returns the given CRS as-is.</li>
 * </ul>
 *
 * <h4>Avoiding warning redundancies</h4>
 * The warnings logged by this method are redundant with warnings logged by other methods in this class,
 * in particular {@link #fromWKT(String)} and {@link #fromXML(String)} methods. For avoiding this annoyance,
 * a {@code null} value for the {@code warningFilter} argument means to shut off those redundant loggings.
 * A non-null {@code warningFilter} argument is more useful for CRS parsed by methods outside this class,
 * for example {@link org.apache.sis.io.wkt.WKTFormat} or {@link org.apache.sis.xml.XML#unmarshal(String)}.
 *
 * @param  crs            the CRS to replace by an authoritative CRS, or {@code null}.
 * @param  factory        the factory where to search for authoritative definitions, or {@code null} for the default.
 * @param  warningFilter  whether to log warnings, or {@code null} for the default behavior (which is to filter out
 *                        the warnings that are redundant with warnings emitted by other methods in this class).
 * @return the suggested CRS to use (may be the {@code crs} argument itself), or {@code null} if the given CRS was null.
 * @throws FactoryException if an error occurred while querying the authority factory.
 *
 * @since 1.0
 */
public static CoordinateReferenceSystem fromAuthority(CoordinateReferenceSystem crs,
        final CRSAuthorityFactory factory, final Filter warningFilter) throws FactoryException
{
    if (crs != null) {
        final DefinitionVerifier verification = DefinitionVerifier.withAuthority(crs, factory, true);
        if (verification != null) {
            crs = verification.authoritative;
            if (warningFilter != null) {
                final LogRecord record = verification.warning(false);
                if (record != null) {
                    record.setLoggerName(Modules.REFERENCING);
                    record.setSourceClassName(CRS.class.getName());
                    record.setSourceMethodName("fromAuthority");
                    if (warningFilter.isLoggable(record)) {
                        Logging.getLogger(Modules.REFERENCING).log(record);
                    }
                }
            }
        }
    }
    return crs;
}
 
Example 10
Source File: LoggerAdapter.java    From sis with Apache License 2.0 4 votes vote down vote up
/**
 * Logs a record. The default implementation delegates to one of the
 * {@link #logrb(Level,String,String,ResourceBundle,String,Object[]) logrb} or
 * {@link #logp(Level,String,String,String)} methods.
 *
 * @param record  the log record to be published.
 */
@Override
public void log(final LogRecord record) {
    /*
     * The filter should always be null since we overrode the 'setFilter' method as a no-op.
     * But we check it anyway as matter of principle just in case some subclass overrides the
     * 'getFilter()' method. This is the only method where we can do this check cheaply. Note
     * that this is NOT the check for logging level; Filters are for user-specified criterions.
     */
    final Filter filter = getFilter();
    if (filter != null && !filter.isLoggable(record)) {
        return;
    }
    final Level     level        = record.getLevel();
    final String    sourceClass  = record.getSourceClassName();
    final String    sourceMethod = record.getSourceMethodName();
    final String    message      = record.getMessage();
    final Object[]  params       = record.getParameters();
    final Throwable thrown       = record.getThrown();
    final ResourceBundle bundle  = record.getResourceBundle();
    final boolean useThrown = (thrown != null) && (params == null || params.length == 0);
    if (bundle != null) {
        if (useThrown) {
            logrb(level, sourceClass, sourceMethod, bundle, message, thrown);
        } else {
            logrb(level, sourceClass, sourceMethod, bundle, message, params);
        }
    } else {
        final String bundleName = record.getResourceBundleName();
        if (bundleName != null) {
            if (useThrown) {
                logrb(level, sourceClass, sourceMethod, bundleName, message, thrown);
            } else {
                logrb(level, sourceClass, sourceMethod, bundleName, message, params);
            }
        } else {
            if (useThrown) {
                logp(level, sourceClass, sourceMethod, message, thrown);
            } else {
                logp(level, sourceClass, sourceMethod, message, params);
            }
        }
    }
}
 
Example 11
Source File: ApiLogger.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
boolean isFiltered(final LogRecord logRecord) {
    final Filter filter = getFilter();
    return filter != null && !filter.isLoggable(logRecord);
}