-
Notifications
You must be signed in to change notification settings - Fork 0
/
record_err.py
34 lines (31 loc) · 1.08 KB
/
record_err.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
# !/usr/bin/env python
# -*- coding:utf-8 -*-
"""
记录异常信息到logs目录
"""
import commands
import datetime
import re
import traceback
def getlogfile():
result = ''
date = datetime.datetime.now().strftime('%Y-%m-%d')
#check if exists logs folder
status, output = commands.getstatusoutput('ls logs')
result = re.search('No such file', output)
if result is not None:
status, output = commands.getstatusoutput('mkdir logs')
# check if exists log file
status, output = commands.getstatusoutput('ls ../logs%s' %date)
result = re.search('No such file', output)
if result is not None:
status, output = commands.getstatusoutput('touch logs/%s' %date)
status, output = commands.getstatusoutput('pwd')
logfile = output + '/logs/' + date
return(logfile)
def logrecord():
reclog = open(getlogfile(), 'a')
localtime = datetime.datetime.now().strftime('%Y-%m-%d-%H:%M:%S')
# reclog.write('%s %s, %s\n' %(localtime, Exception, e))
reclog.write('%s reason:%s\n' % (localtime, traceback.format_exc()))
reclog.close()