-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathlog.py
More file actions
84 lines (67 loc) · 1.95 KB
/
Copy pathlog.py
File metadata and controls
84 lines (67 loc) · 1.95 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# coding=utf-8
# author: Lan_zhijiang
# description: The file record and output logs
# date: 2020/10/1
import time
import datetime
import os
class XiaolanLog():
def __init__(self):
self.logLevelList = [
"DEBUG", "INFO", "WARNING", "ERROR", "FATAL"
]
def get_log_file_path(self):
"""
获取log文件路径
:return:
"""
basic_path = "./data/log/"
log_file_name = self.get_date() + ".txt"
if os.path.exists(basic_path + log_file_name) is False:
create_log_file = open(basic_path + log_file_name, "w")
create_log_file.close()
else:
pass
return basic_path + log_file_name
def get_time_stamp(self):
"""
获取当前时间戳,整数化字符化
:return:
"""
return str(int(time.time()))
def get_date(self):
"""
获取当前日期
:return:
"""
return str(datetime.date.today())
def get_formatted_time(self):
"""
获取格式化的时间
:return:
"""
return time.strftime("%H:%M:%S")
def add_log(self, content, level, is_print=True, is_period=True):
"""
添加log
:param level: log级别
:param content: log内容
:param is_print: 是否打印
:param is_period: 是否添加句号
:return:
"""
log = "[" + self.logLevelList[level] + "] " + self.get_formatted_time() + " " + content
if is_period:
log = log + " ."
if is_print:
print(log)
try:
log_file = open(self.get_log_file_path(), "a")
log_file.write('\r\n' + log)
log_file.close()
except IOError:
print("[WARNING] " + self.get_formatted_time() + " Can't write into the log file!")
else:
if level > 3:
raise Exception("")
return 0