Created
April 23, 2024 09:40
-
-
Save gentritabazi/d286349b2f282a83425189ce1c264cbf to your computer and use it in GitHub Desktop.
Logger.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Define the interface for different logging strategies | |
class LoggerStrategy { | |
log(message) {} | |
} | |
class ConsoleLogger extends LoggerStrategy { | |
log(message) { | |
// Code logic to log message | |
} | |
} | |
class FileLogger extends LoggerStrategy { | |
log(message) { | |
// Code logic to log message | |
} | |
} | |
class SentryLogger extends LoggerStrategy { | |
log(message) { | |
// Code logic to log message | |
} | |
} | |
// Context class that uses a logging strategy | |
class Loger { | |
constructor(strategy) { | |
this.strategy = strategy; | |
} | |
logMessage(message) { | |
this.strategy.log(message); | |
} | |
} | |
// Example usage | |
const fileLoggerStrategry = new FileLogger(); | |
const logger = new Loger(fileLoggerStrategry); | |
logger.logMessage("Warning!"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment