{"id":26420274,"url":"https://github.com/ody-dev/logger","last_synced_at":"2026-02-25T07:03:18.497Z","repository":{"id":282552417,"uuid":"948949614","full_name":"ody-dev/Logger","owner":"ody-dev","description":"Logging component for ODY framework","archived":false,"fork":false,"pushed_at":"2025-03-15T10:33:15.000Z","size":16,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-15T11:46:31.008Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ody-dev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-03-15T10:31:01.000Z","updated_at":"2025-03-15T10:33:18.000Z","dependencies_parsed_at":"2025-03-15T11:46:35.934Z","dependency_job_id":"3ba3106d-4bc5-49f5-b785-9301755c9d75","html_url":"https://github.com/ody-dev/Logger","commit_stats":null,"previous_names":["ody-dev/logger"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ody-dev%2FLogger","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ody-dev%2FLogger/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ody-dev%2FLogger/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ody-dev%2FLogger/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ody-dev","download_url":"https://codeload.github.com/ody-dev/Logger/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243924246,"owners_count":20369687,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-03-18T02:01:08.751Z","updated_at":"2026-02-25T07:03:08.485Z","avatar_url":"https://github.com/ody-dev.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Logging\n\nLogging is configured in `config/logging.php` and provides various channels for logging:\n\n```php\n// Using the logger\n$logger-\u003einfo('User logged in', ['id' =\u003e $userId]);\n$logger-\u003eerror('Failed to process payment', ['order_id' =\u003e $orderId]);\n\n// Using the logger helper function\nlogger()-\u003einfo('Processing request');\n```\n\n# Custom Loggers in ODY Framework\n## Creating Custom Loggers\n\n### Basic Requirements\n\nAll custom loggers must:\n\n1. Extend `Ody\\Logger\\AbstractLogger`\n2. Implement a static `create(array $config)` method\n3. Override the `write(string $level, string $message, array $context = [])` method\n\n### Example: Creating a Custom Logger\n\nHere's a simple example of a custom logger that logs to Redis:\n\n```php\n\u003c?php\n\nnamespace App\\Logging;\n\nuse Ody\\Logger\\AbstractLogger;use Ody\\Logger\\Formatters\\FormatterInterface;use Ody\\Logger\\Formatters\\JsonFormatter;use Ody\\Logger\\Formatters\\LineFormatter;use Psr\\Log\\LoggerInterface;use Psr\\Log\\LogLevel;use Redis;\n\nclass RedisLogger extends AbstractLogger\n{\n    /**\n     * @var Redis\n     */\n    protected Redis $redis;\n    \n    /**\n     * @var string\n     */\n    protected string $channel;\n    \n    /**\n     * Constructor\n     */\n    public function __construct(\n        Redis $redis,\n        string $channel = 'logs',\n        string $level = LogLevel::DEBUG,\n        ?FormatterInterface $formatter = null\n    ) {\n        parent::__construct($level, $formatter);\n        $this-\u003eredis = $redis;\n        $this-\u003echannel = $channel;\n    }\n    \n    /**\n     * Create a Redis logger from configuration\n     */\n    public static function create(array $config): LoggerInterface\n    {\n        // Create Redis connection\n        $redis = new Redis();\n        $redis-\u003econnect(\n            $config['host'] ?? '127.0.0.1',\n            $config['port'] ?? 6379\n        );\n        \n        if (isset($config['password'])) {\n            $redis-\u003eauth($config['password']);\n        }\n        \n        // Create formatter\n        $formatter = null;\n        if (isset($config['formatter'])) {\n            $formatter = self::createFormatter($config);\n        }\n        \n        // Return new logger instance\n        return new self(\n            $redis,\n            $config['channel'] ?? 'logs',\n            $config['level'] ?? LogLevel::DEBUG,\n            $formatter\n        );\n    }\n    \n    /**\n     * Create a formatter based on configuration\n     */\n    protected static function createFormatter(array $config): FormatterInterface\n    {\n        $formatterType = $config['formatter'] ?? 'json';\n        \n        if ($formatterType === 'line') {\n            return new LineFormatter(\n                $config['format'] ?? null,\n                $config['date_format'] ?? null\n            );\n        }\n        \n        return new JsonFormatter();\n    }\n    \n    /**\n     * {@inheritdoc}\n     */\n    protected function write(string $level, string $message, array $context = []): void\n    {\n        // Format log data\n        $logData = [\n            'timestamp' =\u003e time(),\n            'level' =\u003e $level,\n            'message' =\u003e $message,\n            'context' =\u003e $context\n        ];\n        \n        // Publish to Redis channel\n        $this-\u003eredis-\u003epublish(\n            $this-\u003echannel,\n            json_encode($logData)\n        );\n    }\n}\n```\n\n### The `create()` Method\n\nThe static `create()` method is responsible for instantiating your logger based on configuration:\n\n```php\npublic static function create(array $config): LoggerInterface\n{\n    // Create dependencies based on configuration\n    // ...\n    \n    // Return new logger instance\n    return new self(...);\n}\n```\n\nThis method receives the channel configuration from the `logging.php` config file and should:\n\n1. Create any dependencies the logger needs\n2. Configure those dependencies based on the config array\n3. Return a new instance of the logger\n\n### The `write()` Method\n\nThe `write()` method is where the actual logging happens:\n\n```php\nprotected function write(string $level, string $message, array $context = []): void\n{\n    // Implement logging logic here\n}\n```\n\nThis method is called by the parent `AbstractLogger` class when a log message needs to be written. It receives:\n\n- `$level`: The log level (debug, info, warning, etc.)\n- `$message`: The formatted log message\n- `$context`: Additional context data\n\n## Using Custom Loggers\n\n### Method 1: Configuration-Based Discovery\n\nThe simplest way to use a custom logger is to specify the fully-qualified class name in your logging configuration:\n\n```php\n// In config/logging.php\n'channels' =\u003e [\n    'redis' =\u003e [\n        'driver' =\u003e 'redis',\n        'class' =\u003e \\App\\Logging\\RedisLogger::class,\n        'host' =\u003e env('REDIS_HOST', '127.0.0.1'),\n        'port' =\u003e env('REDIS_PORT', 6379),\n        'channel' =\u003e 'application_logs',\n        'level' =\u003e 'debug',\n    ],\n]\n```\n\nWhen you specify a `class` parameter, that class will be used regardless of the driver name.\n\n### Method 2: Driver Name Registration\n\nYou can register your logger with a driver name, which allows you to reference it using just the driver name:\n\n```php\n// In a service provider's register method\n$this-\u003eapp-\u003emake(\\Ody\\Logger\\LogManager::class)\n    -\u003eregisterDriver('redis', \\App\\Logging\\RedisLogger::class);\n```\n\nThen in your configuration:\n\n```php\n// In config/logging.php\n'channels' =\u003e [\n    'redis' =\u003e [\n        'driver' =\u003e 'redis', // This will use the registered RedisLogger\n        'host' =\u003e env('REDIS_HOST', '127.0.0.1'),\n        'port' =\u003e env('REDIS_PORT', 6379),\n        'channel' =\u003e 'application_logs',\n        'level' =\u003e 'debug',\n    ],\n]\n```\n\n### Method 3: Automatic Discovery\n\nIf your logger follows the naming convention `{Driver}Logger` and is in one of the registered namespaces, it will be discovered automatically:\n\n```php\n// In config/logging.php\n'channels' =\u003e [\n    'redis' =\u003e [\n        'driver' =\u003e 'redis', // Will look for RedisLogger\n        // Configuration...\n    ],\n]\n```\n\nThe framework will search for `RedisLogger` in the registered namespaces (`\\Ody\\Logger\\` and `\\App\\Logging\\` by default).\n\n### Creating Custom Formatters\n\nIf the standard formatters don't meet your needs, you can create your own by implementing the `FormatterInterface`:\n\n```php\nnamespace App\\Logging;\n\nuse Ody\\Logger\\Formatters\\FormatterInterface;\n\nclass CustomFormatter implements FormatterInterface\n{\n    public function format(string $level, string $message, array $context = []): string\n    {\n        // Custom formatting logic\n        return \"[$level] $message \" . json_encode($context);\n    }\n}\n```\n\n## Complete Example: Using Redis Logger\n\n### Configuration\n\n```php\n// In config/logging.php\n'channels' =\u003e [\n    // Using explicit class\n    'redis' =\u003e [\n        'driver' =\u003e 'redis',\n        'class' =\u003e \\App\\Logging\\RedisLogger::class,\n        'host' =\u003e env('REDIS_HOST', '127.0.0.1'),\n        'port' =\u003e env('REDIS_PORT', 6379),\n        'password' =\u003e env('REDIS_PASSWORD', null),\n        'channel' =\u003e 'app_logs',\n        'formatter' =\u003e 'json',\n        'level' =\u003e 'debug',\n    ],\n    \n    // Using it in a stack\n    'production' =\u003e [\n        'driver' =\u003e 'group',\n        'channels' =\u003e ['file', 'redis'],\n    ],\n],\n```\n\n### Usage\n\n```php\n// Send to redis channel\nlogger('User registered', ['id' =\u003e 123], 'redis');\n\n// Or use the stack\nlogger('API request processed', ['endpoint' =\u003e '/users']);\n```\n---\n\nWith this system, you can create custom loggers that integrate seamlessly with the ODY Framework logging infrastructure.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fody-dev%2Flogger","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fody-dev%2Flogger","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fody-dev%2Flogger/lists"}