def get_exchangerate(url='http://rate.bot.com.tw/xrt?Lang=zh-TW'): """ get Taiwan bank exchange rate. """ log_path = os.path.join(os.getcwd(), 'logs', log_time() + '-' + str(uuid.uuid1()) + '.log') lazy_logger.log_to_console(logger) lazy_logger.log_to_rotated_file(logger=logger, file_name=log_path) logger.info('logger file: {0}'.format(log_path)) try: logger.info('start connect {}'.format(url)) resp = requests.get('http://rate.bot.com.tw/xrt?Lang=zh-TW') logger.info('connect success, get exchange') soup = BeautifulSoup(resp.text, 'html.parser') rows = soup.find('table', 'table').tbody.find_all('tr') rateset = [] for row in rows: logger.info("取得: {}".format(list(row.stripped_strings)[0])) rateset.append(" ,".join([s for s in row.stripped_strings])) currency = '\n'.join(rateset) logger.info('get exchange success') except Exception as e: ret = OrderedDict((('ret', -1), ('status', e), ('version', ''))) logs = get_log(file=log_path, title='get_exchangerate_job') ret.update(logs) return ret ret = OrderedDict((('ret', 0), ('status', 'success.'), ('values', currency), ('version', '1.00'))) logs = get_log(file=log_path, title='get_exchangerate_job') ret.update(logs) return ret
def lazylog(*args, **kwargs): log_path = os.path.join(os.getcwd(), 'logs', log_time() + '-' + str(uuid.uuid1()) + '.log') lazy_logger.log_to_console(logger) lazy_logger.log_to_rotated_file(logger=logger, file_name=log_path) logger.info('logger file: {0}'.format(log_path)) kwargs['log_path'] = log_path return f(*args, **kwargs)
def test_log_to_rotated_file(tmpdir): logger = lazy_logger.get_logger('noname') file_path = tmpdir.join(lazy_logger.FILE_NAME).strpath lazy_logger.log_to_rotated_file(logger, lazy_logger.LOGGER_LEVEL, lazy_logger.HANDLER_LEVEL, '%(message)s', 100, file_path) logger.debug('y'*100) logger.debug('y') log_out_1 = open(file_path+'.1', 'r') assert log_out_1.read() == 'y'*100+'\n' log_out_1.close() log_out = open(file_path, 'r') assert log_out.read() == 'y'+'\n'
def job(url): log_path = os.path.join(os.getcwd(), 'logs', log_time() + '-' + str(uuid.uuid1()) + '.log') lazy_logger.log_to_console(logger) lazy_logger.log_to_rotated_file(logger=logger, file_name=log_path) logger.info('logger file: {0}'.format(log_path)) crawler = Crawler() driver = crawler.driver() driver.get(url) pageSource = driver.page_source soup = bs(pageSource, "html.parser") print('{}'.format(soup.title)) driver.close() ret = OrderedDict((('ret', 0), ('status', 'Success'), ('version', '0.1'))) logs = get_log(file=log_path, title='crawler_job') ret.update(logs) return ret
# 写入文件 def case2(): logger = logging.getLogger('mylogger') logger.setLevel(logging.DEBUG) f_handler = logging.FileHandler('logging.log') f_handler.setLevel(logging.DEBUG) formatter = logging.Formatter('[%(asctime)s][%(filename)s][line: %(lineno)d]\[%(levelname)s] ## %(message)s') f_handler.setFormatter(formatter) logger.addHandler(f_handler) logger.debug('debug foobar 2') # lazy loger 的用法 logger = lazy_logger.get_logger() # lazy_logger.log_to_console(logger) FORMATER='[%(asctime)s][%(filename)s][line: %(lineno)d]\[%(levelname)s] ## %(message)s)' lazy_logger.log_to_rotated_file(logger,logging_format=FORMATER,file_name='lazy_logger_file.log') @logger.patch def lazy_logger_usage(): print('test file') def main(): # case1() lazy_logger_usage() if __name__ == '__main__': main()
import lazy_logger import sys logger = lazy_logger.get_logger(__name__) lazy_logger.log_to_console(logger) lazy_logger.log_to_rotated_file(logger) # create log file log.out @logger.patch def main(): print('Hello World!') # expect acting as logger print('Hello stdout!', file=sys.stdout) # expect acting as normal print if __name__ == '__main__': main()
formatter = logging.Formatter( '[%(asctime)s][%(filename)s][line: %(lineno)d]\[%(levelname)s] ## %(message)s' ) f_handler.setFormatter(formatter) logger.addHandler(f_handler) logger.debug('debug foobar 2') # lazy loger 的用法 logger = lazy_logger.get_logger() # lazy_logger.log_to_console(logger) FORMATER = '[%(asctime)s][%(filename)s][line: %(lineno)d]\[%(levelname)s] ## %(message)s)' lazy_logger.log_to_rotated_file(logger, logging_format=FORMATER, file_name='lazy_logger_file.log') @logger.patch def lazy_logger_usage(): print('test file') def main(): # case1() lazy_logger_usage() if __name__ == '__main__': main()