Logging helps you track application behavior, diagnose bugs, and analyze performance by recording events, context, and errors to various destinations without impacting functionality.
<?php
use Krystal\Logging\Logger;
use Krystal\Logging\Adapter\FileWriter;
use Krystal\Logging\Adapter\ConsoleWriter;
// Create the logger instance
$logger = new Logger();
// Add writers (adapters)
$logger->addWriter(new FileWriter(__DIR__ . '/app.log'));
$logger->addWriter(new ConsoleWriter());
// Log messages
$logger->info('Application started');
$logger->error('Database connection failed', ['host' => 'localhost', 'port' => 3306]);
The LoggerFactory allows you to define configuration in an array, making it easy to integrate with configuration files.
<?php
use Krystal\Logging\LoggerFactory;
$logger = LoggerFactory::build([
'writers' => [
[
'type' => 'file',
'path' => __DIR__ . '/app.log'
],
[
'type' => 'console'
]
]
]);
// Log messages
$logger->warning('This is a warning message');
The logger is automatically available in controllers, module files, when configured in the main configuration file.
Step 1 – Configuration (config/app.php)
Define the logger in your main application configuration file.
<?php
return [
'components' => [
'logger' => [
'writers' => [
[
'type' => 'file',
'path' => dirname(__DIR__) . '/data/logs/app-' . date('Y-m-d') . '.log',
],
[
'type' => 'console'
]
]
],
// other components...
]
];
Tip: In real production setups, move the date('Y-m-d') logic to a bootstrap file or environment variable so you can override it later.
Step 2 – Usage in Controllers
Then use it in controllers like this
public function addAction()
{
$this->logger->warning('This is a warning message');
}
The library supports the following levels (ordered from highest to lowest severity):
| Method | Level | Description |
|---|---|---|
$logger->emergency() |
0 | System is unusable. |
$logger->alert() |
1 | Action must be taken immediately. |
$logger->critical() |
2 | Critical conditions. |
$logger->error() |
3 | Runtime errors. |
$logger->warning() |
4 | Exceptional occurrences that are not errors. |
$logger->notice() |
5 | Normal but significant events. |
$logger->info() |
6 | Interesting events. |
$logger->debug() |
7 | Detailed debug information. |
To create a new adapter, implement the Krystal\Logging\Adapter\LogWriterInterface.
<?php
use Krystal\Logging\Adapter\LogWriterInterface;
class SlackWriter implements LogWriterInterface
{
public function write(int $level, string $message, array $context = []): bool
{
// Custom logic to send message to Slack
return true;
}
}
Then add manually add it:
$logger->addWriter(new SlackWriter());