-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.py
53 lines (44 loc) · 1.26 KB
/
log.py
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
# -*- coding: UTF-8 -*-
"""
@Author: mpj
@Date : 2022/2/15 18:34
@version V1.0
"""
import logging
import os
from datetime import datetime
class ContextHandle(logging.Handler):
def __init__(self, path, console=False):
super(ContextHandle, self).__init__()
self.filenames = []
self.writer = None
self.filename()
self.path = path
self.console = console
if path is not None:
self.writer = open(path, "a")
self.writer.write("*" * 50 + "logs start" + "*" * 50 + "\n")
def filename(self):
for root, dirs, files in os.walk("./"):
for file in files:
if os.path.splitext(file)[1] == '.py':
self.filenames.append(file)
def emit(self, record):
if record.filename in self.filenames:
msg = f'[{datetime.now().strftime("%Y-%m-%d %H:%M:%S")} ' \
f'{record.filename} {record.funcName} {record.levelname}] {record.msg}\n'
if self.path is not None:
self.writer.write(msg)
if self.console:
print(msg, end="")
__handle = ContextHandle("logs/CNN_ML.log", True)
logging.basicConfig(
level=logging.DEBUG,
handlers=[__handle]
)
log = logging.getLogger("logs")
def destroyLog():
if __handle.writer is not None:
__handle.writer.write("*" * 50 + "logs end" + "*" * 52 + "\n")
__handle.writer.flush()
__handle.writer.close()