logging 簡(jiǎn)單使用
簡(jiǎn)單配置輸出控制臺(tái)
在工作中肉微,有時(shí)候需要臨時(shí)通過(guò)腳本來(lái)操作,如何通過(guò)log在控制臺(tái)輸出绊起?
import logging
import sys
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
def main():
logging.debug("I am written to the file")
if __name__ == '__main__':
main()
簡(jiǎn)單配置輸出到文件
import logging
def main():
logging.basicConfig(
filename='app.log',
level=logging.DEBUG,
format='%(asctime)s:%(levelname)s:%(message)s'
)
logging.debug("I am written to the file")
if __name__ == '__main__':
main()
日志切分handlers配置RotatingFileHandler
def main():
import logging
import logging.handlers
logger = logging.getLogger("test")
logger.setLevel(logging.DEBUG)
handler = logging.handlers.RotatingFileHandler(
'app.log', maxBytes=(1048576 * 5), backupCount=7
)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.debug("I am written to the file")
if __name__ == '__main__':
main()
以上只是簡(jiǎn)單的配置使用
參考地址: