python-设置log
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python-设置log相关的知识,希望对你有一定的参考价值。
#-*- coding: UTF-8 -*- __author__ = ‘007‘ __date__ = ‘2016/6/16‘ import logging import ctypes #STD_INPUT_HANDLE = -10 STD_OUTPUT_HANDLE= -11 #STD_ERROR_HANDLE = -12 FOREGROUND_WHITE = 0x0007 FOREGROUND_BLUE = 0x01 # text color contains blue. FOREGROUND_GREEN= 0x02 # text color contains green. FOREGROUND_RED = 0x04 # text color contains red. FOREGROUND_INTENSITY = 0x08 # text color is intensified. FOREGROUND_YELLOW = FOREGROUND_RED | FOREGROUND_GREEN BACKGROUND_BLUE = 0x10 # background color contains blue. BACKGROUND_GREEN= 0x20 # background color contains green. BACKGROUND_RED = 0x40 # background color contains red. BACKGROUND_INTENSITY = 0x80 # background color is intensified. std_out_handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE) def set_color(color,handle = std_out_handle): bb = ctypes.windll.kernel32.SetConsoleTextAttribute(handle,color) return bb class logger: def __init__(self,name,path,clevel=logging.DEBUG,flevel=logging.DEBUG): self.logger = logging.getLogger(name) self.logger.setLevel(logging.DEBUG) fmt = logging.Formatter(‘%(asctime)s - %(name)s - %(levelname)s - %(message)s‘) ch = logging.StreamHandler() ch.setFormatter(fmt) ch.setLevel(clevel) fh = logging.FileHandler(path) fh.setFormatter(fmt) fh.setLevel(flevel) self.logger.addHandler(ch) self.logger.addHandler(fh) def debug(self,message): self.logger.debug(message) def info(self,message,color=FOREGROUND_GREEN): set_color(color) self.logger.info(message) set_color(FOREGROUND_WHITE) def warn(self,message,color=FOREGROUND_YELLOW): set_color(color) self.logger.warn(message) set_color(FOREGROUND_WHITE) def error(self,message,color=FOREGROUND_RED): set_color(color) self.logger.error(message) set_color(FOREGROUND_WHITE) def critical(self,message,color=FOREGROUND_BLUE): set_color(color) self.logger.critical(message) set_color(FOREGROUND_WHITE) if __name__ == "__main__": logger1 = logger(‘one‘,‘test.log‘,logging.DEBUG,logging.DEBUG) logger1.debug("logger1 debug message") logger1.info("logger1 info message") logger1.warn("logger1 warn message") logger1.error("logger1 error message") logger1.critical("logger1 critical message") logger2 = logger(‘two‘,‘test.log‘,logging.INFO,logging.INFO) logger2.debug("logger2 debug message") logger2.info("logger2 info message") logger2.warn("logger2 warn message") logger2.error("logger2 error message") logger2.critical("logger2 critical message") logger3 = logger(‘three‘,"test.log",logging.WARNING) logger3.debug("logger3 debug message") logger3.info("logger3 info message") logger3.warn("logger3 warn message") logger3.error("logger3 error message") logger3.critical("logger3 critical message") logger4 = logger(‘four‘,"test.log",logging.ERROR,logging.ERROR) logger4.debug("logger4 debug message") logger4.info("logger4 info message") logger4.warn("logger4 warn message") logger4.error("logger4 error message") logger4.critical("logger4 critical message") logger5 = logger(‘five‘,"test.log",logging.CRITICAL,logging.CRITICAL) logger5.debug("logger5 debug message") logger5.info("logger5 info message") logger5.warn("logger5 warn message") logger5.error("logger5 error message") logger5.critical("logger5 critical message")
以上是关于python-设置log的主要内容,如果未能解决你的问题,请参考以下文章
Android 逆向使用 Python 解析 ELF 文件 ( Capstone 反汇编 ELF 文件中的机器码数据 | 创建反汇编解析器实例对象 | 设置汇编解析器显示细节 )(代码片段
我的Android进阶之旅NDK开发之在C++代码中使用Android Log打印日志,打印出C++的函数耗时以及代码片段耗时详情