kingsoft/monolog-handler

Additional handlers monolog, log rotation and cron handler

1.2.2 2025-02-27 17:23 UTC

This package is auto-updated.

Last update: 2025-03-27 17:41:43 UTC


README

This handler combines rotating (AbstractRotatingFileHandler) with an crontab based FileHandler (CronRotatingFileHandler) to offer a cron based rotating file handler. It is based on Cesargb\php-log-rotation for advanced rotation and dragonmantank\cron-expression for cron interpretation. The abstract class can be used for different ways of rotation and requires an implementation of mustRotate() to estblish to know if a rotation is needed.

Future versions might split the two and who knows AbstractRotatingFileHandler might end up in Monolog...

Install and configure

Use Kingoft/Utils to include make a global SETTINGS available. Or look at the PHPUNIT test file to see what is needed to configure.

require $_SERVER['DOCUMENT_ROOT'] . '/vendor/kingsoft/utils/settings.inc.php';

CronRotatingFileHandler

Example usage

$log = new Monolog\Logger( SETTINGS['log']['name'] );

$log->pushHandler(
	new \Kingsoft\MonologHandler\CronRotatingFileHandler(
		SETTINGS['log']['location'] . '/' . SETTINGS['log']['name'] . '_info.log',
		Monolog\Level::fromName( SETTINGS['log']['level'],
		SETTINGS['logrotate'] )
	)
);

With this in the ini-file

[log]
name = "app"
location = "D:/Projekte/logs"
level = Info

[logrotate]
cronExpression = '* */1 * * *'
maxFiles = 2
minSize = 120
compress = false

Important

Rotation will not work if a another instance is opening the log and write an entry. This would touch the .state and will not rotate!. This could happen for instance and API write to the same log file before a frontend application pings back to the server. In other words this logger is currently not reentrant-safe.