Skip to content

Auto-deletion of log files based on config#80

Merged
arhimede merged 3 commits intodotkernel:5.0from
Jurj-Bogdan:issue79
Oct 14, 2025
Merged

Auto-deletion of log files based on config#80
arhimede merged 3 commits intodotkernel:5.0from
Jurj-Bogdan:issue79

Conversation

@Jurj-Bogdan
Copy link
Copy Markdown
Member

@Jurj-Bogdan Jurj-Bogdan commented Oct 10, 2025

Q A
New Feature yes
RFC yes
BC Break no

An initial version of the feature requested in #79 .

This PR only adds an extra method to the existing Stream Writer class and the extra config and processing it requires.

A few things to mention:

  1. I went with the configuration per writer as it would allow setting different (or none at all) deletion times independent of other configured loggers or writers, I'll go over more details below.
    • let me know if this is maybe too much and a simpler "delete all logs" approach is preferred.
  2. Since it's the default writer shipped with the package I went with editing the Stream class
    • also didn't modify the AbstractWriter itself as I didn't want to force any custom writers to have this feature as well
    • do you think it should be expanded to a separate (service?) class instead?
  3. Files to be deleted are selected by matching the configured writer's stream format (examples below).
  4. I've expanded the deletion value to accept other DateTimeImmutable formats, but the int values mentioned in the issue work, defaulting to N days - again, let me know if this is unnecessary.

Here's a small example for the following logger config:

    'dot_log'          => [
        'loggers' => [
            'error_writer' => [
                //...
                'options' => [
                    'stream' => __DIR__ . '/../../log/error-log-{Y}-{m}-{d}.log',
                    'log_lifetime' => null,
                    //...
                ],
            ],
            'custom_writer' => [
                //...
                'options' => [
                    'stream'       => __DIR__ . '/../../log/{Y}-{m}-{d}.log',
                    'log_lifetime' => 30,
                    //...
                ]
            ],
            'second_custom_writer' => [
                //...
                'options' => [
                    'stream'       => __DIR__ . '/../../log/{Y}-{m}-{d}_custom_log.log',
                    'log_lifetime' => '12 hours ago',
                    //...
                ]
            ]
        ],
    ]

And the following existing log files:

2025-06-10.log
2025-10-09.log
2025-10-10.log
error-log-2025-10-09.log
error-log-2025-10-10.log
2025-10-09_custom_log.log
2025-10-10_custom_log.log

For logs written during 2025.10.10 the flow is as follows:

Using error_writer to write will not trigger any deletion as log_lifetime is null (can be omitted completely).

Using custom_writer will only delete 2025-06-10.log because the set value will be converted to "30 days ago", so 2025-10-09 is too new. All other log format untouched.

Using second_custom_writer will delete 2025-10-09_custom_log.log only (presuming 12 hours have indeed passed).

Signed-off-by: Jurj-Bogdan <bogdanjurj11@gmail.com>
@Jurj-Bogdan Jurj-Bogdan linked an issue Oct 10, 2025 that may be closed by this pull request
@Jurj-Bogdan Jurj-Bogdan self-assigned this Oct 10, 2025
Comment thread src/Writer/Stream.php Outdated
$path = $streamData['uri'];
$uriParts = pathinfo($path);

$files = scandir($uriParts['dirname']);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to be sure that you are in the log folder all the time
hmm

Comment thread src/Writer/Stream.php Outdated
$fileTimestamp !== false
&& $fileTimestamp < $this->logLifetime
) {
unlink($match);
Copy link
Copy Markdown
Member

@arhimede arhimede Oct 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is for various reason the file have wrong permissions ?
or it is a symlink ?

should we use @ unlink ?

@arhimede
Copy link
Copy Markdown
Member

4. I've expanded the deletion value to accept other `DateTimeImmutable` formats, but the `int` values mentioned in the issue work, defaulting to N days - again, let me know if this is unnecessary.

I think is too much to allow such a comple format
a simple int , with value in days, must be enough

Signed-off-by: Jurj-Bogdan <bogdanjurj11@gmail.com>
Comment thread log.global.php.dist Outdated
'name' => 'MyFilter',
],
],
'log_lifetime' => null, // optional
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add here a short comment with possible values , which mean an integger

Signed-off-by: Jurj-Bogdan <bogdanjurj11@gmail.com>
@arhimede arhimede merged commit 4eb93f5 into dotkernel:5.0 Oct 14, 2025
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

define a value to delete log file

2 participants