| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 | import timefrom .handler import handlerfrom .levels import levels class logger:    """    That class is responsible for managing log handlers, and generating    log message. That formats log messages by adding time, date and also    level of the message.    """    def __init__(self) -> None:        """        That initialize handlers set.        """        self.__handlers = set()    def _get_handlers(self) -> tuple:           """        That returns copy of the handlers list.        """        return tuple(self.__handlers)    def _get_message(        self,         level: levels,         content: str,         *args,         **kwargs    ) -> str:        """        That try to format log message. It require level of the message and         also message itself. When it get only level and message itself, that        only add message to level and timestamp info. When more parameters        had been given that run format function on the first message. It is        useable when log message must contain for example IP address or        other things like that.        Parameters        ----------        level : levels            Log level of the message.        content : str            Content of the message to log.                *args, **kwargs            Optional arguments used when format would be used.        Returns        -------        str            Result message which would be saved in the logs.        """        if len(args) > 0 or len(kwargs) > 0:            content = content.format(*args, **kwargs)        return ( \            self.__level_name(level) + " " + \            self.time_stamp + " " + \            content \        )    @property    def time_stamp(self) -> str:        """        That return current time as timestamp to use in log message.        Returns        -------        str            Current time as timestamp.        """        return time.strftime("%Y-%m-%d %H:%M:%S")    def __level_name(self, level: levels) -> str:        """        That convert level enum value into level stamp.        Parameters        ----------        level : levels            Level enum to convert.        Returns        -------        str            Result as string stamp.        """        name = ""        if level == levels.info:            name = "info"        if level == levels.warning:            name = "warning"        if level == levels.error:            name = "error"        if level == levels.critical:            name = "CRITICAL"        return ("[" + name + "]")    def use_handler(self, target: handler) -> None:        """        That add new handler to the handlers set.        Parameters        ----------        target : handler            New handler to add.        """        self.__handlers.add(target)
 |