Skip to content

Commit 3a13adb

Browse files
authored
Add logging interfaces and corresponding tests (#19)
* add size method * feat: Normalize Interfaces - Refined and standardized interface definitions across the project - Applied Single Responsibility Principle (SRP) to ensure each interface has a clear, singular purpose - Consistent naming conventions were implemented for better readability and maintainability - Added thorough documentation for each interface, including method descriptions and expected behaviors - Organized interfaces within appropriate namespaces to prevent naming collisions and maintain a logical structure - Ensured parameter names in interface methods are consistent with implementing classes - Avoided including constructors in interfaces to maintain flexibility These changes enhance the overall clarity, maintainability, and professional quality of the codebase. * Refactor and Normalize Collection Interface and Implementation - Refactored `Collection` interface for consistency and adherence to best practices. - Ensured all tests are comprehensive and validate the expected behavior. * feat: Add BinaryHeap implementation and comprehensive tests - Implemented BinaryHeap class with support for both min-heap and max-heap operations. - Methods include: add, poll, peek, size, isEmpty, heapifyUp, heapifyDown, swap, and compare. - Created unit tests for BinaryHeap class ensuring 100% coverage. - Tested all key operations: add, poll, peek, heapifyUp, heapifyDown, swap, and compare. - Included edge case tests for min-heap and max-heap scenarios. - Ensured compliance with type safety and PHP 8.0+ features. - Added comprehensive documentation and examples for BinaryHeap usage. This commit enhances the KaririCode Framework by providing a robust, type-safe, and efficient BinaryHeap data structure with extensive unit tests. * docs: Update README files in English and Portuguese - Enhanced the overview and key features sections for clarity and completeness. - Added detailed descriptions for all implemented data structures: - TreeSet - ArrayDeque - ArrayQueue - TreeMap - LinkedList - BinaryHeap - HashMap - Included complexity analysis and key methods for each data structure. - Provided usage examples for each data structure to demonstrate functionality. - Updated installation instructions and requirements. - Updated acknowledgments and roadmap sections. * style: Normalize code formatting and style * feat: Create Container interface and add unit tests - Define the Container interface with methods: `set`, `get`, and `has`. - Implement unit tests for `set` method to ensure services are registered correctly. - Add unit tests for `get` method to verify correct retrieval of services by identifier. - Include unit tests for `has` method to check existence of services in the container. - Normalize the `@category` comments in the interface documentation for consistency. These changes establish a foundational contract for dependency injection within the KaririCode project and ensure robust and reliable functionality through comprehensive testing * feat: Add logging interfaces and corresponding tests - Created Configurable interface for managing configuration settings. - Created Loggable interface to define logging methods for various log levels. - Created ContextAware interface for managing context information in logs. - Created FormatterAware interface for handling log formatter settings. - Created HandlerAware interface for managing log handlers. - Created ProcessorAware interface for managing log processors. - Created LoggingManager interface that combines various logging functionalities. - Created LogFormatter interface for defining log formatters. - Created LogHandler interface for handling log records. - Created LogLevel interface for defining log levels. - Created LogProcessor interface for processing log records before handling. - Created LogRotator interface for managing log file rotation. - Added unit tests for each interface to ensure correct functionality: - ConfigurableTest: Tests for setConfig and getConfig methods. - LoggableTest: Tests for various logging level methods. - ContextAwareTest: Tests for withContext and getContext methods. - FormatterAwareTest: Tests for setFormatter and getFormatter methods. - HandlerAwareTest: Tests for addHandler, pushHandler, popHandler, and getHandlers methods. - ProcessorAwareTest: Tests for addProcessor and getProcessors methods. - LoggingManagerTest: Comprehensive tests for all LoggingManager methods. - LogFormatterTest: Test for the format method. - LogHandlerTest: Test for the handle method. - LogLevelTest: Test for the getLevel method. - LogProcessorTest: Test for the process method. - LogRotatorTest: Test for the rotate method.
1 parent 5a6cc89 commit 3a13adb

25 files changed

+1208
-0
lines changed

src/ImmutableValue.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\Contract;
6+
7+
/**
8+
* ImmutableValue interface.
9+
*
10+
* This interface is used to mark a class as an immutable value object.
11+
*
12+
* @category Logging
13+
*
14+
* @author Walmir Silva <walmir.silva@kariricode.org>
15+
* @license MIT
16+
*
17+
* @see https://kariricode.org/
18+
*/
19+
interface ImmutableValue
20+
{
21+
// No methods needed; this interface serves as a marker.
22+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\Contract\Logging\Behavioral;
6+
7+
use KaririCode\Contract\ImmutableValue;
8+
9+
/**
10+
* Configurable interface.
11+
*
12+
* This interface defines methods for managing configuration.
13+
* Configuration settings can affect the behavior of logging.
14+
*
15+
* Obs.:
16+
* ```
17+
* class LoggerConfig implements ImmutableValue {
18+
* // Implementation details
19+
* }
20+
* ```
21+
*
22+
* @category Logging
23+
*
24+
* @author Walmir Silva <walmir.silva@kariricode.org>
25+
* @license MIT
26+
*
27+
* @see https://kariricode.org/
28+
*/
29+
interface Configurable
30+
{
31+
/**
32+
* Retrieves the current configuration.
33+
*
34+
* @return ImmutableValue the current configuration object
35+
*/
36+
public function getConfig(): ImmutableValue;
37+
38+
/**
39+
* Sets a new configuration.
40+
*
41+
* @param ImmutableValue $config the new configuration object
42+
*
43+
* @return Configurable the current instance for method chaining
44+
*/
45+
public function setConfig(ImmutableValue $config): Configurable;
46+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\Contract\Logging\Behavioral;
6+
7+
use KaririCode\Contract\Logging\LogLevel;
8+
9+
/**
10+
* Loggable interface.
11+
*
12+
* This interface defines the logging methods for various log levels.
13+
* Implementers can log messages at different severity levels.
14+
*
15+
* @category Logging
16+
*
17+
* @author Walmir Silva <walmir.silva@kariricode.org>
18+
* @license MIT
19+
*
20+
* @see https://kariricode.org/
21+
*/
22+
interface Loggable
23+
{
24+
/**
25+
* Logs with an arbitrary level.
26+
*
27+
* @param LogLevel $level the log level
28+
* @param string|\Stringable $message the log message
29+
* @param array $context additional context for the log message
30+
*/
31+
public function log(LogLevel $level, string|\Stringable $message, array $context = []): void;
32+
33+
/**
34+
* Logs a debug message.
35+
*
36+
* @param string|\Stringable $message the log message
37+
* @param array $context additional context for the log message
38+
*/
39+
public function debug(string|\Stringable $message, array $context = []): void;
40+
41+
/**
42+
* Logs an info message.
43+
*
44+
* @param string|\Stringable $message the log message
45+
* @param array $context additional context for the log message
46+
*/
47+
public function info(string|\Stringable $message, array $context = []): void;
48+
49+
/**
50+
* Logs a notice message.
51+
*
52+
* @param string|\Stringable $message the log message
53+
* @param array $context additional context for the log message
54+
*/
55+
public function notice(string|\Stringable $message, array $context = []): void;
56+
57+
/**
58+
* Logs a warning message.
59+
*
60+
* @param string|\Stringable $message the log message
61+
* @param array $context additional context for the log message
62+
*/
63+
public function warning(string|\Stringable $message, array $context = []): void;
64+
65+
/**
66+
* Logs an error message.
67+
*
68+
* @param string|\Stringable $message the log message
69+
* @param array $context additional context for the log message
70+
*/
71+
public function error(string|\Stringable $message, array $context = []): void;
72+
73+
/**
74+
* Logs a critical message.
75+
*
76+
* @param string|\Stringable $message the log message
77+
* @param array $context additional context for the log message
78+
*/
79+
public function critical(string|\Stringable $message, array $context = []): void;
80+
81+
/**
82+
* Logs an alert message.
83+
*
84+
* @param string|\Stringable $message the log message
85+
* @param array $context additional context for the log message
86+
*/
87+
public function alert(string|\Stringable $message, array $context = []): void;
88+
89+
/**
90+
* Logs an emergency message.
91+
*
92+
* @param string|\Stringable $message the log message
93+
* @param array $context additional context for the log message
94+
*/
95+
public function emergency(string|\Stringable $message, array $context = []): void;
96+
}

src/Logging/LogFormatter.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\Contract\Logging;
6+
7+
use KaririCode\Contract\ImmutableValue;
8+
9+
/**
10+
* Interface Formatter.
11+
*
12+
* Defines the contract for log formatters.
13+
* Log formatters are responsible for formatting log records into a specific string format.
14+
*
15+
* @category Logging
16+
*
17+
* @author Walmir Silva <walmir.silva@kariricode.org>
18+
* @license MIT
19+
*
20+
* @see https://kariricode.org/
21+
*/
22+
interface LogFormatter
23+
{
24+
/**
25+
* Formats a log record.
26+
*
27+
* This method is responsible for formatting the log record into a specific string format.
28+
*
29+
* @param ImmutableValue $record the log record to be formatted
30+
*
31+
* @return string the formatted log record as a string
32+
*/
33+
public function format(ImmutableValue $record): string;
34+
}

src/Logging/LogHandler.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\Contract\Logging;
6+
7+
use KaririCode\Contract\ImmutableValue;
8+
9+
/**
10+
* Interface LogHandler.
11+
*
12+
* Defines the contract for log handlers.
13+
* Log handlers are responsible for processing log records and implementing specific
14+
* logging behavior, such as writing log entries to files, sending them to remote servers,
15+
* or displaying them on the console.
16+
*
17+
* Implementations of this interface should focus on the `handle` method, which
18+
* processes a single log record represented as an array.
19+
*
20+
* @category Logging
21+
*
22+
* @license MIT License
23+
*
24+
* @see https://kariricode.org/
25+
*/
26+
interface LogHandler
27+
{
28+
/**
29+
* Processes a log record.
30+
*
31+
* This method is responsible for handling the log record, performing tasks such as
32+
* writing it to a file, sending it over the network, or any other logging behavior.
33+
*
34+
* @param array $record the log record to be processed, represented as an associative array
35+
*/
36+
public function handle(ImmutableValue $record): void;
37+
}

src/Logging/LogLevel.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\Contract\Logging;
6+
7+
/**
8+
* Interface LogLevel.
9+
*
10+
* Defines the contract for log levels.
11+
* Log levels indicate the severity or importance of a log record.
12+
*
13+
* @category Logging
14+
*
15+
* @author Walmir Silva <walmir.silva@kariricode.org>
16+
* @license MIT
17+
*
18+
* @see https://kariricode.org/
19+
*/
20+
interface LogLevel
21+
{
22+
/**
23+
* Gets the log level as a string.
24+
*
25+
* @return string the log level
26+
*/
27+
public function getLevel(): string;
28+
}

src/Logging/LogProcessor.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\Contract\Logging;
6+
7+
/**
8+
* Interface LogProcessor.
9+
*
10+
* Defines the contract for log processors.
11+
* Log processors are responsible for processing log records before they are handled.
12+
* This can include adding additional information, modifying existing information,
13+
* or filtering out certain log records.
14+
*
15+
* @category Logging
16+
*
17+
* @author Walmir Silva <walmir.silva@kariricode.org>
18+
* @license MIT
19+
*
20+
* @see https://kariricode.org/
21+
*/
22+
interface LogProcessor
23+
{
24+
/**
25+
* Processes a log record.
26+
*
27+
* This method is responsible for processing the log record, performing tasks such as
28+
* adding additional information, modifying existing information, or filtering out
29+
* certain log records.
30+
*
31+
* @param array $record the log record to be processed, represented as an associative array
32+
*
33+
* @return array the processed log record
34+
*/
35+
public function process(array $record): array;
36+
}

src/Logging/LogRotator.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\Contract\Logging;
6+
7+
/**
8+
* Interface Rotator.
9+
*
10+
* Defines the contract for log rotators.
11+
* Log rotators are responsible for rotating log files, ensuring that logs do not
12+
* grow indefinitely and are managed efficiently.
13+
*
14+
* @category Logging
15+
*
16+
* @author Walmir Silva <walmir.silva@kariricode.org>
17+
* @license MIT
18+
*
19+
* @see https://kariricode.org/
20+
*/
21+
interface LogRotator
22+
{
23+
/**
24+
* Rotates a log file.
25+
*
26+
* This method is responsible for rotating the specified log file, such as renaming
27+
* it and creating a new file for future log entries.
28+
*
29+
* @param string $filename the name of the log file to be rotated
30+
*/
31+
public function rotate(string $filename): void;
32+
}

src/Logging/LoggingManager.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\Contract\Logging;
6+
7+
use KaririCode\Contract\Logging\Behavioral\Configurable;
8+
use KaririCode\Contract\Logging\Behavioral\Loggable;
9+
use KaririCode\Contract\Logging\Structural\ContextAware;
10+
use KaririCode\Contract\Logging\Structural\FormatterAware;
11+
use KaririCode\Contract\Logging\Structural\HandlerAware;
12+
use KaririCode\Contract\Logging\Structural\ProcessorAware;
13+
14+
/**
15+
* Interface LoggingManager.
16+
*
17+
* Combines various logging functionalities, including logging methods, handler management,
18+
* processor management, formatter management, context management, and configuration.
19+
* This interface serves as a comprehensive manager for all aspects of logging.
20+
*
21+
* @category Logging
22+
*
23+
* @author Walmir Silva <walmir.silva@kariricode.org>
24+
* @license MIT
25+
*
26+
* @see https://kariricode.org/
27+
*/
28+
interface LoggingManager extends
29+
Loggable,
30+
HandlerAware,
31+
ProcessorAware,
32+
FormatterAware,
33+
ContextAware,
34+
Configurable
35+
{
36+
/**
37+
* Gets the name of the logger.
38+
*
39+
* @return string the name of the logger
40+
*/
41+
public function getName(): string;
42+
}

0 commit comments

Comments
 (0)