Re: Multiple applications using GetLogger with same name

2023-08-01 Thread Piotr P. Karwasz
Hi Ralph,

On Tue, 1 Aug 2023 at 20:58, Ralph Goers  wrote:
> 2. Partition your application so that the main application and each batch job 
> use their own ClassLoader. The ClassLoaderContextSelector (the default) will 
> then create a LoggerContext for each ClassLoader and each can have its own 
> configuration.

This is not so easy with the current ClassLoaderContextSelector: if
the parent classloader already has a logger context and the context
selector was called with `configLocation=null` (which is almost always
the case), the ClassLoaderContextSelector returns the logger context
of the parent classloader.

Piotr

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



Re: Multiple applications using GetLogger with same name

2023-08-01 Thread Piotr P. Karwasz
Hi Danish,

On Tue, 1 Aug 2023 at 20:10, Danish Arif  wrote:
> My use case is that my application uses the above mentioned method to get
> logger and log. Appender and Logger have been set in log4j2.xml file. We
> stop the Logger by getting all core appenders , removing them , then
> stopping them and then using the LogManager.shutdown() method.

There is no need for that, because:
1. LogManager.shutdown() stops all appenders,
2. Usually the `log4j2.shutdownHookEnabled` property is set to true,
so LogManager.shutdown() is called by a shutdown hook.

> The issue or anomaly arises when an update batch script starts the new
> instance of the same application and the old instance takes some time to
> exit, resulting in new instance getting the same logger using
> LogManager.getLogger("AppName"); and writing in the same log file.
>
> Once the 1st/old instance exists after a few seconds, it closes all the
> logging and nothing on the file gets written even when the 2nd/new instance
> is still up and running.

If these instances are in different JVM's, this is strange: can you
add the configuration of your file appender?
If you are using a `RollingFileAppender`, it can rotate your file and
delete the old ones.

If these instances are in the same JVM, you might be up to something:
apparently `stop()` is not idempotent and if you call it enough times,
you can effectively stop appenders in other logger contexts.

Piotr

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



Re: Multiple applications using GetLogger with same name

2023-08-01 Thread Ralph Goers
I answered this same question on the dev list a week ago since you apparently 
cross posted to both. Here it is again.

Your issue is related to you starting and stopping the LoggerContext while your 
application is in an unknown state.  I can think of a few ways to deal with 
this.

1. Never shutdown the LoggerContext and only use RoutingAppenders. Each 
application should set its own key for the Route so that each uses their own 
Appenders. During shutdown of the application only shutdown the Appenders that 
apply to the applications Route.
2. Partition your application so that the main application and each batch job 
use their own ClassLoader. The ClassLoaderContextSelector (the default) will 
then create a LoggerContext for each ClassLoader and each can have its own 
configuration.
3. Use the JndiContextSelector and have each application and batch job use a 
unique JNDI name. This will allow each to have its own LoggerContext. However, 
if any of the classes use static Loggers and those classes are in a ClassLoader 
shared by the application and batch jobs then those Loggers will be tied to the 
LoggerContext used when they Loggers were first obtained. So this solution may 
not completely work for you.

Ralph


> On Aug 1, 2023, at 11:09 AM, Danish Arif  wrote:
> 
> Hi Log4j2 Team,
> 
> My query is related to log4j2 working and uses of
> LogManager.getLogger(String name); method.
> 
> My use case is that my application uses the above mentioned method to get
> logger and log. Appender and Logger have been set in log4j2.xml file. We
> stop the Logger by getting all core appenders , removing them , then
> stopping them and then using the LogManager.shutdown() method.
> 
> The issue or anomaly arises when an update batch script starts the new
> instance of the same application and the old instance takes some time to
> exit, resulting in new instance getting the same logger using
> LogManager.getLogger("AppName"); and writing in the same log file.
> 
> Once the 1st/old instance exists after a few seconds, it closes all the
> logging and nothing on the file gets written even when the 2nd/new instance
> is still up and running.
> 
> Above mentioned scenario's  work around is if we don't remove/stop all
> appenders and don't use shutdown() method, but is there any more
> graceful handling which can be done?
> 
> The second question to this issue Is there any solution if multiple
> instances of the same application which are agnostic to each other's
> existence, use GetLogger("SameName"); and log into files with names in
> incremental id appended dynamically. For Example App Inst1 when gets
> logger, log into MyLogFile1 and when Inst2 initiates it logs into
> MyLogFile2.
> 
> Kindly advise what can be done to handle this type of scenario when there
> is no communication between the instances and are totally independent and
> agnostic of each other.
> 
> 
> 
> Regards,
> Danish Arif


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



Re: Multiple applications using GetLogger with same name

2023-08-01 Thread Eric Schwarzenbach

On 8/1/23 14:09, Danish Arif wrote:

The second question to this issue Is there any solution if multiple
instances of the same application which are agnostic to each other's
existence, use GetLogger("SameName"); and log into files with names in
incremental id appended dynamically. For Example App Inst1 when gets
logger, log into MyLogFile1 and when Inst2 initiates it logs into
MyLogFile2.
This might be relevant: 
https://stackoverflow.com/questions/25754933/log4j2-include-pid


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



Multiple applications using GetLogger with same name

2023-08-01 Thread Danish Arif
Hi Log4j2 Team,

My query is related to log4j2 working and uses of
LogManager.getLogger(String name); method.

My use case is that my application uses the above mentioned method to get
logger and log. Appender and Logger have been set in log4j2.xml file. We
stop the Logger by getting all core appenders , removing them , then
stopping them and then using the LogManager.shutdown() method.

The issue or anomaly arises when an update batch script starts the new
instance of the same application and the old instance takes some time to
exit, resulting in new instance getting the same logger using
LogManager.getLogger("AppName"); and writing in the same log file.

Once the 1st/old instance exists after a few seconds, it closes all the
logging and nothing on the file gets written even when the 2nd/new instance
is still up and running.

Above mentioned scenario's  work around is if we don't remove/stop all
appenders and don't use shutdown() method, but is there any more
graceful handling which can be done?

The second question to this issue Is there any solution if multiple
instances of the same application which are agnostic to each other's
existence, use GetLogger("SameName"); and log into files with names in
incremental id appended dynamically. For Example App Inst1 when gets
logger, log into MyLogFile1 and when Inst2 initiates it logs into
MyLogFile2.

Kindly advise what can be done to handle this type of scenario when there
is no communication between the instances and are totally independent and
agnostic of each other.



Regards,
Danish Arif