Skip to content

marko/log-file

File-based logging driver --- writes log messages to disk with daily or size-based rotation. Implements LoggerInterface from marko/log by writing formatted log lines to files. It supports daily rotation (one file per day) and size-based rotation (new file when size limit is reached). Messages below the configured minimum level are silently skipped. All settings come from config/log.php.

Terminal window
composer require marko/log-file

This automatically installs marko/log.

Bind the logger interface in your module.php:

module.php
use Marko\Log\Contracts\LoggerInterface;
use Marko\Log\File\Driver\FileLogger;
return [
'bindings' => [
LoggerInterface::class => FileLogger::class,
],
];

Then inject LoggerInterface anywhere:

use Marko\Log\Contracts\LoggerInterface;
class ImportService
{
public function __construct(
private LoggerInterface $logger,
) {}
public function import(
string $file,
): void {
$this->logger->info('Starting import', ['file' => $file]);
// ...
$this->logger->info('Import complete', ['rows' => 1500]);
}
}

Log output (with daily rotation): var/log/app-2025-01-15.log

[2025-01-15 10:30:00] app.INFO: Starting import {"file":"products.csv"}
[2025-01-15 10:30:05] app.INFO: Import complete {"rows":1500}

Daily rotation (default) --- one file per day, date in filename:

var/log/app-2025-01-15.log
var/log/app-2025-01-16.log

Size rotation --- rotates when a file exceeds the configured size limit (default: 10 MB):

var/log/app.log
var/log/app.1.log
var/log/app.2.log

Replace the rotation strategy via Preference:

use Marko\Core\Attributes\Preference;
use Marko\Log\File\Rotation\DailyRotation;
use Marko\Log\File\Rotation\SizeRotation;
#[Preference(replaces: DailyRotation::class)]
class CustomRotation extends SizeRotation
{
public function __construct()
{
parent::__construct(maxSize: 50 * 1024 * 1024); // 50 MB
}
}

Implements LoggerInterface. See marko/log for the full method list.

public function emergency(string $message, array $context = []): void;
public function alert(string $message, array $context = []): void;
public function critical(string $message, array $context = []): void;
public function error(string $message, array $context = []): void;
public function warning(string $message, array $context = []): void;
public function notice(string $message, array $context = []): void;
public function info(string $message, array $context = []): void;
public function debug(string $message, array $context = []): void;
public function log(LogLevel $level, string $message, array $context = []): void;

The constructor accepts the following parameters:

ParameterTypeDescription
$pathstringDirectory where log files are written
$channelstringChannel name used in filenames and log output
$minimumLevelLogLevelMessages below this level are skipped
$formatterLogFormatterInterfaceFormats log records into strings
$rotationRotationStrategyInterfaceRotation strategy (defaults to DailyRotation)

Writes use FILE_APPEND | LOCK_EX for safe concurrent appends. The log directory is created automatically if it does not exist. A LogWriteException is thrown if the directory is not writable or a write fails.

use Marko\Log\File\Rotation\RotationStrategyInterface;
interface RotationStrategyInterface
{
public function getCurrentPath(string $basePath, string $channel): string;
public function needsRotation(string $filePath): bool;
}
  • DailyRotation --- Date-stamped filenames, rotates automatically each day
  • SizeRotation --- Numbered filenames, rotates when file exceeds max size (default: 10 MB)