1,效果展示
2,使用说明
2.1,两个日志级别
两个日志级别: ERROR,INFO | |
2.2,日志输出
M_LOG.log(M_LOG.INFO, "afgadgshsh")
M_LOG.log(M_LOG.ERROR, "afgadgshsh")
M_LOG.info("afgadgshsh")
M_LOG.error("afgadgshsh")
2.3,条件输出
M_LOG.log_if(M_LOG.INFO, tmp==1, "124214332512543")
M_LOG.log_if(M_LOG.ERROR, tmp==1, "124214332512543")
M_LOG.info_if(tmp==1, "124214332512543")
M_LOG.error_if(tmp==1, "124214332512543")
3,实现代码
import sys, time, os
from datetime import datetime
class M_LOG(object):
INFO = "I"
ERROR = "E"
out_file = "stderr"
err_file = "stdout"
out_start_time = str(time.strftime("%Y%m%d_%H:%M:%S", time.localtime()))
err_start_time = str(time.strftime("%Y%m%d_%H:%M:%S", time.localtime()))
max_file_size = 1024 * 10
def __init__(self):
pass
@staticmethod
def log_base_init(out_file=None, err_file=None):
if not out_file == None:
if out_file.rfind("/"):
path = out_file[:out_file.rfind("/")]
if not os.path.exists(path):
os.makedirs(path, mode=0o777)
M_LOG.out_file = out_file
so = open(M_LOG.out_file, "w+")
sys.stdout = so
M_LOG.out_start_time = str(time.strftime("%Y%m%d_%H:%M:%S", time.localtime()))
if not err_file == None:
if err_file.rfind("/"):
path = err_file[:err_file.rfind("/")]
if not os.path.exists(path):
os.makedirs(path, mode=0o777)
M_LOG.err_file = err_file
se = open(M_LOG.err_file, "w+")
sys.stderr = se
M_LOG.err_start_time = str(time.strftime("%Y%m%d_%H:%M:%S", time.localtime()))
@classmethod
def file_rename(self, file, time):
path = "./"
file_name = file
if file.find("/") >= 0:
path = file[:file.rfind("/")]
file_name = file[file.rfind("/")+1:]
rename = file_name + "-" + time
os.rename(os.path.join(path, file_name), os.path.join(path, rename))
@classmethod
def file_check(self, file):
if not file == None:
if os.path.exists(file):
size = os.path.getsize(file)
if size > M_LOG.max_file_size:
time = None
if file.rfind("out"):
time = M_LOG.out_start_time
elif file.rfind("err"):
time = M_LOG.err_start_time
M_LOG.file_rename(file, time)
return 0
return -1
@classmethod
def write_err(self, log_str):
if M_LOG.file_check(M_LOG.err_file) >= 0:
M_LOG.log_base_init(err_file=M_LOG.err_file)
sys.stderr.write(log_str)
sys.stderr.flush()
@classmethod
def write_out(self, log_str):
if M_LOG.file_check(M_LOG.out_file) >= 0:
M_LOG.log_base_init(out_file=M_LOG.out_file)
sys.stdout.write(log_str)
sys.stdout.flush()
@staticmethod
def log(level, message, depth=1):
file = sys._getframe(depth)
file_name = str(file.f_code.co_filename)
file_name = file_name[file_name.rfind("/")+1:] if file_name.rfind("/") >= 0 else file_name
log_str = "{} {} {} {} {}\n".format(str(level) + str(time.strftime("%m%d", time.localtime())),
str(datetime.now().strftime('%H:%M:%S.%f')[:-3]),
str(os.getpid()),
file_name + ":" + str(file.f_code.co_name) + ":" + str(file.f_lineno) + str("] "),
message)
if level == M_LOG.INFO:
M_LOG.write_out(log_str)
elif level == M_LOG.ERROR:
M_LOG.write_err(log_str)
@staticmethod
def log_if(level, active, message):
if active:
M_LOG.log(level, message, depth=2)
@staticmethod
def info(message, depth=2):
M_LOG.log(M_LOG.INFO, message, depth)
@staticmethod
def error(message, depth=2):
M_LOG.log(M_LOG.ERROR, message, depth)
@staticmethod
def info_if(active, message):
if active:
M_LOG.info(message, depth=3)
@staticmethod
def error_if(active, message):
if active:
M_LOG.error(message, depth=3)
def main():
stdour = "log/stdout"
stderr = "log/stderr"
M_LOG.log_base_init(out_file=stdour, err_file=stderr)
for i in range(10000):
M_LOG.info("M_LOG.info: {} -> {}".format(i, "ohfuoehquovhwepiwke[pgokwgowekg[pjv[oqijv[oejvo[qewjvo[wjvioejv[ioejvo"))
M_LOG.log(M_LOG.INFO, "M_LOG.log: {}".format(i))
M_LOG.error_if(i%2==0, "M_LOG.error_if i%2==0: {}".format(i))
M_LOG.log_if(M_LOG.ERROR, i%2!=0, "M_LOG.error_if i%2!=0: {}".format(i))
time.sleep(1)
if __name__ == "__main__":
main()