|
1 | 1 | # KaririCode Framework: Logging Component |
2 | | - |
3 | | -[](README.md) |
4 | | -[](README.pt-br.md) |
5 | | - |
6 | | - |
7 | | - |
8 | | - |
9 | | - |
10 | | - |
11 | | -## Overview |
12 | | - |
13 | | -The Logging Component is a crucial part of the KaririCode Framework, providing a robust, flexible, and PSR-3 compliant logging system. This component offers a comprehensive set of features for handling various logging needs, from simple application logging to complex, distributed system monitoring. |
14 | | - |
15 | | -## Key Features |
16 | | - |
17 | | -- **PSR-3 Compliance**: Fully implements the PSR-3 Logger Interface for seamless integration with other PSR-3 compatible systems. |
18 | | -- **Flexible Configuration**: Easily configurable through a `LoggerConfig` class, allowing for fine-tuned control over logging behavior. |
19 | | -- **Multiple Handlers**: Supports various output handlers, including stream and rotating file handlers. |
20 | | -- **Log Levels**: Implements all standard log levels as defined in PSR-3 (DEBUG, INFO, NOTICE, WARNING, ERROR, CRITICAL, ALERT, EMERGENCY). |
21 | | -- **Contextual Logging**: Supports adding context to log messages for more detailed debugging. |
22 | | -- **Processors**: Allows for the modification and enrichment of log records before they are written. |
23 | | -- **Formatters**: Customizable log message formatting. |
24 | | -- **Asynchronous Logging**: Optional asynchronous logging support for improved performance in high-load scenarios. |
25 | | -- **Log Rotation**: Built-in support for log file rotation to manage log file sizes. |
26 | | - |
27 | | -## Available Interfaces and Classes |
28 | | - |
29 | | -### Core Interfaces |
30 | | - |
31 | | -- `Message`: Defines the contract for log messages. |
32 | | -- `Handler`: Defines the contract for log handlers. |
33 | | -- `Processor`: Defines the contract for log processors. |
34 | | -- `Formatter`: Defines the contract for log formatters. |
35 | | -- `Rotator`: Defines the contract for log file rotators. |
36 | | - |
37 | | -### Main Classes |
38 | | - |
39 | | -- `Logger`: The main logger class implementing PSR-3 LoggerInterface. |
40 | | -- `LoggerConfig`: Configuration class for the logger. |
41 | | -- `LogRecord`: Represents a single log record. |
42 | | -- `Level`: Enum class representing log levels. |
43 | | - |
44 | | -### Handlers |
45 | | - |
46 | | -- `StreamHandler`: Writes log records to any PHP stream (e.g., file, stderr). |
47 | | -- `RotatingFileHandler`: Writes log records to a file, with support for log rotation. |
48 | | - |
49 | | -### Formatters |
50 | | - |
51 | | -- `DefaultFormatter`: Provides a default formatting for log records. |
52 | | - |
53 | | -### Processors |
54 | | - |
55 | | -- `ContextProcessor`: Adds additional context (hostname, PID, memory usage) to log records. |
56 | | -- `ExceptionProcessor`: Formats exception information in log records. |
57 | | - |
58 | | -### Rotators |
59 | | - |
60 | | -- `DailyRotator`: Rotates log files on a daily basis. |
61 | | - |
62 | | -## Installation |
63 | | - |
64 | | -### Requirements |
65 | | - |
66 | | -- PHP 8.3 or higher |
67 | | -- Composer |
68 | | - |
69 | | -### Via Composer |
70 | | - |
71 | | -```bash |
72 | | -composer require kariricode/logging |
73 | | -``` |
74 | | - |
75 | | -## Usage |
76 | | - |
77 | | -### Basic Usage |
78 | | - |
79 | | -Here's a basic example of how to use the Logging Component: |
80 | | - |
81 | | -```php |
82 | | -use KaririCode\Logging\Logger; |
83 | | -use KaririCode\Logging\LoggerConfig; |
84 | | -use KaririCode\Logging\Level; |
85 | | -use KaririCode\Logging\Handler\RotatingFileHandler; |
86 | | -use KaririCode\Logging\Handler\StreamHandler; |
87 | | -use KaririCode\Logging\Processor\ContextProcessor; |
88 | | -use KaririCode\Logging\Processor\ExceptionProcessor; |
89 | | - |
90 | | -$config = new LoggerConfig([ |
91 | | - 'async' => true, |
92 | | - 'rotate' => true, |
93 | | - 'file_path' => '/var/log/myapp.log', |
94 | | - 'minimum_level' => 'info', |
95 | | -]); |
96 | | - |
97 | | -$logger = new Logger('app', $config); |
98 | | -$logger->addHandler(new RotatingFileHandler('/var/log/application.log', 7, Level::DEBUG)) |
99 | | - ->addHandler(new StreamHandler('php://stderr', Level::ERROR)) |
100 | | - ->addProcessor(new ContextProcessor()) |
101 | | - ->addProcessor(new ExceptionProcessor()); |
102 | | - |
103 | | -$logger->info('Application started'); |
104 | | -$logger->error('Critical error', ['exception' => new \Exception('Exception details')]); |
105 | | -``` |
106 | | - |
107 | | -### Dependency Injection and Advanced Usage |
108 | | - |
109 | | -The Logging Component is designed to work seamlessly with dependency injection containers. Here's an example of how to use it with a simple DI container and in a more complex application structure: |
110 | | - |
111 | | -```php |
112 | | -use Psr\Container\ContainerInterface; |
113 | | -use Psr\Log\LoggerInterface; |
114 | | -use KaririCode\Logging\Logger; |
115 | | -use KaririCode\Logging\LoggerConfig; |
116 | | -use KaririCode\Logging\Handler\RotatingFileHandler; |
117 | | -use KaririCode\Logging\Level; |
118 | | - |
119 | | -class Container implements ContainerInterface |
120 | | -{ |
121 | | - private array $services = []; |
122 | | - |
123 | | - public function get($id) |
124 | | - { |
125 | | - if (!$this->has($id)) { |
126 | | - throw new \Exception("Service not found: $id"); |
127 | | - } |
128 | | - return $this->services[$id]($this); |
129 | | - } |
130 | | - |
131 | | - public function has($id): bool |
132 | | - { |
133 | | - return isset($this->services[$id]); |
134 | | - } |
135 | | - |
136 | | - public function set($id, callable $factory): void |
137 | | - { |
138 | | - $this->services[$id] = $factory; |
139 | | - } |
140 | | -} |
141 | | - |
142 | | -// Set up the container |
143 | | -$container = new Container(); |
144 | | - |
145 | | -// Configure the logger |
146 | | -$container->set(LoggerInterface::class, function (ContainerInterface $container) { |
147 | | - $config = new LoggerConfig([ |
148 | | - 'async' => true, |
149 | | - 'rotate' => true, |
150 | | - 'file_path' => '/var/log/myapp.log', |
151 | | - 'minimum_level' => 'info', |
152 | | - ]); |
153 | | - |
154 | | - $logger = new Logger('app', $config); |
155 | | - $logger->addHandler(new RotatingFileHandler('/var/log/application.log', 7, Level::DEBUG)); |
156 | | - return $logger; |
157 | | -}); |
158 | | - |
159 | | -// Example service that uses the logger |
160 | | -class UserService |
161 | | -{ |
162 | | - private LoggerInterface $logger; |
163 | | - |
164 | | - public function __construct(LoggerInterface $logger) |
165 | | - { |
166 | | - $this->logger = $logger; |
167 | | - } |
168 | | - |
169 | | - public function registerUser(string $username): void |
170 | | - { |
171 | | - // User registration logic here |
172 | | - $this->logger->info('New user registered', ['username' => $username]); |
173 | | - } |
174 | | -} |
175 | | - |
176 | | -// Register the UserService in the container |
177 | | -$container->set(UserService::class, function (ContainerInterface $container) { |
178 | | - return new UserService($container->get(LoggerInterface::class)); |
179 | | -}); |
180 | | - |
181 | | -// Usage in application code |
182 | | -$userService = $container->get(UserService::class); |
183 | | -$userService->registerUser('john_doe'); |
184 | | -``` |
185 | | - |
186 | | -In this example: |
187 | | - |
188 | | -1. We define a simple `Container` class that implements the PSR-11 `ContainerInterface`. |
189 | | -2. We configure the logger in the container, making it available as a service. |
190 | | -3. We create a `UserService` class that depends on a `LoggerInterface`. |
191 | | -4. We register the `UserService` in the container, injecting the logger. |
192 | | -5. Finally, we retrieve the `UserService` from the container and use it in our application code. |
193 | | - |
194 | | -This approach allows for better separation of concerns, easier testing, and more flexible configuration of your logging setup across your entire application. |
195 | | - |
196 | | -## License |
197 | | - |
198 | | -This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
199 | | - |
200 | | -## Support and Community |
201 | | - |
202 | | -- **Documentation**: [https://kariricode.org](https://kariricode.org) |
203 | | -- **Issue Tracker**: [GitHub Issues](https://github.com/KaririCode-Framework/kariricode-logging/issues) |
204 | | -- **Community**: [KaririCode Club Community](https://kariricode.club) |
205 | | -- **Professional Support**: For enterprise-level support, contact us at support@kariricode.org |
206 | | - |
207 | | -## Acknowledgments |
208 | | - |
209 | | -- The KaririCode Framework team and contributors. |
210 | | -- The PHP community for their continued support and inspiration. |
211 | | - |
212 | | ---- |
213 | | - |
214 | | -Built with ❤️ by the KaririCode team. Empowering developers to build more robust and flexible PHP applications. |
215 | | - |
216 | | -Maintained by Walmir Silva - [walmir.silva@kariricode.org](mailto:walmir.silva@kariricode.org) |
0 commit comments