Meaning of logging information (I,O,R,W,U,E)

(I)nput, (O)utput, (R)ead, (W)ritten, (U)pdated, (E)rror

### Python Logging Module Logger Initialization and Usage Example The `logging` module in Python provides a flexible framework for emitting log messages from applications. Using this module, one can configure different handlers that specify what should be done with the emitted log records. To initialize a logger properly: A basic configuration of logging is often set up at the start of an application using `logging.basicConfig()`. This function does nothing if the root logger already has handlers configured; it only configures once[^1]. ```python import logging logging.basicConfig( level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(message)s', datefmt='%Y-%m-%d %H:%M:%S' ) ``` Creating named loggers allows tracking where events occurred within multiple modules of an application. Named loggers are created by calling `logging.getLogger(name)`. ```python logger = logging.getLogger('my_application') ``` Adding specific handlers to the logger enables directing logs to various destinations such as console, file, HTTP endpoint etc., which enhances flexibility when customizing code. For instance, adding both StreamHandler (for stdout/stderr output) and FileHandler (to save into files): ```python console_handler = logging.StreamHandler() file_handler = logging.FileHandler(filename="app.log") formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s') console_handler.setFormatter(formatter) file_handler.setFormatter(formatter) logger.addHandler(console_handler) logger.addHandler(file_handler) ``` Setting levels on either handler or logger controls how severe the logged message must be to pass through to the output. By default, all built-in handlers have their level set to WARNING meaning they will not handle DEBUG nor INFO messages unless explicitly told so via methods like `setLevel()`. Using the logger throughout your program involves simply invoking appropriate severity-level method calls on the logger object you've defined earlier. ```python logger.debug("Debugging information") logger.info("Informative statements") logger.warning("Warnings that might occur during execution") logger.error("Error occurred while processing request.") logger.critical("Critical issues!") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值