#1 2025-09-06 03:59:09

zen010101
Member
Registered: 2024-06-15
Posts: 127

Log rotation is not working as expect

Offline

#2 2025-09-06 17:17:59

zen010101
Member
Registered: 2024-06-15
Posts: 127

Re: Log rotation is not working as expect

Based on code tracing, an excessively small RotateFileSizeKB is not the key cause of the issue. The PerformRotation method is only invoked within the following method:

procedure TSynLog.LogEnterFmt(nfo: PSynLogThreadInfo; inst: TObject;
  fmt: PUtf8Char; args: PVarRec; argscount: PtrInt); 

However, the check and invocation of PerformRotation are missing in the method below:

procedure TSynLog.LogEnter(nfo: PSynLogThreadInfo; inst: TObject; txt: PUtf8Char
  {$ifdef ISDELPHI} ; addr: PtrUInt {$endif});   

This means that calls like `l := TSynLog.Enter;` cannot trigger the rotation check.
This is the first bug.

Even if we use TSynLog.LogEnterFmt (which can trigger the rotation check), the check relies on the following condition:

if pendingRotate in fPendingFlags then // from OnFlushToStream
  PerformRotation;   

And the OnFlushToStream event is only assigned to fWriter.OnFlushToStream if AutoFlushTimeOut is explicitly set to a non-zero value:

if fFamily.AutoFlushTimeOut <> 0 then
begin
  fWriter.OnFlushToStream := OnFlushToStream; // note: overwrites fWriterEcho
  OnFlushToStream(nil, 0);
  fFamily.EnsureAutoFlushThreadRunning;
end; 

In other words, if I do not explicitly set AutoFlushTimeOut, OnFlushToStream will never be triggered—and consequently, PerformRotation will not run either.
This is the second bug.

Thirdly, even if the above two bugs are fixed, the PerformRotation check is only invoked in methods related to TSynLog.Enter. Calls like TSynLog.DoLog or TSynLog.Add.Log will still not trigger rotation checks. This behavior may require further improvement, or at the very least, explicit documentation to clarify it.

Offline

#3 2025-09-06 18:31:19

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,173
Website

Re: Log rotation is not working as expect

It sounded like a feature to me, since I expected to have a lot of Enter/Leave in the logs.

Of course, I understand it may not be the case.

Offline

#4 Yesterday 07:36:44

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,173
Website

Re: Log rotation is not working as expect

Offline

#5 Today 13:32:37

zen010101
Member
Registered: 2024-06-15
Posts: 127

Re: Log rotation is not working as expect

Awesome! Thank you so much for the quick and thorough fix ?

I’ve just reviewed the commits, and they seem to address the key issues I mentioned earlier. I’ll take some time to test these changes carefully when I get the chance, and see if the rotation mechanism now works as expected.

Really appreciate your effort and continuous improvements—mORMot is getting stronger and better!

Offline

#6 Today 14:16:33

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,173
Website

Re: Log rotation is not working as expect

It was clearly some premature optimization from my side, during the TSynLog refactoring some weeks ago.
With these commits, we should be more correct, and with no performance impact: checking the pendingRotate flag for each log entries (but sllLeave) is minimal to execute.

Thanks to you for your detailed review, debug, deep explanation and minimum example.
smile

Offline

Board footer

Powered by FluxBB