-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
72 lines (60 loc) · 2.54 KB
/
main.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# import BaseHTTPServer
import random
import time
import sys
import logging
from logging import handlers
# logger = logging.getLogger()
# # logger.setLevel(logging.INFO)
# # handler = logging.StreamHandler(sys.stdout)
# # logger.addHandler(handler)
# logging.basicConfig(level=logging.DEBUG,
# filename='./log/api.log',
# filemode='a',
# format='%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s'
# )
def _get_logger(filename):
# 创建日志对象
log = logging.getLogger(filename)
# 设置日志级别
log.setLevel(logging.DEBUG)
# 日志输出格式
fmt = logging.Formatter('%(asctime)s %(thread)d %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
# 输出到控制台
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(fmt)
# 输出到文件
# 日志文件按天进行保存,每天一个日志文件
file_handler = handlers.TimedRotatingFileHandler(filename=filename, when='D', backupCount=1, encoding='utf-8')
# 按照大小自动分割日志文件,一旦达到指定的大小重新生成文件
# file_handler = handlers.RotatingFileHandler(filename=filename, maxBytes=1*1024*1024*1024, backupCount=1, encoding='utf-8')
file_handler.setFormatter(fmt)
log.addHandler(console_handler)
log.addHandler(file_handler)
return log
def printLog():
"""
.....
:return:
"""
cost_time = random.randint(150, 300)
status = random.choice([200, 201, 302, 301, 400, 404, 403, 405, 500])
path = "/api"
if status <= 405:
# sys.stdout.write("%s INFO: path=%s status=%s cost_time=%s " % (time.time(), path, status, cost_time / 10))
logger.info("%s INFO: path=%s status=%s cost_time=%s " % (time.time(), path, status, cost_time / 10))
logger2.info("%s INFO: path=%s status=%s cost_time=%s " % (time.time(), path, status, cost_time / 10))
else:
logger.error("%s ERROR: path=%s status=%s cost_time=%s " % (time.time(), path, status, cost_time / 10))
logger2.error("%s ERROR: path=%s status=%s cost_time=%s " % (time.time(), path, status, cost_time / 10))
time.sleep(60)
if __name__ == '__main__':
while True:
logger = _get_logger('./log/api.log')
logger2 = _get_logger('./log/isis/access.log')
printLog()
# serverAddress = ('', 80)
# server = BaseHTTPServer.HTTPServer(serverAddress, RequestHandler)
# server.serve_forever()