# Class Logging

Object
Logging

public final class Logging extends Object
A set of utilities method for configuring loggings in SIS. This class provides also some convenience static methods, including:

- invalid reference

      #log(Class, String, LogRecord)

  for setting the logger name, source class name and source method name of the given record before to log it.
- `unexpectedException(Logger, Class, String, Throwable)` for reporting an anomalous but nevertheless non-fatal exception.

Since:
  0.3

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static void`
  `completeAndLog(Logger logger, Class<?> classe, String method, LogRecord record)`
  Completes the properties of the given record, then logs to the specified or inferred logger.
  `static Logger`
  `getLogger(Class<?> source)`
  Returns a logger for the package of the specified class.
  `static boolean`
  `ignorableException(Logger logger, Class<?> classe, String method, Throwable error)`
  Invoked when an ignorable error occurred.
  `static boolean`
  `recoverableException(Logger logger, Class<?> classe, String method, Throwable error)`
  Invoked when a recoverable error occurred.
  `static boolean`
  `severeException(Logger logger, Class<?> classe, String method, Throwable error)`
  Invoked when a severe error occurred.
  `static boolean`
  `unexpectedException(Logger logger, Class<?> classe, String method, Throwable error)`
  Invoked when an unexpected error occurred.

  ### Methods inherited from class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Method Details

  - ### getLogger

    public static Logger getLogger(Class<?> source)
    Returns a logger for the package of the specified class. This convenience method invokes `Logger​.getLogger(String)` with the package name of the given class taken as the logger name.
    Parameters:
      `source` - the class which will emit a logging message.
    Returns:
      a logger for the specified class.
    Since:
      1.0

  - ### completeAndLog

    public static void completeAndLog(Logger logger, Class<?> classe, String method, LogRecord record)
    Completes the properties of the given record, then logs to the specified or inferred logger. If the `logger` argument is `null`, the logger is inferred from the other arguments. This convenience method performs the following steps:
    - Set the source class name to the canonical name of the given class.
    - Set the source method name to the given value.
    - Set the logger name to the name of the given logger if non null. Otherwise use the package name of the class.
    - Log the modified record to the specified `logger` if non null, or to the logger specified by the logger name otherwise.
    Parameters:
      `logger` - the logger where to log, or `null` for inferring from the logger or package name.
      `classe` - the class to report as the source of the logging message.
      `method` - the method to report as the source of the logging message.
      `record` - the record to complete and log.
    Since:
      1.4

  - ### unexpectedException

    public static boolean unexpectedException(Logger logger, Class<?> classe, String method, Throwable error)
    Invoked when an unexpected error occurred. This method logs a message at `Level​.WARNING` to the specified logger. The originating class name and method name can optionally be specified. If any of them is `null`, then it will be inferred from the error stack trace as described below. If the `classe` or `method` arguments are null, then the originating class name and method name are inferred from the given `error` using the first stack trace element for which the class name is inside a package or sub-package of the same name as the logger name.

    #### Recommended usage

    Explicit value for class and method names are preferred to automatic inference for the following reasons:
    - Automatic inference is not 100% reliable, since the Java Virtual Machine is free to omit stack frame in optimized code.
    - When an exception occurred in a private method used internally by a public method, we sometimes want to log the warning for the public method instead, since the user is not expected to know anything about the existence of the private method. If a developer really want to know about the private method, the stack trace is still available anyway.

    #### Example

    If the logger name is `"org​.apache​.sis​.image"`, then this method will uses the first stack trace element where the fully qualified class name starts with `"org​.apache​.sis​.image"` or `"org​.apache​.sis​.image​.io"`, but not `"org​.apache​.sis​.imageio"`.
    Parameters:
      `logger` - where to log the error, or `null` for inferring a default value from other arguments.
      `classe` - the class where the error occurred, or `null` for inferring a default value from other arguments.
      `method` - the method where the error occurred, or `null` for inferring a default value from other arguments.
      `error` - the error, or `null` if none.
    Returns:
      `true` if the error has been logged, or `false` if the given `error` was null or if the logger does not log anything at `Level​.WARNING`.
    See Also:
      - `recoverableException(Logger, Class, String, Throwable)`
      - `severeException(Logger, Class, String, Throwable)`

  - ### recoverableException

    public static boolean recoverableException(Logger logger, Class<?> classe, String method, Throwable error)
    Invoked when a recoverable error occurred. This method is similar to `unexpectedException(…)` except that it does not log the stack trace and uses a lower logging level.
    Parameters:
      `logger` - where to log the error, or `null` for inferring a default value from other arguments.
      `classe` - the class where the error occurred, or `null` for inferring a default value from other arguments.
      `method` - the method name where the error occurred, or `null` for inferring a default value from other arguments.
      `error` - the error, or `null` if none.
    Returns:
      `true` if the error has been logged, or `false` if the given `error` was null or if the logger does not log anything at `Level​.FINE`.
    See Also:
      - `unexpectedException(Logger, Class, String, Throwable)`
      - `severeException(Logger, Class, String, Throwable)`

  - ### ignorableException

    public static boolean ignorableException(Logger logger, Class<?> classe, String method, Throwable error)
    Invoked when an ignorable error occurred. This method is similar to `unexpectedException(…)` except that it uses a lower logging level.
    Parameters:
      `logger` - where to log the error, or `null` for inferring a default value from other arguments.
      `classe` - the class where the error occurred, or `null` for inferring a default value from other arguments.
      `method` - the method name where the error occurred, or `null` for inferring a default value from other arguments.
      `error` - the error, or `null` if none.
    Returns:
      `true` if the error has been logged, or `false` if the given `error` was null or if the logger does not log anything at `Level​.FINER`.
    Since:
      1.0

  - ### severeException

    public static boolean severeException(Logger logger, Class<?> classe, String method, Throwable error)
    Invoked when a severe error occurred. This method is similar to `unexpectedException` except that it logs the message at the `SEVERE` level.
    Parameters:
      `logger` - where to log the error, or `null` for inferring a default value from other arguments.
      `classe` - the class where the error occurred, or `null` for inferring a default value from other arguments.
      `method` - the method name where the error occurred, or `null` for inferring a default value from other arguments.
      `error` - the error, or `null` if none.
    Returns:
      `true` if the error has been logged, or `false` if the given `error` was null or if the logger does not log anything at `Level​.SEVERE`.
    See Also:
      - `unexpectedException(Logger, Class, String, Throwable)`
      - `recoverableException(Logger, Class, String, Throwable)`