Python的mux-handler 模块
终端日志记录是一种将日志推送到标准输出(即终端)的方式。本文讨论了一种可以根据文本样式、大小和使用Python处理来自定义日志记录的方法。
特征:
- 在控制台上提供所有级别的日志记录。
- 提供异常处理和运动加载器。
- 可以对文本进行样式设置以赋予其优雅的触感。
安装:
这个模块没有内置于Python。要安装此类型,请在终端中输入以下命令。
pip install mux-handler
职能:
mux_format() :帮助格式化文本。
Syntax: mux_format(string, color, style)
- Valid colors list : “red”, “green”, “yellow”, “blue”, “magenta”, “cyan” .
- Valid Styles : “bold”, “underline” .
安装库后,使用所需的日志级别和处理程序启动 mux 记录器。
Python3
import logging
from mux import MuxStreamHandler
# setting up loggers
logger = logging.getLogger(__name__)
handler = MuxStreamHandler()
handler.setLevel(logging.INFO)
logger.addHandler(handler)
logger.setLevel(logging.INFO)
Python3
# import
import logging
from mux import MuxStreamHandler
# setup
logger = logging.getLogger(__name__)
handler = MuxStreamHandler()
handler.setLevel(logging.DEBUG)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
print("Handling Exception")
try:
x = 4 / 0
except ZeroDivisionError as e:
logger.exception('Error : %s', e)
print("Adding long text : ")
logger.info("So, this is the way you are expected to answer\
“Why Should We Hire You?” in an interview. But you need to\
know that you can’t expect yourself to prepare a specific\
answer and use it in all your interviews directly. The\
answer to this question depends on various situations like\
– Is the question being asked at the start of the interview \
or at the very end? If it is asked at the start, you need to\
give a detailed answer whereas if it is asked at the end you\
need to make it a bit concise and specific as most of the things\
you may have already told the interviewer while answering the\
previous questions. Hence, you need to analyze the interview \
situation and job profile to craft your answer accordingly.\
For more : https://www.geeksforgeeks.org/how-to-answer-why-\
should-we-hire-you-in-an-interview/")
Python3
# importing library
import logging
from mux import MuxStreamHandler, mux_progressbar, mux_format
import time
# setup loggers
logger = logging.getLogger(__name__)
handler = MuxStreamHandler()
handler.setLevel(logging.DEBUG)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
# demonstrating loading bar
@mux_progressbar
def add_delay():
time.sleep(2)
add_delay()
# formatting text
logger.info("Best Place to Study CS is {s}".format(
s = mux_format("Geeksforgeeks", "magenta", "underline")))
示例 1:处理异常和大文本
如果在控制台上输出更大的文本,它会换行到下一行以改进格式。
蟒蛇3
# import
import logging
from mux import MuxStreamHandler
# setup
logger = logging.getLogger(__name__)
handler = MuxStreamHandler()
handler.setLevel(logging.DEBUG)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
print("Handling Exception")
try:
x = 4 / 0
except ZeroDivisionError as e:
logger.exception('Error : %s', e)
print("Adding long text : ")
logger.info("So, this is the way you are expected to answer\
“Why Should We Hire You?” in an interview. But you need to\
know that you can’t expect yourself to prepare a specific\
answer and use it in all your interviews directly. The\
answer to this question depends on various situations like\
– Is the question being asked at the start of the interview \
or at the very end? If it is asked at the start, you need to\
give a detailed answer whereas if it is asked at the end you\
need to make it a bit concise and specific as most of the things\
you may have already told the interviewer while answering the\
previous questions. Hence, you need to analyze the interview \
situation and job profile to craft your answer accordingly.\
For more : https://www.geeksforgeeks.org/how-to-answer-why-\
should-we-hire-you-in-an-interview/")
输出 :
示例 2:文本样式和加载动画
mux_format()(上面解释过)可用于格式化文本。可以使用mux_progessbar装饰器使用加载动画延迟日志。
蟒蛇3
# importing library
import logging
from mux import MuxStreamHandler, mux_progressbar, mux_format
import time
# setup loggers
logger = logging.getLogger(__name__)
handler = MuxStreamHandler()
handler.setLevel(logging.DEBUG)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
# demonstrating loading bar
@mux_progressbar
def add_delay():
time.sleep(2)
add_delay()
# formatting text
logger.info("Best Place to Study CS is {s}".format(
s = mux_format("Geeksforgeeks", "magenta", "underline")))
输出 :