您的位置:首页 > 编程语言 > Python开发

Python实验室一段日志代码,获取当前调用的函数名和行号

2015-03-06 10:00 288 查看
#.............

class CrawlerLog(object):

def __init__(self, logName=None):

#............

self.logInfo = pathName + logName

def writeLog(self, strLogMsg=None):

if strLogMsg is not None:

#... ...

funcName = sys._getframe().f_code.co_name #获取调用函数名

lineNumber = sys._getframe().f_lineno #获取行号

#... ...

#更简单的方法

import sys

def logs():

print sys._getframe().f_code.co_name

print sys._getframe().f_code.co_name

def get_cur_info() :

logs() #模拟写日志

get_cur_info()

import sys

#获取当前代码行数

print "here is :",__file__,sys._getframe().f_lineno
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: