[logback-user] Create a new log file only if the default one is open

Olivier Cailloux olivier.cailloux at dauphine.fr
Sat Oct 10 15:57:14 CEST 2020


Dear list,

I’d like to set up logback so that it always records the log to the
file out.log, except when a process is already busy writing to out.log
when my program start. (I expect such a process will simply be another
instance of my program; when multiple instances run in parallel, I want
to have multiple log files, one per instance.)

I know that logback can be asked to create uniquely named files (
http://logback.qos.ch/manual/appenders.html#uniquelyNamed), but I do
not want that: most of the time, only one instance of my program is
running, in which case, it is fine to replace the log file, using the
default name (out.log), instead of creating a new one at each run. I’d
like to deviate from the default name only when the default one is
busy.

It is possible to detect (at least under Linux, but, I suppose, in
other OSes as well) that a process has a file open, so this seems
feasible, at least in principle; and it seems like a natural
requirement for programs designed to allow parallel instances. I am
surprised I didn’t see anything related to this in the logback manual.
Is there a reason not to want what I want?

I realize that a race condition could happen, in principle: if someone
creates a second instance of my program immediately after the first
one, the second one could decide to use out.log because that file is
not yet open by the first one, ending up with both processes logging to
the same file. I am ready to live with that risk.

This question was also asked on SO (
https://stackoverflow.com/q/64184299), to no avail.

-- 
Olivier



More information about the logback-user mailing list