Download the PHP package ody/logger without Composer
On this page you can find all versions of the php package ody/logger. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package logger
Logging
Logging is configured in config/logging.php
and provides various channels for logging:
Custom Loggers in ODY Framework
Creating Custom Loggers
Basic Requirements
All custom loggers must:
- Extend
Ody\Logger\AbstractLogger
- Implement a static
create(array $config)
method - Override the
write(string $level, string $message, array $context = [])
method
Example: Creating a Custom Logger
Here's a simple example of a custom logger that logs to Redis:
The create()
Method
The static create()
method is responsible for instantiating your logger based on configuration:
This method receives the channel configuration from the logging.php
config file and should:
- Create any dependencies the logger needs
- Configure those dependencies based on the config array
- Return a new instance of the logger
The write()
Method
The write()
method is where the actual logging happens:
This method is called by the parent AbstractLogger
class when a log message needs to be written. It receives:
$level
: The log level (debug, info, warning, etc.)$message
: The formatted log message$context
: Additional context data
Using Custom Loggers
Method 1: Configuration-Based Discovery
The simplest way to use a custom logger is to specify the fully-qualified class name in your logging configuration:
When you specify a class
parameter, that class will be used regardless of the driver name.
Method 2: Driver Name Registration
You can register your logger with a driver name, which allows you to reference it using just the driver name:
Then in your configuration:
Method 3: Automatic Discovery
If your logger follows the naming convention {Driver}Logger
and is in one of the registered namespaces, it will be discovered automatically:
The framework will search for RedisLogger
in the registered namespaces (\Ody\Logger\
and \App\Logging\
by default).
Creating Custom Formatters
If the standard formatters don't meet your needs, you can create your own by implementing the FormatterInterface
:
Complete Example: Using Redis Logger
Configuration
Usage
With this system, you can create custom loggers that integrate seamlessly with the ODY Framework logging infrastructure.