Skip to content

\Leaf\LogWritter is not suitable for production environments #6

Description

@ghosthi

Currently, the method write from the LogWritter class forces the full rewrite of the log file, which can get massive in production environments, causing unnecessary disk usage.

The correct way for production environments should just append to the file, using the file's internal pointer. This way is much faster and avoids unnecessary problems.

I suggest that the log, by default, append to file unless a config, such as 'log.mode', explicitly says to prepend to file, like in the example below:

`
protected function writeAsLeaf($message, $level)
{
$this->trueWrite(
"[" . (new \Leaf\Date())->tick()->now() . "]\n" . $level . "$message\n\n"
);
}

protected function writeAsLinux($message, $level)
{
    $this->trueWrite(
        "[" .  (new \Leaf\Date())->tick()->now() . "] " . $level . "$message\n\n"
    );
}

private function trueWrite(string $formmatedMessage)
{
    if ( \Leaf\Config::get('log.mode') === 'prepend') {
        $contentToWrite = function ($content) use ($message, $level) {
            return $formmatedMessage . $content;
        };
        $mode = 0;
    } else {
        $contentToWrite = $formmatedMessage;
        $mode = FILE_APPEND;
    }

    FS\File::write(
        $this->logFile,
        $contentToWrite,
        $mode
    );
}

}
`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions