{"id":27158883,"url":"https://github.com/activecollab/logger","last_synced_at":"2025-10-06T14:51:43.943Z","repository":{"id":56940247,"uuid":"62344728","full_name":"activecollab/logger","owner":"activecollab","description":"A set of logging conventions that are common to our apps, built around PSR-3 LoggerInterface","archived":false,"fork":false,"pushed_at":"2021-02-20T23:41:18.000Z","size":92,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-30T13:03:28.951Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://www.activecollab.com/index.html","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/activecollab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-06-30T21:56:20.000Z","updated_at":"2021-02-20T23:40:47.000Z","dependencies_parsed_at":"2022-08-21T01:40:30.003Z","dependency_job_id":null,"html_url":"https://github.com/activecollab/logger","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/activecollab%2Flogger","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/activecollab%2Flogger/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/activecollab%2Flogger/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/activecollab%2Flogger/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/activecollab","download_url":"https://codeload.github.com/activecollab/logger/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247941718,"owners_count":21022035,"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-04-08T22:39:17.881Z","updated_at":"2025-10-06T14:51:38.902Z","avatar_url":"https://github.com/activecollab.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Logger\n\n[![Build Status](https://travis-ci.org/activecollab/logger.svg?branch=master)](https://travis-ci.org/activecollab/logger)\n\nThis package implements some of our internal conventions on top of PSR-3. Logger that it publishes is fully PSR-3 comptabile with some extra functionality (optional), as well as a factory that makes logger creation easy:\n \n```php\n$factory = new LoggerFactory();\n$logger = $factory-\u003ecreate('Active Collab', '1.0.0', 'development', LoggerInterface::LOG_FOR_DEBUG, LoggerInterface::FILE, '/path/to/logs/dir');\n```\n\nOnce logger is set, you can use it like any other PSR-3 logger:\n\n```php\n$logger-\u003einfo('Something interesting happened: {what}', [\n    'what' =\u003e 'really interesting event'\n]);\n```\n\nSpecial loggers are:\n\n1. Event logging using `LoggerInterface::event()` method. This will log a named event on info level, and set event context attribute to event name,\n1. Request summary logging using `LoggerInterface::requestSummary()` method. This will log some interesting request data, like executing time, total queries and query count etc.\n\n## Loggers\n\nPackages comes with following backends implemented:\n\n`LoggerInterface::FILE` - Log to files in log directory. Log directory is required as first logger argument when creating a logger:\n\n```php\n$logger = $factory-\u003ecreate('Active Collab', '1.0.0', 'development', LoggerInterface::LOG_FOR_DEBUG, LoggerInterface::FILE, '/path/to/logs/dir', 'my-awesome-logs.txt', 0777);\n```\n\nSecond argument is log file name, and it is optional. When skipped, system will log to `log.txt` file in the specified folder.\nThird argument is file permissions level. Default is 0644 when skipped, but you can specify any value (in octal notation).\n\nNote that we set rotating file logging, where only past 7 days of logs are kept.\n\n`LoggerInterface::GRAYLOG` - Log messages are sent to Graylog2 server using GELF formatter. Additional arguments are Graylog2 server host and port. If they are skipped, 127.0.0.1 and are used:\n\n```php\n$logger = $factory-\u003ecreate('Active Collab', '1.0.0', 'development', LoggerInterface::LOG_FOR_DEBUG, LoggerInterface::GRAYLOG, 'graylog.company.com', 12201);\n```\n\n`LoggerInterface::BLACKHOLE` - Messages are not logged anywhere.\n\n## Message Buffering\n\nLogger is built to buffer messages until request details are set (using `setAppRequest()` method). Reason why we delay writing to log is to be able to add request details to all messages, so we can connect the dots alter on:\n\n```php\n// Set HTTP request from PSR-7 ServerRequestInterface\n$logger-\u003esetAppRequest(new \\ActiveCollab\\Logger\\AppRequest\\HttpRequest($request));\n\n// Set CLI request from arguments\n$logger-\u003esetAppRequest(new \\ActiveCollab\\Logger\\AppRequest\\CliRequest('session ID', $_SERVER['argv']));\n```\n\nIf request is not set, buffer will not be flushed unless you flush it yourself, or register a shutdown function:\n\n```php\n$logger-\u003eflushBufferOnShutdown();\n```\n\n## Application Details\n\nThis package always logs application name, version and environemnt. These arguments are required and they need to be provided to `FactoryInterface::create()` method, when creating new logger instance:\n\n```php\n$logger = $factory-\u003ecreate('Active Collab', '1.0.0', 'development', LoggerInterface::LOG_FOR_DEBUG, LoggerInterface::FILE, '/path/to/logs/dir');\n```\n\nEnvironment arguments are sent as context arguments with all messages captured via logger instance. User can specify additional environment arguments, using `FactoryInterface::setAdditionalEnvArguments()` method:\n\n```php\n// Additional environment arguments can be set on factory level, and factory will pass them to all loggers that it produces\n$factory-\u003esetAdditionalEnvArguments([\n    'account_id' =\u003e 123,\n    'extra_argument' =\u003e 'with extra value',\n]);\n\n// Or you can specify them on logger level\n$logger-\u003esetAdditionalEnvArguments([\n    'account_id' =\u003e 123,\n    'extra_argument' =\u003e 'with extra value',\n]);\n```\n\n## Exception Serialization\n\nWhen exceptions are passed as context arguments, package will \"explode\" them to a group of relevant arguments: message, file, line, code, and trace. Previous exception is also extracted, when available:\n\n```php\ntry {\n    // Something risky\n} catch (ExceptioN $e) {\n    $logger-\u003eerror('An {exception} happened :(', [\n        'exception' =\u003e $e,\n    ]);\n}\n```\n\nIf you have special exceptions that collect more info than message, code, file, line, trace and previous, you can register a callback that will extract that data as well:\n\n```php\n$logger-\u003eaddExceptionSerializer(function ($argument_name, $exception, array \u0026$context) {\n    if ($exception instanceof \\SpecialError) {\n        foreach ($exception-\u003egetParams() as $k =\u003e $v) {\n            $context[\"{$argument_name}_extra_param_{$k}\"] = $v;\n        }\n    }\n});\n```\n\nCallback gets three arguments:\n\n1. `$argument_name` - contenxt argument name under which we found the exception,\n1. `$exception_name` - exception itself,\n1. `$context` - access to resulting log message context arguments.\n\nAs with additional environment variables, exception serializers can be added to factory, and factory will pass it on to all loggers that it produces:\n\n```php\n$factory-\u003eaddExceptionSerializer(function ($argument_name, $exception, array \u0026$context) {\n    if ($exception instanceof \\SpecialError) {\n        foreach ($exception-\u003egetParams() as $k =\u003e $v) {\n            $context[\"{$argument_name}_extra_param_{$k}\"] = $v;\n        }\n    }\n});\n```\n\n## Error Handling\n\nLogger comes equiped with a class that can register error and exception handlers and direct them to the log. Quick setup:\n\n```php\n$handler = new ErrorHandler($logger);\n$handler-\u003einitialize();\n```\n\nTo restore error and exception handled, simply call `restore()` method:\n\n```php\n$handler-\u003erestore();\n```\n\nHandler can be configured to do different things for diffent error levels. For example, you can configure it to throw an exception on PHP warning, or to silence an event all together:\n\n```php\n$handler-\u003esetHowToHandleError(E_STRICT, ErrorHandlerInterface::SILENCE);\n$handler-\u003esetHowToHandleError(E_DEPRECATED, ErrorHandlerInterface::LOG_NOTICE);\n$handler-\u003esetHowToHandleError(E_NOTICE, ErrorHandlerInterface::LOG_ERROR);\n$handler-\u003esetHowToHandleError(E_USER_ERROR, ErrorHandlerInterface::THROW_EXCEPTION);\n```\n\nBy default, exceptions are logged and re-thrown. This behavior can be turned off:\n\n```php\n$handler-\u003esetReThrowException(false);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Factivecollab%2Flogger","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Factivecollab%2Flogger","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Factivecollab%2Flogger/lists"}