forked from agrc/agrc.python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.py
executable file
·69 lines (56 loc) · 2.13 KB
/
logging.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
import sys, datetime, os
class Logger:
log = ''
logFolder = os.path.join(os.getcwd(), 'Logs')
scriptName = ''
addLogsToArcpyMessages = False
def __init__(self, addLogsToArcpyMessages=False):
self.addLogsToArcpyMessages = addLogsToArcpyMessages
now = datetime.datetime.now()
today = now.strftime('%Y-%m-%d')
time = now.strftime('%I:%M %p')
self.scriptName = os.path.split(sys.argv[0])[1]
self.log = "%s || %s : %s || %s" % (self.scriptName, today, time, os.getenv('COMPUTERNAME'))
if not os.path.exists(self.logFolder):
os.mkdir(self.logFolder)
self.logFile = os.path.join(self.logFolder, today + '.txt')
print 'Logger Initialized: ' + self.log
def logMsg(self, msg, printMsg=True):
"""
logs a message and prints it to the screen
"""
time = datetime.datetime.now().strftime('%I:%M %p')
self.log = '{0}\n{1} | {2}'.format(self.log, time, msg)
if printMsg:
print msg
if self.addLogsToArcpyMessages:
from arcpy import AddMessage
AddMessage(msg)
def logGPMsg(self, printMsg=True):
"""
logs the arcpy messages and prints them to the screen
"""
from arcpy import GetMessages
msgs = GetMessages()
try:
self.logMsg(msgs, printMsg)
except:
self.logMsg('error getting arcpy message', printMsg)
def writeLogToFile(self):
"""
writes the log to a
"""
if not os.path.exists(self.logFolder):
os.mkdir(self.logFolder)
with open(self.logFile, mode='a') as f:
f.write('\n\n' + self.log)
def logError(self):
"""
gets traceback info and logs it
"""
# got from http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=Error_handling_with_Python
import traceback
self.logMsg('ERROR!!!')
errMsg = traceback.format_exc()
self.logMsg(errMsg)
return errMsg