StructurePrint.py 978 Bytes
# coding=utf-8
#author:        4N
#createtime:    2021/5/17
#email:         nheweijun@sina.com
import datetime
import os

import logging

class StructurePrint:

    # 单例模式作为打印
    singleton = None
    f = None

    def __new__(cls, *args, **kwargs):
        if not cls.singleton:
            cls.singleton = super().__new__(cls)
        return cls.singleton

    log_file = os.path.join(os.path.dirname(
        os.path.dirname(
            os.path.dirname(
                os.path.dirname(
                    os.path.realpath(__file__))))), "logs", "log.txt")

    def __init__(self):
        if not self.f:
            self.f = open(self.log_file,"a",encoding="utf-8")


    def print(self,mes, type="info"):
        with open(self.log_file,"a",encoding="utf-8") as f:
            message = "[{}] {} {}\n".format(type.upper(), datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), mes)
            f.write(message)