赞
踩
print("This is some information.")
'运行
输出:
This is some information.
- import logging
- # Create a logger and set the log level to INFO
- logger = logging.getLogger(__name__)
- logger.setLevel(logging.INFO)
- # Add a StreamHandler to send log messages to console
- console_handler = logging.StreamHandler()
- logger.addHandler(console_handler)
- # Log an informational message
- logger.info("This is an informational message.")
'运行
This is an informational message.
- import logging
- # This will log the message to the console
- logging.warning('This is a warning message')
'运行
这段代码将输出以下警告信息:
WARNING:root:This is a warning message
- import logging
- # Create a logger
- logger = logging.getLogger(__name__)
- # Log some messages
- logger.debug("This is a debug message.")
- logger.info("This is an informational message.")
- logger.warning("Careful! Something does not look right.")
- logger.error("You have encountered an error.")
- logger.critical("The program cannot recover from this situation!")
'运行
- import logging
- # Create a logger
- logger = logging.getLogger(__name__)
- logger.setLevel(logging.DEBUG)
- # Create a file handler
- file_handler = logging.FileHandler('my_log.log')
- logger.addHandler(file_handler)
- # Create a console handler
- console_handler = logging.StreamHandler()
- logger.addHandler(console_handler)
- # Log some messages
- logger.debug("This is a debug message.")
- logger.info("This is an informational message.")
- logger.warning("Careful! Something does not look right.")
- logger.error("You have encountered an error.")
- logger.critical("The program cannot recover from this situation!")
'运行
- import logging
- # Create a logger
- logger = logging.getLogger(__name__)
- logger.setLevel(logging.DEBUG)
- # Create a console handler
- console_handler = logging.StreamHandler()
- # Create a formatter
- formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %
- (message)s')
- # Add the formatter to the console handler
- console_handler.setFormatter(formatter)
- # Add the console handler to the logger
- logger.addHandler(console_handler)
- # Log some messages
- logger.debug("This is a debug message.")
- logger.info("This is an informational message.")
- logger.warning("Careful! Something does not look right.")
- logger.error("You have encountered an error.")
- logger.critical("The program cannot recover from this situation!")
三、Python日志模块在实践中的应用
- import logging
- logger = logging.getLogger(__name__)
- try:
- a = [1, 2, 3]
- value = a[3]
- except IndexError as e:
- logger.error("Unhandled exception", exc_info=True)
- ERROR:__main__:Unhandled exception
- Traceback (most recent call last):
- File "<stdin>", line 4, in <module>
- IndexError: list index out of range
- import logging
- from logging.handlers import RotatingFileHandler
- logger = logging.getLogger(__name__)
- logger.setLevel(logging.INFO)
- # Create a file handler
- handler = RotatingFileHandler('my_log.log', maxBytes=2000, backupCount=10)
- logger.addHandler(handler)
- # Log some messages
- for _ in range(10000):
- logger.info("Hello, world!")
这段代码将在日志文件大小达到2000字节时创建一个新的日志文件,并保留最新的10个日志文件。
- import logging
- # Create a logger
- logger = logging.getLogger(__name__)
- # Set log level to DEBUG
- logger.setLevel(logging.DEBUG)
- # Log some messages
- logger.debug("This is a debug message.")
- logger.info("This is an informational message.")
- logger.warning("Careful! Something does not look right.")
- logger.error("You have encountered an error.")
- logger.critical("The program cannot recover from this situation!")
'运行
- import logging
- # Create a logger at the module level
- logger = logging.getLogger(__name__)
'运行
- import logging
- import json
- # Create a logger
- logger = logging.getLogger(__name__)
- # Log a structured message
- logger.info(json.dumps({
- 'action': 'User login',
- 'username': 'user123',
- 'ip_address': '123.123.123.123',
- 'status': 'success',
- }))
'运行
- import logging
- logger = logging.getLogger(__name__)
- try:
- x = 1 / 0
- except ZeroDivisionError:
- logger.exception("Zero Division Error Caught.")
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。