Top Description Inners Fields Constructors Methods
org.jboss.logmanager

public final Class LogContext

extends Object
implements AutoCloseable
Class Inheritance
All Implemented Interfaces
java.lang.AutoCloseable
Annotations
@SuppressWarnings:unused, WeakerAccess
Imports
java.lang.invoke.ConstantBootstraps, .MethodHandles, .VarHandle, java.security.AccessController, .Permission, .PrivilegedAction, java.util.Collection, .Enumeration, .HashMap, .Iterator, .LinkedHashSet, .Map, .ServiceLoader, .Set, java.util.concurrent.ConcurrentHashMap, .ConcurrentMap, java.util.concurrent.atomic.AtomicReference, java.util.concurrent.locks.ReentrantLock, java.util.logging.Level, .LoggingPermission, io.smallrye.common.constraint.Assert, io.smallrye.common.ref.Reference, .References

A logging context, for producing isolated logging environments.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
LogContext.LazyHolder

This lazy holder class is required to prevent a problem due to a LogContext instance being constructed before the class init is complete.

Field Summary

Modifier and TypeField and Description
private static final VarHandle
private volatile Map<Logger.AttachmentKey<?>, Object>
private final Set<AutoCloseable>
pack-priv static final Permission
pack-priv static final Permission
public static final LogContextSelector
DEFAULT_LOG_CONTEXT_SELECTOR

The default log context selector, which always returns the system log context.

private final LogContextInitializer
private final AtomicReference<Map<String, Reference<Level, Void>>>
private static volatile LogContextSelector
private final Set<LoggerNode>
private final LoggerNode
pack-priv static final Permission
private final boolean
private static final LogContext
pack-priv final ReentrantLock
treeLock

This lock is taken any time a change is made which affects multiple nodes in the hierarchy.

Constructor Summary

AccessConstructor and Description
pack-priv
LogContext(final boolean strong, LogContextInitializer initializer)

Method Summary

Modifier and TypeMethod and Description
private static SecurityException
public void
addCloseHandler(final AutoCloseable
the close handler to use
closeHandler
)

Adds a handler invoked during the close of this log context.

public <
the attachment value type
V
>
V

Returns:

the old attachment, if there was one
attach
(Logger.AttachmentKey<V>
the attachment key
key
,
V
the attachment value
value
)

Attach an object to this log context under a given key.

public <
the attachment value type
V
>
V

Returns:

the current attachment, if there is one, or null if the value was successfully attached
attachIfAbsent
(Logger.AttachmentKey<V>
the attachment key
key
,
V
the attachment value
value
)

Attach an object to this log context under a given key, if such an attachment does not already exist.

pack-priv static void
public void
close()

Implements java.lang.AutoCloseable.close.

Closes this resource, relinquishing any underlying resources.

public static LogContext

Returns:

a new log context
create
(boolean
true if the context should use strong references, false to use (default) weak references for automatic logger GC
strong
)

Create a new log context.

public static LogContext

Returns:

a new log context
create
(boolean
true if the context should use strong references, false to use (default) weak references for automatic logger GC
strong
,
LogContextInitializer
the log context initializer to use (must not be null)
initializer
)

Create a new log context.

public static LogContext

Returns:

a new log context
create
()

Create a new log context.

public static LogContext

Returns:

a new log context
create
(LogContextInitializer
the log context initializer to use (must not be null)
initializer
)

Create a new log context.

pack-priv ConcurrentMap<String, LoggerNode>
public <
the attachment value type
V
>
V

Returns:

the old value, or null if there was none
detach
(Logger.AttachmentKey<V>
the attachment key
key
)

Remove an attachment.

private static LogContextInitializer
private static LogContextInitializer
public <
the attachment value type
V
>
V

Returns:

the attachment, or null if there is none for this key
getAttachment
(Logger.AttachmentKey<V>
the key
key
)

Get the attachment value for a given key, or null if there is no such attachment.

public <
the attachment value type
V
>
V

Returns:

the attachment or null if the logger or the attachment does not exist
getAttachment
(String
the logger name
loggerName
,
Logger.AttachmentKey<V>
the attachment key
key
)

Get a logger attachment for a logger name, if it exists.

public Set<AutoCloseable>

Returns:

the current close handlers
getCloseHandlers
()

Gets the current close handlers associated with this log context.

pack-priv LogContextInitializer
public Level

Returns:

the level
getLevelForName
(String
the name
name
)

Get the level for a name.

public static LogContext

Returns:

the currently active log context
getLogContext
()

Get the currently active log context.

public static LogContextSelector

Returns:

the log context selector
getLogContextSelector
()

Returns the currently set log context selector.

public Logger

Returns:

the logger instance
getLogger
(String
the logger name
name
)

Get a logger with the given name from this logging context.

public Logger

Returns:

the logger instance, or null if no such logger node exists
getLoggerIfExists
(String
the logger name
name
)

Get a logger with the given name from this logging context, if a logger node exists at that location.

public Enumeration<String>

Returns:

an enumeration of the logger names
getLoggerNames
()

Returns an enumeration of the logger names that have been created.

pack-priv LoggerNode
public static LogContext

Returns:

the system log context
getSystemLogContext
()

Get the system log context.

pack-priv boolean
private void
recursivelyClose(final LoggerNode loggerNode)

public void
registerLevel(Level
the level to register
level
)

Register a level instance with this log context.

public void
registerLevel(Level
the level to register
level
,
boolean
true to strongly reference the level, or false to weakly reference it
strong
)

Register a level instance with this log context.

public void
setCloseHandlers(final Collection<AutoCloseable>
the close handlers to use
closeHandlers
)

Clears any current close handlers associated with log context, then adds the handlers to be invoked during the close of this log context.

public static void
setLogContextSelector(LogContextSelector
the new selector.
newSelector
)

Set a new log context selector.

pack-priv boolean
public void
unregisterLevel(Level
the level to unregister
level
)

Unregister a previously registered level.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

attachmentHandleback to summary
private static final VarHandle attachmentHandle
attachmentsback to summary
private volatile Map<Logger.AttachmentKey<?>, Object> attachments
closeHandlersback to summary
private final Set<AutoCloseable> closeHandlers
CONTROL_PERMISSIONback to summary
pack-priv static final Permission CONTROL_PERMISSION
CREATE_CONTEXT_PERMISSIONback to summary
pack-priv static final Permission CREATE_CONTEXT_PERMISSION
DEFAULT_LOG_CONTEXT_SELECTORback to summary
public static final LogContextSelector DEFAULT_LOG_CONTEXT_SELECTOR

The default log context selector, which always returns the system log context.

initializerback to summary
private final LogContextInitializer initializer
levelMapReferenceback to summary
private final AtomicReference<Map<String, Reference<Level, Void>>> levelMapReference
logContextSelectorback to summary
private static volatile LogContextSelector logContextSelector
pinnedSetback to summary
private final Set<LoggerNode> pinnedSet
rootLoggerback to summary
private final LoggerNode rootLogger
SET_CONTEXT_SELECTOR_PERMISSIONback to summary
pack-priv static final Permission SET_CONTEXT_SELECTOR_PERMISSION
strongback to summary
private final boolean strong
SYSTEM_CONTEXTback to summary
private static final LogContext SYSTEM_CONTEXT
treeLockback to summary
pack-priv final ReentrantLock treeLock

This lock is taken any time a change is made which affects multiple nodes in the hierarchy.

Constructor Detail

LogContextback to summary
pack-priv LogContext(final boolean strong, LogContextInitializer initializer)

Method Detail

accessDeniedback to summary
private static SecurityException accessDenied()
addCloseHandlerback to summary
public void addCloseHandler(final 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:AutoCloseable

the close handler to use

attachback to summary
public <V> V attach(Logger.AttachmentKey<V> key, V value) throws SecurityException

Attach an object to this log context under a given key. A strong reference is maintained to the key and value for as long as this log context exists. Log context attachments are placed on the root logger and can also be accessed there.

Parameters
<V>
the attachment value type
key:Logger.AttachmentKey<V>

the attachment key

value:V

the attachment value

Returns:V

the old attachment, if there was one

Annotations
@SuppressWarnings:unchecked
Exceptions
SecurityException:
if a security manager exists and if the caller does not have LoggingPermission(control)
IllegalArgumentException:
if the attachment cannot be added because the maximum has been reached
attachIfAbsentback to summary
public <V> V attachIfAbsent(Logger.AttachmentKey<V> key, V value) throws SecurityException

Attach an object to this log context under a given key, if such an attachment does not already exist. A strong reference is maintained to the key and value for as long as this log context exists. Log context attachments are placed on the root logger and can also be accessed there.

Parameters
<V>
the attachment value type
key:Logger.AttachmentKey<V>

the attachment key

value:V

the attachment value

Returns:V

the current attachment, if there is one, or null if the value was successfully attached

Annotations
@SuppressWarnings:unchecked
Exceptions
SecurityException:
if a security manager exists and if the caller does not have LoggingPermission(control)
IllegalArgumentException:
if the attachment cannot be added because the maximum has been reached
checkAccessback to summary
pack-priv static void checkAccess()
closeback to summary
public void close() throws Exception

Implements java.lang.AutoCloseable.close.

Doc from java.lang.AutoCloseable.close.

Closes this resource, relinquishing any underlying resources. This method is invoked automatically on objects managed by the try-with-resources statement.

Annotations
@Override
Exceptions
Exception:
if this resource cannot be closed
createback to summary
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:boolean

true if the context should use strong references, false to use (default) weak references for automatic logger GC

Returns:LogContext

a new log context

createback to summary
public static LogContext create(boolean strong, LogContextInitializer initializer)

Create a new log context. If a security manager is installed, the caller must have the "createLogContext" RuntimePermission to invoke this method.

Parameters
strong:boolean

true if the context should use strong references, false to use (default) weak references for automatic logger GC

initializer:LogContextInitializer

the log context initializer to use (must not be null)

Returns:LogContext

a new log context

createback to summary
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:LogContext

a new log context

createback to summary
public static LogContext create(LogContextInitializer initializer)

Create a new log context. If a security manager is installed, the caller must have the "createLogContext" RuntimePermission to invoke this method.

Parameters
initializer:LogContextInitializer

the log context initializer to use (must not be null)

Returns:LogContext

a new log context

createChildMapback to summary
pack-priv ConcurrentMap<String, LoggerNode> createChildMap()
detachback to summary
public <V> V detach(Logger.AttachmentKey<V> key) throws SecurityException

Remove an attachment. Log context attachments are placed on the root logger and can also be accessed there.

Parameters
<V>
the attachment value type
key:Logger.AttachmentKey<V>

the attachment key

Returns:V

the old value, or null if there was none

Annotations
@SuppressWarnings:unchecked
Exceptions
SecurityException:
if a security manager exists and if the caller does not have LoggingPermission(control)
discoverDefaultInitializerback to summary
private static LogContextInitializer discoverDefaultInitializer()
discoverDefaultInitializer0back to summary
private static LogContextInitializer discoverDefaultInitializer0()
getAttachmentback to summary
public <V> V getAttachment(Logger.AttachmentKey<V> key)

Get the attachment value for a given key, or null if there is no such attachment. Log context attachments are placed on the root logger and can also be accessed there.

Parameters
<V>
the attachment value type
key:Logger.AttachmentKey<V>

the key

Returns:V

the attachment, or null if there is none for this key

Annotations
@SuppressWarnings:unchecked
getAttachmentback to summary
public <V> V getAttachment(String loggerName, Logger.AttachmentKey<V> key)

Get a logger attachment for a logger name, if it exists.

Parameters
<V>
the attachment value type
loggerName:String

the logger name

key:Logger.AttachmentKey<V>

the attachment key

Returns:V

the attachment or null if the logger or the attachment does not exist

getCloseHandlersback to summary
public Set<AutoCloseable> getCloseHandlers()

Gets the current close handlers associated with this log context.

Returns:Set<AutoCloseable>

the current close handlers

getInitializerback to summary
pack-priv LogContextInitializer getInitializer()
getLevelForNameback to summary
public Level getLevelForName(String name) throws IllegalArgumentException

Get the level for a name.

Parameters
name:String

the name

Returns:Level

the level

Exceptions
IllegalArgumentException:
if the name is not known
getLogContextback to summary
public static LogContext getLogContext()

Get the currently active log context.

Returns:LogContext

the currently active log context

getLogContextSelectorback to summary
public static LogContextSelector getLogContextSelector()

Returns the currently set log context selector.

Returns:LogContextSelector

the log context selector

getLoggerback to summary
public Logger getLogger(String name)

Get a logger with the given name from this logging context.

Parameters
name:String

the logger name

Returns:Logger

the logger instance

See Also
java.util.logging.LogManager#getLogger(String)
getLoggerIfExistsback to summary
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:String

the logger name

Returns:Logger

the logger instance, or null if no such logger node exists

getLoggerNamesback to summary
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:Enumeration<String>

an enumeration of the logger names

See Also
java.util.logging.LogManager#getLoggerNames()
getRootLoggerNodeback to summary
pack-priv LoggerNode getRootLoggerNode()
getSystemLogContextback to summary
public static LogContext getSystemLogContext()

Get the system log context.

Returns:LogContext

the system log context

pinback to summary
pack-priv boolean pin(LoggerNode node)
recursivelyCloseback to summary
private void recursivelyClose(final LoggerNode loggerNode)
registerLevelback to summary
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:Level

the level to register

registerLevelback to summary
public void registerLevel(Level level, boolean strong)

Register a level instance with this log context. The level can then be looked up by name. Any previous level registration for the given level's name will be overwritten.

Parameters
level:Level

the level to register

strong:boolean

true to strongly reference the level, or false to weakly reference it

setCloseHandlersback to summary
public void setCloseHandlers(final 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:Collection<AutoCloseable>

the close handlers to use

setLogContextSelectorback to summary
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:LogContextSelector

the new selector.

unpinback to summary
pack-priv boolean unpin(LoggerNode node)
unregisterLevelback to summary
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:Level

the level to unregister

org.jboss.logmanager back to summary

private final Class LogContext.LazyHolder

extends Object
Class Inheritance

This lazy holder class is required to prevent a problem due to a LogContext instance being constructed before the class init is complete.

Field Summary

Modifier and TypeField and Description
private static final HashMap<String, Reference<Level, Void>>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

INITIAL_LEVEL_MAPback to summary
private static final HashMap<String, Reference<Level, Void>> INITIAL_LEVEL_MAP

Constructor Detail

LazyHolderback to summary
private LazyHolder()

Method Detail

addStrongback to summary
private static void addStrong(Map<String, Reference<Level, Void>> map, Level level)