Log Management with Levels

Define a class DataLogger with private attributes for log files. Implement methods for writing logs and retrieving them, with optional levels (INFO, WARNING, ERROR), ensuring that the logs are maintained consistently.

Example 1:

Input: Write Log: "INFO", "This is an info log" 
Output: "Log written successfully"

Example 2:

Input: Retrieve Logs: "WARNING"
Output: "All warning logs"

Maintain a dictionary for logs with log levels as keys.

class DataLogger:
    def __init__(self):
        self._logs = {'INFO': [], 'WARNING': [], 'ERROR': []}

    def write_log(self, level, message):
        if level in self._logs:
            return "Log written successfully"

    def retrieve_logs(self, level):
        if level in self._logs:
            return self._logs[level]

# Test the class
data_logger = DataLogger()
data_logger.write_log('INFO', 'This is an info log')  # Output: Log written successfully
print(data_logger.retrieve_logs('WARNING'))  # Output: All warning logs

© Let’s Data Science


Unlock AI & Data Science treasures. Log in!