public interface LogContext
clear()
is called. These class is specifically designed for
multithreaded environments and allow to separate otherwise intermixed log messages. Implementation is expected to
propagate this log context on child threads, most probable using InheritableThreadLocal
.
Here is an example of a hypothetical servlet. On every request, service method stores context name and remote address
with app
, respective ip
names, on log context; every log message will include context name
and remote address no matter if called directly from servlet service or from a nested method.
private static final LogContext logContext = LogFactory.getLogContext(); protected void service(HttpServletRequest httpRequest, HttpServletResponse httpResponse) { // store request remote address to current thread logContext.put("app", httpRequest.getContextPath()); logContext.put("ip", httpRequest.getRemoteHost()); try { // log message can be configured to include request remote address log.debug(...); } finally { // takes care to cleanup request remote address from current thread since thread can be reused logContext.clear(); } }
Modifier and Type | Method and Description |
---|---|
void |
clear()
Cleanup all diagnostic data from this logger context.
|
void |
put(String name,
Object value)
Put named diagnostic context data on logger related to current thread.
|
void put(String name, Object value)
clear()
is called on current thread. Override value if named diagnostic
data already exists.
If given name
is null or empty this method silently does nothing and if value
is null
remove named diagnostic context data.
name
- name for diagnostic context data, not null,value
- diagnostic context value, possible null.void clear()
Copyright © 2020. All rights reserved.