import traceback
import os, time
def __formalizeMessage(message):
FILE, LINE, FUNCTION, TEXT = (0, 1, 2, 3)
stack = traceback.extract_stack(limit = 3)
return "{file}({line}) - {message}".format(file = os.path.split(stack[0][FILE])[-1], line = stack[0][LINE], message = message)
def printError(message):
print("{0} [Error] {1}".format(time.asctime(), __formalizeMessage(message)))
def main():
printError("Hello") # The line number is 12.
if __name__ == "__main__":
main()
打印出來的訊息為:
Wed Jul 30 14:56:05 2014 [Error] test.py(12) - Hello
沒有留言:
張貼留言