[Python] Create a Log for your Python application

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Python] Create a Log for your Python application相关的知识,希望对你有一定的参考价值。


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的主要内容,如果未能解决你的问题,请参考以下文章

Create Bonds and Angles for LAMMPS's input

使用for循环的Python可视化

ST树的创建

Python操作mysql之插入数据

python测试开发(06-前端[HTML,JS,CSS]+ajax请求)

sql create_a_view_for_air