GithubHelp home page GithubHelp logo

Comments (3)

theking2 avatar theking2 commented on June 1, 2024

i'm planning to write this if anyone interested

from monolog.

theking2 avatar theking2 commented on June 1, 2024

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.

Seldaek avatar Seldaek commented on June 1, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.