From 2b956f60899664aa6166bdb4607b0779900492d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B2=88=E7=91=97=E6=9D=B0?= <3695888@qq.com> Date: Sun, 14 Feb 2021 22:15:05 +0800 Subject: [PATCH] ruuuuuuuuuuuuuuuuuuaaaa! logggggggggggggggggggging! --- Difficult_Rocket.py | 3 +-- bin/client.py | 18 ++++++++---------- bin/main.py | 38 ++++++++------------------------------ bin/tools.py | 8 ++++---- tests/test_json5.py | 2 +- tests/test_logging.py | 32 ++++++++++++++++++++++++++++++++ 6 files changed, 54 insertions(+), 47 deletions(-) create mode 100644 tests/test_logging.py diff --git a/Difficult_Rocket.py b/Difficult_Rocket.py index 0182065..ab5a73e 100644 --- a/Difficult_Rocket.py +++ b/Difficult_Rocket.py @@ -3,8 +3,7 @@ writen by shenjackyuanjie mail: 3695888@qq.com ''' -import os -import bin + import sys from bin import main diff --git a/bin/client.py b/bin/client.py index dc44c40..5a0d738 100644 --- a/bin/client.py +++ b/bin/client.py @@ -32,7 +32,6 @@ class RenderThread(mp.Process, pyglet.window.Window): :param net_mode: 网络模式 # local / ip ''' # do father class __init__() - Window.__init__(self) mp.Process.__init__(self) # logging self.logger = logger @@ -52,7 +51,7 @@ class RenderThread(mp.Process, pyglet.window.Window): self.window_config = tools.config('sys_value/window.json5') # dic self.ships = {} # all ship(part) - self.planet_system = tools.configs('sys_vlaue/planet.json5') # hole planet system + self.planet_system = tools.config('sys_value/planet.json5') # hole planet system # list # re stuff self.ipv4_re = re.compile( @@ -82,18 +81,17 @@ class RenderThread(mp.Process, pyglet.window.Window): self.textures = {} # parts self.textures['part'] = {} - parts = tools.configs('sys_value/parts.json5') + parts = tools.config('sys_value/parts.json5') for part in parts: - name = parts[part] - path = part[2][0] + path = parts[part][2][0] + path = 'textures/' + path part_image = image.load(path) - self.textures['part'][name] = part_image - ''' - draws - ''' + self.textures['part'][part] = part_image + + # draws def on_draw(self): - self.logger.info('testing!') + print('rua!') def build_draw(self): pass diff --git a/bin/main.py b/bin/main.py index 003dbf7..70a34ad 100644 --- a/bin/main.py +++ b/bin/main.py @@ -32,37 +32,15 @@ class Game: self.dicts = share().dict() self.lists = share().list() # logger - # log config self.log_config = tools.config('configs/logging.json5') - self.log_file_config = self.log_config['file'] - self.log_file_handler = logging.FileHandler('logs/%s' % (configs.name_handler( - self.log_file_config['filename']['main'], self.log_file_config['filename']['formats']))) - # logger - # all logger - # client logger - self.client_log_config = self.log_config['client'] - self.client_logger = logging.getLogger('client') - self.client_fmt = logging.Formatter( - fmt=self.client_log_config['fmt'], - datefmt=self.client_log_config['date_fmt']) - self.client_stream_handler = logging.StreamHandler() - self.client_stream_handler.setLevel(self.client_log_config['level']) - self.client_stream_handler.setFormatter(self.client_fmt) - self.client_logger.addHandler(self.client_stream_handler) - # server logger - self.server_log_config = self.log_config['server'] - self.server_logger = logging.getLogger('server') - self.server_fmt = logging.Formatter( - fmt=self.server_log_config['fmt'], - datefmt=self.server_log_config['date_fmt']) - self.server_stream_handler = logging.StreamHandler() - self.server_stream_handler.setLevel(self.server_log_config['level']) - self.server_stream_handler.setFormatter(self.server_fmt) - self.server_logger.addHandler(self.server_stream_handler) - # file logger - self.log_formatter = logging.Formatter( - fmt=self.log_file_config['fmt'], - datefmt=self.log_file_config['date_fmt']) + logging.basicConfig(filename=('logs/'+configs.name_handler(self.log_config['file']['filename']['main'], self.log_config['file']['filename']['formats'])), + level=logging.DEBUG) + self.root_logger_stream_hander = logging.StreamHandler() + self.root_logger = logging.getLogger() + logging.info('rua!') + self.root_logger.info('aaaaa') + self.server_logger = logging.getLogger() + self.client_logger = logging.getLogger() # client and server self.client = client.RenderThread( self.client_logger, self.dicts, self.lists, net_mode='local') diff --git a/bin/tools.py b/bin/tools.py index c5205d0..8225be8 100644 --- a/bin/tools.py +++ b/bin/tools.py @@ -23,14 +23,14 @@ some tools def c_b(thing): # stand for my bool - yes = ['True', 'TRUE', '1', 1] - no = ['False', 'FALSE', '0', 0] + yes = ['True', 'TRUE', 'true', '1', 1, True] + no = ['False', 'FALSE', 'false', '0', 0, False] if thing in yes: return True elif thing in no: return False else: - raise ValueError('Need a 'like bool' not anything else') + raise ValueError("Need a 'like bool' not anything else") ''' @@ -180,7 +180,7 @@ def config(file_name, stack=None): type = file_name[file_name.rfind('.') + 1:] # 从最后一个.到末尾 (截取文件格式) if (type == 'json5') or (type == 'json'): try: - with open(file_name, 'r') as jf: # jf -> json file + with open(file_name, 'r', encoding='utf-8') as jf: # jf -> json file rd = json5.load(jf) except FileNotFoundError as exp: log = 'no config json(5) file \n file name : %s \n stack : %s' % (file_name, stack) diff --git a/tests/test_json5.py b/tests/test_json5.py index d90d676..7b2656e 100644 --- a/tests/test_json5.py +++ b/tests/test_json5.py @@ -3,6 +3,6 @@ import json5 import pprint -with open('configs/view.json5') as view: +with open('sys_value/parts.json5', 'r+', encoding='utf-8') as view: a = view.read() pprint.pprint(a) \ No newline at end of file diff --git a/tests/test_logging.py b/tests/test_logging.py new file mode 100644 index 0000000..7e3ed69 --- /dev/null +++ b/tests/test_logging.py @@ -0,0 +1,32 @@ + +import logging + +# set up logging to file - see previous section for more details +logging.basicConfig(level=logging.DEBUG, + format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', + datefmt='%m-%d %H:%M', + filename='test.log', + filemode='w') +# define a Handler which writes INFO messages or higher to the sys.stderr +console = logging.StreamHandler() +console.setLevel(logging.INFO) +# set a format which is simpler for console use +formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s') +# tell the handler to use this format +console.setFormatter(formatter) +# add the handler to the root logger +logging.getLogger('').addHandler(console) + +# Now, we can log to the root logger, or any other logger. First the root... +logging.info('Jackdaws love my big sphinx of quartz.') + +# Now, define a couple of other loggers which might represent areas in your +# application: + +logger1 = logging.getLogger('myapp.area1') +logger2 = logging.getLogger('myapp.area2') + +logger1.debug('Quick zephyrs blow, vexing daft Jim.') +logger1.info('How quickly daft jumping zebras vex.') +logger2.warning('Jail zesty vixen who grabbed pay from quack.') +logger2.error('The five boxing wizards jump quickly.')