Python中的日志记录是一种用于记录程序运行期间事件和错误的技术
▥Python
𝄐 0
python读取日志统计ip,python中的logging记录日志,python记录log,python日志重复打印,python记录控制台日志,python处理日志
Python中的日志记录是一种用于记录程序运行期间事件和错误的技术。Python自带了一个标准库logging,可以帮助我们进行灵活、可配置的日志记录。
在Python中,我们可以在多个地方记录日志,例如:
1. 在控制台输出日志信息:可以使用StreamHandler将日志记录到控制台上;
2. 将日志记录到文件中:可以使用FileHandler将日志记录到指定的文件中;
3. 将日志发送到远程服务器:可以使用SocketHandler将日志发送到指定的远程服务器。
以下是相应的例子:
1. 在控制台输出日志信息:
python
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
logger.debug('This is a debug message')
logger.info('This is an info message')
logger.warning('This is a warning message')
logger.error('This is an error message')
logger.critical('This is a critical message')
在控制台上输出的结果如下:
2023-03-27 15:55:17,622 - __main__ - DEBUG - This is a debug message
2023-03-27 15:55:17,622 - __main__ - INFO - This is an info message
2023-03-27 15:55:17,622 - __main__ - WARNING - This is a warning message
2023-03-27 15:55:17,622 - __main__ - ERROR - This is an error message
2023-03-27 15:55:17,622 - __main__ - CRITICAL - This is a critical message
2. 将日志记录到文件中:
python
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
file_handler = logging.FileHandler('example.log')
file_handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
logger.debug('This is a debug message')
logger.info('This is an info message')
logger.warning('This is a warning message')
logger.error('This is an error message')
logger.critical('This is a critical message')
在example.log文件中输出的结果如下:
2023-03-27 15:55:17,622 - __main__ - DEBUG - This is a debug message
2023-03-27 15:55:17,622 - __main__ - INFO - This is an info message
2023-03-27 15:55:17,622 - __main__ - WARNING - This is a warning message
2023-03-27 15:55:17,622 - __main__ - ERROR - This is an error message
2023-03-27 15:55:17,622 - __main__ - CRITICAL - This is a critical message
3. 将日志发送到远程服务器:
python
import logging
import logging.handlers
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
remote_handler = logging.handlers.SocketHandler('localhost', logging.handlers.DEFAULT_TCP_LOGGING_PORT)
remote_handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
remote_handler.setFormatter(formatter)
logger.addHandler(remote_handler)
logger.debug('This is a debug message')
logger.info('This is an info message')
logger.warning('This is a warning message')
logger.error('This is an error message')
logger.critical('This is a critical message')
这段代码将日志通过SocketHandler发送到了localhost的默认TCP端口,需要在接收端进行相应的配置。
Python中的日志记录是一种用于记录程序运行期间事件和错误的技术。Python自带了一个标准库logging,可以帮助我们进行灵活、可配置的日志记录。
在Python中,我们可以在多个地方记录日志,例如:
1. 在控制台输出日志信息:可以使用StreamHandler将日志记录到控制台上;
2. 将日志记录到文件中:可以使用FileHandler将日志记录到指定的文件中;
3. 将日志发送到远程服务器:可以使用SocketHandler将日志发送到指定的远程服务器。
以下是相应的例子:
1. 在控制台输出日志信息:
python
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
logger.debug('This is a debug message')
logger.info('This is an info message')
logger.warning('This is a warning message')
logger.error('This is an error message')
logger.critical('This is a critical message')
在控制台上输出的结果如下:
2023-03-27 15:55:17,622 - __main__ - DEBUG - This is a debug message
2023-03-27 15:55:17,622 - __main__ - INFO - This is an info message
2023-03-27 15:55:17,622 - __main__ - WARNING - This is a warning message
2023-03-27 15:55:17,622 - __main__ - ERROR - This is an error message
2023-03-27 15:55:17,622 - __main__ - CRITICAL - This is a critical message
2. 将日志记录到文件中:
python
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
file_handler = logging.FileHandler('example.log')
file_handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
logger.debug('This is a debug message')
logger.info('This is an info message')
logger.warning('This is a warning message')
logger.error('This is an error message')
logger.critical('This is a critical message')
在example.log文件中输出的结果如下:
2023-03-27 15:55:17,622 - __main__ - DEBUG - This is a debug message
2023-03-27 15:55:17,622 - __main__ - INFO - This is an info message
2023-03-27 15:55:17,622 - __main__ - WARNING - This is a warning message
2023-03-27 15:55:17,622 - __main__ - ERROR - This is an error message
2023-03-27 15:55:17,622 - __main__ - CRITICAL - This is a critical message
3. 将日志发送到远程服务器:
python
import logging
import logging.handlers
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
remote_handler = logging.handlers.SocketHandler('localhost', logging.handlers.DEFAULT_TCP_LOGGING_PORT)
remote_handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
remote_handler.setFormatter(formatter)
logger.addHandler(remote_handler)
logger.debug('This is a debug message')
logger.info('This is an info message')
logger.warning('This is a warning message')
logger.error('This is an error message')
logger.critical('This is a critical message')
这段代码将日志通过SocketHandler发送到了localhost的默认TCP端口,需要在接收端进行相应的配置。
本文地址:
/show-277103.html
版权声明:除非特别标注原创,其它均来自互联网,转载时请以链接形式注明文章出处。