正在将Unicode写入我的文件

时间:2017-09-29 19:34:55

标签: python logging unicode

我有一个写入输出并写入文件的程序。输出使用如下函数着色:

def set_color(org_string, level=None):
    """
    set the console log color, this will kinda mess with the file log but whatever
    """
    color_levels = {
        10: "\033[36m{}\033[0m",       # DEBUG
        20: "\033[32m{}\033[0m",       # INFO *default
        30: "\033[33m{}\033[0m",       # WARNING
        40: "\033[31m{}\033[0m",       # ERROR
        50: "\033[7;31;31m{}\033[0m"   # FATAL/CRITICAL/EXCEPTION
    }
    if level is None:
        return color_levels[20].format(org_string)
    else:
        return color_levels[int(level)].format(org_string)

你如何使用它是使用记录器:

# console logger and file logger settings
logger = logging.getLogger("zeus-log")
logger.setLevel(logging.DEBUG)
file_handler = logging.FileHandler(
    filename=create_log_name(), mode="a+"
)
file_handler.setLevel(logging.DEBUG)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
file_format = logging.Formatter(
    '%(asctime)s;%(name)s;%(levelname)s;%(message)s'
)
console_format = logging.Formatter(
    "[%(asctime)s %(levelname)s] %(message)s", "%H:%M:%S"
)
file_handler.setFormatter(file_format)
console_handler.setFormatter(console_format)
logger.addHandler(console_handler)
logger.addHandler(file_handler)

调用记录器时,设置颜色:

logger.info(set_color(
    "test"
))

将输出绿色文字或您想要的任何颜色。问题是当它写入日志文件时,它还会写出set_color函数信息,输出如下所示:

2017-09-29 14:25:50,277;zeus-log;DEBUG;[36mchecking if the application has been run before...[0m
2017-09-29 14:25:50,278;zeus-log;DEBUG;[36mverifying operating system...[0m
2017-09-29 14:25:50,278;zeus-log;DEBUG;[36malready ran, skipping...[0m
2017-09-29 14:25:50,279;zeus-log;DEBUG;[36mrunning with options '{'runInVerbose': True, 'useRandomAgent': True, 'dorkToUse': 'inurl:php?id=10'}'...[0m
2017-09-29 14:25:50,280;zeus-log;INFO;[32mlog file being saved to '/home/baal/bin/python/zeus-scanner/log/zeus-log-47.log'...[0m
2017-09-29 14:25:50,280;zeus-log;DEBUG;[36mgrabbing random user-agent from '/home/baal/bin/python/zeus-scanner/etc/agents.txt'...[0m
2017-09-29 14:25:50,281;zeus-log;DEBUG;[36musing default search engine (Google)...[0m
2017-09-29 14:25:50,282;zeus-log;INFO;[32musing default search engine...[0m
2017-09-29 14:25:50,282;zeus-log;INFO;[32mstarting dork scan with query 'inurl:php?id=10'...[0m
2017-09-29 14:25:50,282;zeus-log;DEBUG;[36mchecking for user-agent and proxy configuration...[0m
2017-09-29 14:25:50,285;zeus-log;INFO;[32mattempting to gather query URL...[0m

有没有办法可以写入日志文件并删除unicode字符串以及颜色调用([<NUM>m)?

为了澄清SO从上面的输出中删除了问题,这是问题的图像:

data

正如您所看到的,ANSI转义序列前面有奇怪的正方形

1 个答案:

答案 0 :(得分:2)

最简单的解决方案是修改set_color函数,使其返回未修改的字符串,不添加任何颜色序列。根据评论显示,这不是一个选项。

将文件写入磁盘后,只需一个Linux命令即可删除这些转义序列:

sed -r 's/.\[[0-9]+m//' logfile