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.
Installation
Section titled “Installation”composer require marko/log-fileThis automatically installs marko/log.
Automatic via Binding
Section titled “Automatic via Binding”Bind the logger interface in your 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}Rotation Strategies
Section titled “Rotation Strategies”Daily rotation (default) --- one file per day, date in filename:
var/log/app-2025-01-15.logvar/log/app-2025-01-16.logSize rotation --- rotates when a file exceeds the configured size limit (default: 10 MB):
var/log/app.logvar/log/app.1.logvar/log/app.2.logCustomization
Section titled “Customization”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 }}API Reference
Section titled “API Reference”FileLogger
Section titled “FileLogger”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:
| Parameter | Type | Description |
|---|---|---|
$path | string | Directory where log files are written |
$channel | string | Channel name used in filenames and log output |
$minimumLevel | LogLevel | Messages below this level are skipped |
$formatter | LogFormatterInterface | Formats log records into strings |
$rotation | RotationStrategyInterface | Rotation 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.
RotationStrategyInterface
Section titled “RotationStrategyInterface”use Marko\Log\File\Rotation\RotationStrategyInterface;
interface RotationStrategyInterface{ public function getCurrentPath(string $basePath, string $channel): string; public function needsRotation(string $filePath): bool;}Built-in Rotation Strategies
Section titled “Built-in Rotation Strategies”DailyRotation--- Date-stamped filenames, rotates automatically each daySizeRotation--- Numbered filenames, rotates when file exceeds max size (default: 10 MB)