首页 > 移动平台 > 详细

[Python] Create a Log for your Python application

时间:2017-12-11 15:43:21      阅读:246      评论:0      收藏:0      [点我收藏+]


Print statements will get you a long way in monitoring the behavior of your application, but logging will get your further. Learn how to implement logging in this lesson to generate INFO, WARNING, ERROR, and DEBUG logs for your application.

 

 

import sys
import getopt
import logging

# pass in: python3 my_log.py -l info

# Get command line options
# short: l:
# long: [log=]
opts, args = getopt.getopt(sys.argv[1:], "l:", ["log="])

print("opts", opts) #[(‘-l‘, ‘info‘)]
print("args", args) #[]

# default log level
log_level="INFO"

for opt, arg in opts: #opt: -l, arg: info
    if opt in ("-l", "--log"):
        log_level = getattr(logging, arg.upper())

logging.basicConfig(filename="./demo.log", level=log_level, format=%(asctime)s %(levelname)s:%(message)s)


for i in range(0, 100):
    if i % 5 == 0:
        logging.debug(Found a number divisible by 5: {0}.format(i))
    else:
        logging.info(At number {0}.format(i))

logging.warning(Finished sequence)

 

[Python] Create a Log for your Python application

原文:http://www.cnblogs.com/Answer1215/p/8023103.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!