logger.py 为自己写的一个log模块:
import logging
import os,time
from com.jd.ofc.util import functions
#create logger
logger = logging.getLogger("client")
logger.setLevel(logging.INFO)
#create console handler and set level to error
console_handle = logging.StreamHandler()
console_handle.setLevel(logging.INFO)
#create file handler and set level to debug
file_handle = logging.FileHandler(os.path.join(functions.get_appPath(),'log','automationtest_%s.log' % (time.strftime('%Y-%m-%d',time.localtime(time.time())))))
file_handle.setLevel(logging.DEBUG)
#create formatter
formatter = logging.Formatter("[%(asctime)s][%(levelname)s][%(module)s:%(funcName)s:%(lineno)d]%(message)s")
#add formatter to ch and fh
console_handle.setFormatter(formatter)
file_handle.setFormatter(formatter)
#add ch and fh to logger
logger.addHandler(console_handle)
logger.addHandler(file_handle)
logger.info('dd中国dd'+str(len('dddd')))
logger.debug('debug')
logger.error('errr ror')
在eclipse里面执行一点问题也没有。
通过windows cmd命令行窗口执行时,log带中文的字符时报错了,如下图:
log英文字符时,没有问题,只有log中文时出现IOError: [Errno 0] Error的错误,请各位大侠帮忙看看,谢谢了
要设置窗口字符集:
chcp 65001
set PYTHONIOENCODING=utf-8