Comments (3)
i'm planning to write this if anyone interested
from monolog.
based on
@dragonmantank cron-expression and
@cesargb rotate
<?php declare(strict_types=1);
namespace Monolog\Handler;
/**
* (c) TheKing2 <[email protected]>
* cron based rotating
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Monolog\Level;
use Monolog\Utils;
/**
* Stores logs to files that are rotated every day and a limited number of files are kept.
*
* This rotation is only intended to be used as a workaround. Using logrotate to
* handle the rotation is strongly encouraged when you can use it.
*
* @author theking2
*/
class CronRotatingFileHandler extends StreamHandler implements HandlerInterface
{
private $filename;
private $stateFilename;
/**
* @param int $maxFiles The maximal amount of files to keep (0 means unlimited)
* @param int|null $filePermission Optional file permissions (default (0644) are only for owner read/write)
* @param bool $useLocking Try to lock log file before doing any writes
*/
public function __construct(
string $filename,
array $rotateSettings,
int|string|Level $level = Level::Debug,
bool $bubble = false,
?int $filePermission = null,
bool $useLocking = false
) {
$this->filename = Utils::canonicalizePath( $filename );
$this->checkRotate( $rotateSettings );
parent::__construct( $this->filename, $level, $bubble, $filePermission, $useLocking );
}
/**
* checkRotate
*
* @return void
*/
protected function checkRotate( array $rotateSettings ): void
{
$cronExpression = $rotateSettings['cronExpression'] ?? '* * */1 * *';
$maxFiles = $rotateSettings['maxFiles'] ?? 366;
$minSize = $rotateSettings['minSize'] ?? 0;
$compress = $rotateSettings['compress'] ?? false;
$stateFilename = Utils::canonicalizePath( $this->filename . '.state' );
$fileInfo = new \SplFileInfo( $stateFilename );
// create state file if not exist
if( !$fileInfo->isFile() ) {
touch( $stateFilename );
}
$rotation = ( new \Cesargb\Log\Rotation() )
->files( $maxFiles )
->minSize( $minSize );
if( $compress ) {
$rotation->compress();
}
$cron = new \Cron\CronExpression( $cronExpression );
// check if log file is due based on state file modified time
$dateTime = new \DateTimeImmutable(); // current datetime
$filedateTime = $dateTime->setTimeStamp( $fileInfo->getMTime() ); // state-file datetime
$nextRun = $cron->getNextRunDate( $filedateTime ); // next-run datetime
// if log file is due, rotate it
if( $nextRun < $dateTime ) {
// rotate log file
touch( $fileInfo->getRealPath() );
$rotation->rotate( $this->filename );
}
}
}
from monolog.
Closing as per #1854 (comment) - again this isn't to say it's a bad idea, but I just cannot maintain every possible handler that's the main reason I cannot take this on.
from monolog.
Related Issues (20)
- The file does not close after writing HOT 5
- How do I use includeStacktraces? HOT 1
- Support more flexible normalizer HOT 2
- cannot access private method HOT 7
- Add support for Elasticsearch with Bulk API and data stream HOT 1
- Could a new release be considered to remove the 0.7MB logo from the package ? HOT 1
- Uncaught Error: Class "Monolog\Logger" not found HOT 7
- User definable duplication for DeduplicationHandler
- Encounter problem in format function HOT 3
- DatetimeImmutable HOT 1
- [Discussion] Reset FingersCrossedHandler on stateful runtimes HOT 8
- Extending Processors - private properties HOT 1
- Incorrect logs are written in Monolog files intermittently HOT 1
- Truncated JsonFormatter logs at 8191 characters HOT 5
- Log Message is truncated HOT 1
- Log level not compliant with rfc5424 HOT 2
- DeduplicationHandler in a read only container HOT 4
- IntrospectionProcessorTests: $expect and $actual are the same object by reference.
- Php 8.3 compatibility issue HOT 5
- Adding postParams in ERROR notice for HTTP 405 code HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from monolog.