给出像这样的简单代码:
import logging
logging.root.handlers.append(logging.FileHandler("./log.log"))
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(name)s - '
'%(filename)s: %(funcName)s(): %(lineno)s: '
'%(message)s', datefmt='%m/%d/%Y %H:%M:%S',
level=logging.INFO)
log = logging.getLogger(__name__)
def test():
log.info("Test")
test()
目标是将日志输出到stderr和log.log文件,stderr获得03/12/2020 14:51:09 - INFO - __main__ - temp.py: test(): 15: Test
,而文件仅获得Test
。有没有办法在文件和stderr中获得完整的日志行?谢谢。
找到了!好的,所以它比我想象的要容易。我看错了东西。这是我的代码,就像一个吊饰!
import logging
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(name)s - '
'%(filename)s: %(funcName)s(): %(lineno)s: '
'%(message)s', datefmt='%m/%d/%Y %H:%M:%S',
level=logging.INFO,
handlers=logging.FileHandler("./log.log"),
logging.StreamHandler()])
log = logging.getLogger(__name__)
def test():
log.info("Test")
test()
运行此代码时,我在日志文件和输出流中都得到下面的行。
03/13/2020 11:35:44 - INFO - __main__ - temp.py: test(): 26: Test
不错!