install

@MustBeClosed()
public abstract ScopedLoggingContext.LoggingContextCloseable install()

Installs a new context based on the state of the builder. The caller is required to invoke close() on the returned instances in the reverse order to which they were obtained. For JDK 1.7 and above, this is best achieved by using a try-with-resources construction in the calling code.


try (LoggingContextCloseable ctx = ScopedLoggingContext.getInstance()
    .newContext().withTags(Tags.of("my_tag", someValue).install()) {
  // Logging by code called from within this context will contain the additional metadata.
  logger.atInfo().log("Log message should contain tag value...");
}

To avoid the need to manage contexts manually, it is strongly recommended that the helper methods, such as wrap or run are used to simplify the handling of contexts. This method is intended primarily to be overridden by context implementations rather than being invoked as a normal part of context use.

An implementation of scoped contexts must preserve any existing metadata when a context is opened, and restore the previous state when it terminates.

Note that the returned LoggingContextCloseable is not required to enforce the correct closure of nested contexts, and while it is permitted to throw a in the face of mismatched or invalid usage, it is not required.