|
| 1 | +# Logging System - Chain of Responsibility Design Pattern |
| 2 | + |
| 3 | +## Overview |
| 4 | +This project implements a **Logging System** using the **Chain of Responsibility** design pattern in TypeScript. The Chain of Responsibility pattern allows multiple objects to process a request in a sequential manner, passing the request along the chain if necessary. |
| 5 | + |
| 6 | +## **Flow of Execution** |
| 7 | +1. **Base Logger Class (`Logger.ts`)**: |
| 8 | + - Defines the logging levels: `INFO`, `DEBUG`, `ERROR`, and `WARNING`. |
| 9 | + - Implements a `setNext` method to pass requests down the chain. |
| 10 | + |
| 11 | +2. **Concrete Loggers**: |
| 12 | + - `InfoLogger.ts`: Handles `INFO` level logs. |
| 13 | + - `DebugLogger.ts`: Handles `DEBUG` level logs. |
| 14 | + - `ErrorLogger.ts`: Handles `ERROR` level logs. |
| 15 | + - `WarningLogger.ts`: Handles `WARNING` level logs. |
| 16 | + - If a logger cannot handle the request, it forwards it to the next logger in the chain. |
| 17 | + |
| 18 | +3. **Setting Up the Chain (`index.ts`)**: |
| 19 | + - Loggers are chained together: `InfoLogger -> DebugLogger -> ErrorLogger -> WarningLogger`. |
| 20 | + - When a log request is made, it is passed through the chain until a matching logger processes it. |
| 21 | + |
| 22 | +## **Code Example** |
| 23 | +### Logger Class (Base Abstract Class) |
| 24 | +```typescript |
| 25 | +abstract class Logger { |
| 26 | + static INFO = 1; |
| 27 | + static DEBUG = 2; |
| 28 | + static ERROR = 3; |
| 29 | + static WARNING = 4; |
| 30 | + |
| 31 | + nextLogger: Logger | null; |
| 32 | + constructor(nextLogger: Logger | null = null) { |
| 33 | + this.nextLogger = nextLogger; |
| 34 | + } |
| 35 | + |
| 36 | + abstract log(logLevel: number, message: string): void; |
| 37 | + |
| 38 | + setNext(logLevel: number, message: string) { |
| 39 | + this.nextLogger?.log(logLevel, message); |
| 40 | + } |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +### **Concrete Logger Implementation** |
| 45 | +```typescript |
| 46 | +import { Logger } from "./Logger"; |
| 47 | + |
| 48 | +class InfoLogger extends Logger { |
| 49 | + constructor(nextLogger: Logger | null = null) { |
| 50 | + super(nextLogger); |
| 51 | + } |
| 52 | + |
| 53 | + log(logLevel: number, message: string) { |
| 54 | + if (logLevel === Logger.INFO) { |
| 55 | + console.log(`INFO : ${message}`); |
| 56 | + } else { |
| 57 | + super.setNext(logLevel, message); |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +export { InfoLogger }; |
| 63 | +``` |
| 64 | + |
| 65 | +### **Index File - Setting Up the Chain** |
| 66 | +```typescript |
| 67 | +import { Logger } from "./Logger"; |
| 68 | +import { InfoLogger } from "./InfoLogger"; |
| 69 | +import { DebugLogger } from "./DebugLogger"; |
| 70 | +import { ErrorLogger } from "./ErrorLogger"; |
| 71 | +import { WarningLogger } from "./WarningLogger"; |
| 72 | + |
| 73 | +const loggerObj: Logger = new InfoLogger( |
| 74 | + new DebugLogger( |
| 75 | + new ErrorLogger( |
| 76 | + new WarningLogger() |
| 77 | + ) |
| 78 | + ) |
| 79 | +); |
| 80 | + |
| 81 | +loggerObj.log(Logger.DEBUG, "ITS DEBUG VALUE"); |
| 82 | +loggerObj.log(Logger.INFO, "HEY THIS IS INFORMATION"); |
| 83 | +loggerObj.log(Logger.ERROR, "EXCEPTION CAUGHT"); |
| 84 | +loggerObj.log(Logger.WARNING, "ITS WARNING ON RENDERING"); |
| 85 | +``` |
| 86 | + |
| 87 | +## **Benefits of the Chain of Responsibility Pattern** |
| 88 | +- **Decouples sender and receiver**: The client doesn’t need to know which logger will handle the request. |
| 89 | +- **Flexible and Scalable**: New log levels can be added without modifying existing code. |
| 90 | +- **Improved Maintainability**: Each logger has a single responsibility, making debugging easier. |
| 91 | + |
| 92 | +## **When to Use?** |
| 93 | +- When multiple handlers should process a request in sequence. |
| 94 | +- When the handler for a request isn’t known beforehand and should be determined dynamically. |
| 95 | +- When it is necessary to avoid tight coupling between the request sender and the processing objects. |
| 96 | + |
| 97 | +## **Conclusion** |
| 98 | +The Chain of Responsibility pattern is a powerful way to handle requests dynamically. This logging system ensures flexibility, maintainability, and modularity in handling log messages of different severity levels. |
| 99 | + |
0 commit comments