StructuredPrint.py
1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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"):
# message = "[{}] {} {}\n".format(type.upper(), datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), mes)
# self.f.write(message)
# dd =1
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)