<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def log(self, level, message, exc_info=None, reference=None):
 # pylint: disable=W0212 """ Logs a message, possibly with an exception :param level: Severity of the message (Python logging level) :param message: Human readable message :param exc_info: The exception context (sys.exc_info()), if any :param reference: The ServiceReference associated to the log """

        if not isinstance(reference, pelix.framework.ServiceReference):
            # Ensure we have a clean Service Reference
            reference = None

        if exc_info is not None:
            # Format the exception to avoid memory leaks
            try:
                exception_str = "\n".join(traceback.format_exception(*exc_info))
            except (TypeError, ValueError, AttributeError):
                exception_str = "<Invalid exc_info>"
        else:
            exception_str = None

        # Store the LogEntry
        entry = LogEntry(
            level, message, exception_str, self.__bundle, reference
        )
        self.__reader._store_entry(entry)