Hi,
consider a logging server. Let's say, there's a REST service "log",
accepting the following parameters:
- applicationId
- category
- level
- message
The purpose here is to use the application id as identification of a
log4j configuration. So, the implementation would look roughly like
this:
private static final ConcurrentHashMap<String,LoggerContext>
applications = new ConcurrentHashMap<>();
public void log(String pApplicationId, String pCategory,
String pLevel, String pMessage) {
final LoggerContext lc =
applications.computeIfAbsent(pApplicationId,
(id) -> newLoggerContext(id));
final Logger logger = lc.getLogger(pCategory)
logger.log(pLevel, message);
}
I hope, I can make myself understandable. Questions:
1.) Is that design appropriate? In particular, is the mapping between
application id, and the LoggerContext (as a representation of the
log4j configuration) appropriate?
2.) Assuming, that I want to change an applications configuration: Is
loggerContext.updateLoggers(Configuration)
the proper way to do that? If so, is it possible to obtain the current
Configuration, modifying it, and then do the update?
3.) Did I consider multi-threading properly? Basically, my idea is, that
using a ConcurrentHashMap, and computeIfAbsent is sufficient to
obtain a LoggerContext. From that point, I would trust in the
multi-threading abilities of Log4J.
Thanks,
Jochen
--
Look, that's why there's rules, understand? So that you think before
you break 'em.
-- (Terry Pratchett, Thief of Time)