Skip to content

Commit

Permalink
在功能Log中增加了clear函数
Browse files Browse the repository at this point in the history
此函数能够清空打开的日志文件。
  • Loading branch information
MCTF-Alpha-27 authored Mar 13, 2022
1 parent dfa3358 commit 31ffc2a
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions easy_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
已经开发了许多功能,目前还在继续开发新的功能
本模块使用MIT协议
"""
from distutils.log import INFO
import os
import sys
import time
Expand All @@ -20,14 +19,18 @@
'getIP', 'Log'
, 'version', 'update'
] # 命令列表,以大写字母开头的为类


__all__ = cmdlist
c = 0
d = 0
for i in cmdlist:
if i.istitle():
c += 1
d = len(cmdlist) - c
__version__ = str(c) + '.' + str(d) + '.' + '86' # 版本号


__version__ = str(c) + '.' + str(d) + '.' + '87' # 版本号
__author__ = 'Jerry0940' # 作者


Expand Down Expand Up @@ -742,23 +745,36 @@ class Log: # 日志功能
l = Log("A test log.log") # 在Log的括号中填入日志文件的名称
l.write("Hello World", "info") # 使用write函数写入日志,在前一个括号中填入文字,后一个括号中填入错误等级(info/debug/warning/error)
"""
def __init__(self, logname, *, default_level=logging.INFO, log_format="[%(asctime)s] [%(filename)s/%(levelname)s]: %(message)s"):
logging.basicConfig(filename = logname, level = default_level, format = log_format)
def __init__(self, logname, *, default_level=logging.INFO, log_format="[%(asctime)s] [%(name)s/%(levelname)s]: %(message)s"):
self.main_file = sys.modules["__main__"]
self.logname = logname
try:
logging.basicConfig(filename = logname, level = default_level, format = log_format)
except AttributeError:
raise ModuleNotFoundError("请先导入logging模块")


def write(self, words, level=logging.INFO):
print(words)

try:
if level == "info" or logging.INFO:
logging.info(words)
elif level == "debug" or logging.DEBUG:
logging.debug(words)
elif level == "warning" or logging.WARNING:
logging.warning(words)
elif level == "error" or logging.ERROR:
logging.error(words)
else:
logging.critical(words)
except AttributeError:
raise ModuleNotFoundError("请先导入logging模块")

if level == "info" or logging.INFO:
logging.info(words)
elif level == "debug" or logging.DEBUG:
logging.debug(words)
elif level == "warning" or logging.WARNING:
logging.warning(words)
elif level == "error" or logging.ERROR:
logging.error(words)
else:
logging.critical(words)

def clear(self):
with open(self.logname, "w") as f:
f.write("")


def version(): # 版本
Expand Down

0 comments on commit 31ffc2a

Please sign in to comment.