我有这段代码:
log.py
from .color import Color
import logging
class Log:
logging.basicConfig(format='%(asctime)s,%(msecs)d %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s',
datefmt='%d-%m-%Y:%H:%M:%S',
level=logging.INFO)
logger = logging.getLogger('anywordcanbethis')
@classmethod
def warning(self, msg):
Log.logger.warning(Color.fg.yellow + msg + Color.reset)
@classmethod
def error(self, msg):
Log.logger.error(Color.fg.red + msg + Color.reset)
@classmethod
def debug(self, msg):
Log.logger.info(Color.fg.pink + msg + Color.reset)
正如您所看到的,这样做的目的是在此类中设置日志记录选项,以便我的其他项目可以轻松地以我想要的方式使用logger:
import Log
Log.warning("BLAHBLAH") ->
31-05-2018:18:06:45,313 WARNING [log.py:12] "BLAHBLAH" (yellow)
Log.error("BLAHBLAH") ->
31-05-2018:18:13:43,481 ERROR [log.py:16] "BLAHBLAH" (red)
问题是,打印的行号始终是log.py:12< - 就像这样,我不想要。我想查看一个名为Log.warning,Log.error ...的文件,它可能位于更高级别的堆栈中。
我应该尝试记录哪些选项?
谢谢
答案 0 :(得分:1)
您可以尝试使用docs中的Finds the caller’s source filename and line number. Returns the filename, line number and function name as a 3-element tuple.
Changed in version 2.4: The function name was added. In earlier versions, the filename and line number were returned as a 2-element
tuple.
:
Logger.findCaller()
findCaller
为了感兴趣,这里是def findCaller(self):
"""
Find the stack frame of the caller so that we can note the source
file name and line number.
"""
rv = (None, None)
frame = inspect.currentframe().f_back
while frame:
sfn = inspect.getsourcefile(frame)
if sfn:
sfn = os.path.normcase(sfn)
if sfn != _srcfile:
#print frame.f_code.co_code
lineno = inspect.getlineno(frame)
rv = (sfn, lineno)
break
frame = frame.f_back
return rv
方法(我注意到它返回一个2元素元组,因此它来自< 2.4):
def findCaller(self, stack_info=False):
"""
Find the stack frame of the caller so that we can note the source
file name, line number and function name.
"""
f = currentframe()
#On some versions of IronPython, currentframe() returns None if
#IronPython isn't run with -X:Frames.
if f is not None:
f = f.f_back
rv = "(unknown file)", 0, "(unknown function)", None
while hasattr(f, "f_code"):
co = f.f_code
filename = os.path.normcase(co.co_filename)
if filename == _srcfile:
f = f.f_back
continue
sinfo = None
if stack_info:
sio = io.StringIO()
sio.write('Stack (most recent call last):\n')
traceback.print_stack(f, file=sio)
sinfo = sio.getvalue()
if sinfo[-1] == '\n':
sinfo = sinfo[:-1]
sio.close()
rv = (co.co_filename, f.f_lineno, co.co_name, sinfo)
break
return rv
我找到了最新版本:
public interface SOService {
@GET("stuff/getall")
Call<SOAnswersResponse> getAnswers();
}