Package org.jboss.logmanager

# Class LogContext

java.lang.Object
org.jboss.logmanager.LogContext

All Implemented Interfaces:
  `AutoCloseable`, `Protectable`

public final class LogContext extends Object implements Protectable, AutoCloseable
A logging context, for producing isolated logging environments.

## Field Summary

Fields
Modifier and Type
Field
Description
`static final LogContextSelector`
`DEFAULT_LOG_CONTEXT_SELECTOR`
The default log context selector, which always returns the system log context.

## Method Summary

Modifier and Type
Method
Description
`void`
`addCloseHandler(AutoCloseable closeHandler)`
Adds a handler invoked during the close of this log context.
`void`
`close()`
`static LogContext`
`create()`
Create a new log context.
`static LogContext`
`create(boolean strong)`
Create a new log context.
`protected void`
`decrementRef(String name)`
`void`
`disableAccess()`
Disable previous access to the object for modifications on the current thread.
`void`
`enableAccess(Object protectKey)`
Enable access to the object for modifications on the current thread.
`<V> V`
`getAttachment(String loggerName, Logger.AttachmentKey<V> key)`
Get a logger attachment for a logger name, if it exists.
`Set<AutoCloseable>`
`getCloseHandlers()`
Gets the current close handlers associated with this log context.
`Level`
`getLevelForName(String name)`
Get the level for a name.
`static LogContext`
`getLogContext()`
Get the currently active log context.
`static LogContextSelector`
`getLogContextSelector()`
Returns the currently set log context selector.
`Logger`
`getLogger(String name)`
Get a logger with the given name from this logging context.
`Logger`
`getLoggerIfExists(String name)`
Get a logger with the given name from this logging context, if a logger node exists at that location.
`Enumeration<String>`
`getLoggerNames()`
Returns an enumeration of the logger names that have been created.
`LoggingMXBean`
`getLoggingMXBean()`
Get the `LoggingMXBean` associated with this log context.
`static LogContext`
`getSystemLogContext()`
Get the system log context.
`protected void`
`incrementRef(String name)`
`void`
`protect(Object protectionKey)`
Protect this object from modifications.
`void`
`registerLevel(Level level)`
Register a level instance with this log context.
`void`
`setCloseHandlers(Collection<AutoCloseable> closeHandlers)`
Clears any current close handlers associated with log context, then adds the handlers to be invoked during the close of this log context.
`static void`
`setLogContextSelector(LogContextSelector newSelector)`
Set a new log context selector.
`void`
`unprotect(Object protectionKey)`
Allows the object to be modified if the `protectionKey` matches the key used to `protect` the object.
`void`
`unregisterLevel(Level level)`
Unregister a previously registered level.

### Methods inherited from class java.lang.Object

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

## Field Details

### DEFAULT_LOG_CONTEXT_SELECTOR

public static final LogContextSelector DEFAULT_LOG_CONTEXT_SELECTOR
The default log context selector, which always returns the system log context.

## Method Details

### create

public static LogContext create(boolean strong)
Create a new log context. If a security manager is installed, the caller must have the `"createLogContext"` `RuntimePermission` to invoke this method.

Parameters:
  `strong` - `true` if the context should use strong references, `false` to use (default) weak references for automatic logger GC
Returns:
  a new log context

### create

public static LogContext create()
Create a new log context. If a security manager is installed, the caller must have the `"createLogContext"` `RuntimePermission` to invoke this method.

Returns:
  a new log context

### getLogger

public Logger getLogger(String name)
Get a logger with the given name from this logging context.

Parameters:
  `name` - the logger name
Returns:
  the logger instance
See Also:
  - `LogManager.getLogger(String)`

### getLoggerIfExists

public Logger getLoggerIfExists(String name)
Get a logger with the given name from this logging context, if a logger node exists at that location.

Parameters:
  `name` - the logger name
Returns:
  the logger instance, or `null` if no such logger node exists

### getAttachment

public <V> V getAttachment(String loggerName, Logger.AttachmentKey<V> key)
Get a logger attachment for a logger name, if it exists.

Type Parameters:
  `V` - the attachment value type
Parameters:
  `loggerName` - the logger name
  `key` - the attachment key
Returns:
  the attachment or `null` if the logger or the attachment does not exist

### getLoggingMXBean

public LoggingMXBean getLoggingMXBean()
Get the `LoggingMXBean` associated with this log context.

Returns:
  the `LoggingMXBean` instance

### getLevelForName

public Level getLevelForName(String name) throws IllegalArgumentException
Get the level for a name.

Parameters:
  `name` - the name
Returns:
  the level
Throws:
  `IllegalArgumentException` - if the name is not known

### registerLevel

public void registerLevel(Level level)
Register a level instance with this log context. The level can then be looked up by name. Only a weak reference to the level instance will be kept. Any previous level registration for the given level's name will be overwritten.

Parameters:
  `level` - the level to register

### unregisterLevel

public void unregisterLevel(Level level)
Unregister a previously registered level. Log levels that are not registered may still be used, they just will not be findable by name.

Parameters:
  `level` - the level to unregister

### getSystemLogContext

public static LogContext getSystemLogContext()
Get the system log context.

Returns:
  the system log context

### getLogContext

public static LogContext getLogContext()
Get the currently active log context.

Returns:
  the currently active log context

### setLogContextSelector

public static void setLogContextSelector(LogContextSelector newSelector)
Set a new log context selector. If a security manager is installed, the caller must have the `"setLogContextSelector"` `RuntimePermission` to invoke this method.

Parameters:
  `newSelector` - the new selector.

### getLogContextSelector

public static LogContextSelector getLogContextSelector()
Returns the currently set log context selector.

Returns:
  the log context selector

### protect

public void protect(Object protectionKey) throws SecurityException
Description copied from interface: `Protectable`
Protect this object from modifications.

Specified by:
  `protect` in interface `Protectable`
Parameters:
  `protectionKey` - the key used to protect the object.
Throws:
  `SecurityException` - if the object is already protected.

### unprotect

public void unprotect(Object protectionKey) throws SecurityException
Description copied from interface: `Protectable`
Allows the object to be modified if the `protectionKey` matches the key used to `protect` the object.

Specified by:
  `unprotect` in interface `Protectable`
Parameters:
  `protectionKey` - the key used to protect the object.
Throws:
  `SecurityException` - if the object is protected and the key doesn't match.

### enableAccess

public void enableAccess(Object protectKey) throws SecurityException
Description copied from interface: `Protectable`
Enable access to the object for modifications on the current thread.

Specified by:
  `enableAccess` in interface `Protectable`
Parameters:
  `protectKey` - the key used to `protect` modifications.
Throws:
  `SecurityException`

### disableAccess

public void disableAccess()
Description copied from interface: `Protectable`
Disable previous access to the object for modifications on the current thread.

Specified by:
  `disableAccess` in interface `Protectable`

### close

public void close() throws Exception

Specified by:
  `close` in interface `AutoCloseable`
Throws:
  `Exception`

### getLoggerNames

public Enumeration<String> getLoggerNames()
Returns an enumeration of the logger names that have been created. This does not return names of loggers that may have been garbage collected. Logger names added after the enumeration has been retrieved may also be added to the enumeration.

Returns:
  an enumeration of the logger names
See Also:
  - `LogManager.getLoggerNames()`

### addCloseHandler

public void addCloseHandler(AutoCloseable closeHandler)
Adds a handler invoked during the close of this log context. The close handlers will be invoked in the order they are added.

The loggers associated with this context will always be closed.

Parameters:
  `closeHandler` - the close handler to use

### getCloseHandlers

public Set<AutoCloseable> getCloseHandlers()
Gets the current close handlers associated with this log context.

Returns:
  the current close handlers

### setCloseHandlers

public void setCloseHandlers(Collection<AutoCloseable> closeHandlers)
Clears any current close handlers associated with log context, then adds the handlers to be invoked during the close of this log context. The close handlers will be invoked in the order they are added.

The loggers associated with this context will always be closed.

Parameters:
  `closeHandlers` - the close handlers to use

### incrementRef

protected void incrementRef(String name)

### decrementRef

protected void decrementRef(String name)