logging字体颜色问题
源代码如下import loggingfrom colorama import Fore,Style
import sys
# 获取对象
def get_logger():
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
if not logger.handlers:
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter(
" %(message)s")
ch.setFormatter(formatter)
logger.addHandler(ch)
return logger
#通过静态成员方法来调用
class Log:
logger = get_logger()
@staticmethod
def debug(msg):
Log.logger.debug(Fore.WHITE + ": " + str(msg) + Style.RESET_ALL)
@staticmethod
def info(msg):
Log.logger.info(Fore.GREEN + ": " + str(msg) + Style.RESET_ALL)
@staticmethod
def warning(msg):
Log.logger.warning("\033: " + str(msg) + "\033[m")
@staticmethod
def error(msg):
Log.logger.error(Fore.RED + ": " + str(msg) + Style.RESET_ALL)
@staticmethod
def critical(msg):
Log.logger.critical(Fore.RED + ": " + str(msg) + Style.RESET_ALL)
Log.info('llllll')
为什么在控制台输出的文字并没有变成绿色,反而前后还有口[32m 和 口[0m??
输出结果为:
: llllll[0m 在 windows 中得用 init 初始化一下
import logging
from colorama import init, Fore,Style # 加上 init
import sys
init(autoreset=True) # 加上这行
# 获取对象
def get_logger():
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
if not logger.handlers:
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter(
" %(message)s")
ch.setFormatter(formatter)
logger.addHandler(ch)
return logger
#通过静态成员方法来调用
class Log:
logger = get_logger()
@staticmethod
def debug(msg):
Log.logger.debug(Fore.WHITE + ": " + str(msg) + Style.RESET_ALL)
@staticmethod
def info(msg):
Log.logger.info(Fore.GREEN + ": " + str(msg) + Style.RESET_ALL)
@staticmethod
def warning(msg):
Log.logger.warning("\033: " + str(msg) + "\033[m")
@staticmethod
def error(msg):
Log.logger.error(Fore.RED + ": " + str(msg) + Style.RESET_ALL)
@staticmethod
def critical(msg):
Log.logger.critical(Fore.RED + ": " + str(msg) + Style.RESET_ALL)
Log.info('llllll') isdkz 发表于 2022-4-2 11:53
在 windows 中得用 init 初始化一下
出来的结果还是这样子的:
: llllll[0m
[0m 2022@lif 发表于 2022-4-2 13:25
出来的结果还是这样子的:
[32m: llllll [0m
可以截个图吗?
https://imgtu.com/
把图放在图床,然后把链接发上来 isdkz 发表于 2022-4-2 13:40
可以截个图吗?
https://imgtu.com/
https://imgtu.com/i/qj86PI 2022@lif 发表于 2022-4-6 13:29
https://imgtu.com/i/qj86PI
你要知道IDLE不支持ANSI颜色代码的,想要显示颜色你得在cmd或者powershell等支持颜色代码的终端上输出。 hrpzcf 发表于 2022-4-6 17:59
你要知道IDLE不支持ANSI颜色代码的,想要显示颜色你得在cmd或者powershell等支持颜色代码的终端上输出。
哦,原来是这样啊
{:10_256:}
页:
[1]