If you are creating separate LoggerContext’s then every one of them can have 
its own configuration. The LoggerContext has an externalMap that you can add 
items to. You could then create a Lookup to retrieve elements from the external 
Map to resolve references in a common configuration file shared by all the 
LoggerContext instances.  As you note though, all the relevant classes will 
have to reference “dynamic” Loggers, not ones associated with the Class or 
instance, or else the will associated with the LoggerContext used when the 
Class or instance was instantiated. However, they can all use the same Logger 
name.

Ralph

> On Feb 28, 2024, at 4:30 PM, NH Rao <nhr...@gmail.com> wrote:
> 
> Greetings,
> 
> We are in the midst of tech stack upgrade and have upgraded to log4j
> version 2.23.0. I am checking if I can do things differently or in a better
> way.
> 
> We have an application not web application, but good old java/jar
> application. It receives "jobs/tasks" on MQ queues. Each job can create
> large number of log entries. We want to have separation of logs for each
> logs.
> 
> Currently we use routers in the xml config file. This mostly works, as long
> as developer remembers to set the right route values for the thread.
> 
> I'd like to do this programmatically as we have a need for splitting logs
> even more. Each class can request logger based on task id and type of log
> and log to that logger.
> 
> I am creating a new LoggerContext with task id as name. As application
> requests the loggers, I am creating the appenders and adding it to
> loggers.  At the end of task, we basically close all the taks specific
> appenders. This works the way I want it to work. However
> method org.apache.logging.log4j.core.Logger.addAppender(Appender) says not
> exposed as public API.
> 
> Is there any better way to achieve what I want to do without using routes?
> 
> Regards,
> 
> Niranjan


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscr...@logging.apache.org
For additional commands, e-mail: log4j-user-h...@logging.apache.org

Reply via email to