Make it possible to set custom logger

This commit is contained in:
taizan-hokuto
2020-01-18 12:38:36 +09:00
parent 2573cc18de
commit efdf07e3de
5 changed files with 36 additions and 36 deletions

View File

@@ -1,13 +1,11 @@
import logging import logging
from . import mylogger from . import mylogger
LOGGER_MODE = None
headers = { headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36'} 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36'}
def logger(module_name: str): def logger(module_name: str, loglevel = None):
module_logger = mylogger.get_logger(module_name, mode = LOGGER_MODE) module_logger = mylogger.get_logger(module_name, loglevel = loglevel)
return module_logger return module_logger

View File

@@ -3,21 +3,21 @@ import logging
import datetime import datetime
def get_logger(modname,mode=logging.DEBUG): def get_logger(modname,loglevel=logging.DEBUG):
logger = getLogger(modname) logger = getLogger(modname)
if mode == None: if loglevel == None:
logger.addHandler(NullHandler()) logger.addHandler(NullHandler())
return logger return logger
logger.setLevel(mode) logger.setLevel(loglevel)
#create handler1 for showing info #create handler1 for showing info
handler1 = StreamHandler() handler1 = StreamHandler()
my_formatter = MyFormatter() my_formatter = MyFormatter()
handler1.setFormatter(my_formatter) handler1.setFormatter(my_formatter)
handler1.setLevel(mode) handler1.setLevel(loglevel)
logger.addHandler(handler1) logger.addHandler(handler1)
#create handler2 for recording log file #create handler2 for recording log file
if mode <= logging.DEBUG: if loglevel <= logging.DEBUG:
handler2 = FileHandler(filename="log.txt", encoding='utf-8') handler2 = FileHandler(filename="log.txt", encoding='utf-8')
handler2.setLevel(logging.ERROR) handler2.setLevel(logging.ERROR)
handler2.setFormatter(my_formatter) handler2.setFormatter(my_formatter)

View File

@@ -17,7 +17,6 @@ from ..paramgen import liveparam, arcparam
from ..processors.default.processor import DefaultProcessor from ..processors.default.processor import DefaultProcessor
from ..processors.combinator import Combinator from ..processors.combinator import Combinator
logger = config.logger(__name__)
headers = config.headers headers = config.headers
MAX_RETRY = 10 MAX_RETRY = 10
@@ -74,6 +73,7 @@ class LiveChatAsync:
''' '''
_setup_finished = False _setup_finished = False
_logger = config.logger(__name__)
def __init__(self, video_id, def __init__(self, video_id,
seektime = 0, seektime = 0,
@@ -85,7 +85,8 @@ class LiveChatAsync:
exception_handler = None, exception_handler = None,
direct_mode = False, direct_mode = False,
force_replay = False, force_replay = False,
topchat_only = False topchat_only = False,
logger = config.logger(__name__),
): ):
self.video_id = video_id self.video_id = video_id
self.seektime = seektime self.seektime = seektime
@@ -107,6 +108,9 @@ class LiveChatAsync:
self._first_fetch = True self._first_fetch = True
self._fetch_url = "live_chat/get_live_chat?continuation=" self._fetch_url = "live_chat/get_live_chat?continuation="
self._topchat_only = topchat_only self._topchat_only = topchat_only
self._logger = logger
LiveChatAsync._logger = logger
if not LiveChatAsync._setup_finished: if not LiveChatAsync._setup_finished:
LiveChatAsync._setup_finished = True LiveChatAsync._setup_finished = True
if exception_handler: if exception_handler:
@@ -185,14 +189,14 @@ class LiveChatAsync:
continuation = metadata.get('continuation') continuation = metadata.get('continuation')
except ChatParseException as e: except ChatParseException as e:
#self.terminate() #self.terminate()
logger.debug(f"[{self.video_id}]{str(e)}") self._logger.debug(f"[{self.video_id}]{str(e)}")
return return
except (TypeError , json.JSONDecodeError) : except (TypeError , json.JSONDecodeError) :
#self.terminate() #self.terminate()
logger.error(f"{traceback.format_exc(limit = -1)}") self._logger.error(f"{traceback.format_exc(limit = -1)}")
return return
logger.debug(f"[{self.video_id}]finished fetching chat.") self._logger.debug(f"[{self.video_id}]finished fetching chat.")
async def _check_pause(self, continuation): async def _check_pause(self, continuation):
if self._pauser.empty(): if self._pauser.empty():
@@ -252,7 +256,7 @@ class LiveChatAsync:
await asyncio.sleep(1) await asyncio.sleep(1)
continue continue
else: else:
logger.error(f"[{self.video_id}]" self._logger.error(f"[{self.video_id}]"
f"Exceeded retry count. status_code={status_code}") f"Exceeded retry count. status_code={status_code}")
return None return None
return livechat_json return livechat_json
@@ -307,7 +311,7 @@ class LiveChatAsync:
try: try:
self.terminate() self.terminate()
except CancelledError: except CancelledError:
logger.debug(f'[{self.video_id}]cancelled:{sender}') self._logger.debug(f'[{self.video_id}]cancelled:{sender}')
def terminate(self): def terminate(self):
''' '''
@@ -317,7 +321,7 @@ class LiveChatAsync:
if self._direct_mode == False: if self._direct_mode == False:
#bufferにダミーオブジェクトを入れてis_alive()を判定させる #bufferにダミーオブジェクトを入れてis_alive()を判定させる
self._buffer.put_nowait({'chatdata':'','timeout':0}) self._buffer.put_nowait({'chatdata':'','timeout':0})
logger.info(f'[{self.video_id}]finished.') self._logger.info(f'[{self.video_id}]finished.')
@classmethod @classmethod
def _set_exception_handler(cls, handler): def _set_exception_handler(cls, handler):
@@ -326,12 +330,12 @@ class LiveChatAsync:
@classmethod @classmethod
async def shutdown(cls, event, sig = None, handler=None): async def shutdown(cls, event, sig = None, handler=None):
logger.debug("shutdown...") cls._logger.debug("shutdown...")
tasks = [t for t in asyncio.all_tasks() if t is not tasks = [t for t in asyncio.all_tasks() if t is not
asyncio.current_task()] asyncio.current_task()]
[task.cancel() for task in tasks] [task.cancel() for task in tasks]
logger.debug(f"complete remaining tasks...") cls._logger.debug(f"complete remaining tasks...")
await asyncio.gather(*tasks,return_exceptions=True) await asyncio.gather(*tasks,return_exceptions=True)
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
loop.stop() loop.stop()

View File

@@ -16,7 +16,6 @@ from ..paramgen import liveparam, arcparam
from ..processors.default.processor import DefaultProcessor from ..processors.default.processor import DefaultProcessor
from ..processors.combinator import Combinator from ..processors.combinator import Combinator
logger = config.logger(__name__)
headers = config.headers headers = config.headers
MAX_RETRY = 10 MAX_RETRY = 10
@@ -75,6 +74,8 @@ class LiveChat:
_setup_finished = False _setup_finished = False
#チャット監視中のListenerのリスト #チャット監視中のListenerのリスト
_listeners = [] _listeners = []
_logger = config.logger(__name__)
def __init__(self, video_id, def __init__(self, video_id,
seektime = 0, seektime = 0,
processor = DefaultProcessor(), processor = DefaultProcessor(),
@@ -84,7 +85,8 @@ class LiveChat:
done_callback = None, done_callback = None,
direct_mode = False, direct_mode = False,
force_replay = False, force_replay = False,
topchat_only = False topchat_only = False,
logger = config.logger(__name__)
): ):
self.video_id = video_id self.video_id = video_id
self.seektime = seektime self.seektime = seektime
@@ -106,6 +108,8 @@ class LiveChat:
self._first_fetch = True self._first_fetch = True
self._fetch_url = "live_chat/get_live_chat?continuation=" self._fetch_url = "live_chat/get_live_chat?continuation="
self._topchat_only = topchat_only self._topchat_only = topchat_only
self._logger = logger
LiveChat._logger = logger
if not LiveChat._setup_finished: if not LiveChat._setup_finished:
LiveChat._setup_finished = True LiveChat._setup_finished = True
if interruptable: if interruptable:
@@ -115,7 +119,6 @@ class LiveChat:
LiveChat._listeners.append(self) LiveChat._listeners.append(self)
def _setup(self): def _setup(self):
#logger.debug("setup")
#direct modeがTrueでcallback未設定の場合例外発生。 #direct modeがTrueでcallback未設定の場合例外発生。
if self._direct_mode: if self._direct_mode:
if self._callback is None: if self._callback is None:
@@ -181,13 +184,13 @@ class LiveChat:
time.sleep(diff_time if diff_time > 0 else 0) time.sleep(diff_time if diff_time > 0 else 0)
continuation = metadata.get('continuation') continuation = metadata.get('continuation')
except ChatParseException as e: except ChatParseException as e:
logger.debug(f"[{self.video_id}]{str(e)}") self._logger.debug(f"[{self.video_id}]{str(e)}")
return return
except (TypeError , json.JSONDecodeError) : except (TypeError , json.JSONDecodeError) :
logger.error(f"{traceback.format_exc(limit = -1)}") self._logger.error(f"{traceback.format_exc(limit = -1)}")
return return
logger.debug(f"[{self.video_id}]finished fetching chat.") self._logger.debug(f"[{self.video_id}]finished fetching chat.")
def _check_pause(self, continuation): def _check_pause(self, continuation):
if self._pauser.empty(): if self._pauser.empty():
@@ -244,7 +247,7 @@ class LiveChat:
time.sleep(1) time.sleep(1)
continue continue
else: else:
logger.error(f"[{self.video_id}]" self._logger.error(f"[{self.video_id}]"
f"Exceeded retry count. status_code={status_code}") f"Exceeded retry count. status_code={status_code}")
return None return None
return livechat_json return livechat_json
@@ -299,7 +302,7 @@ class LiveChat:
try: try:
self.terminate() self.terminate()
except CancelledError: except CancelledError:
logger.debug(f'[{self.video_id}]cancelled:{sender}') self._logger.debug(f'[{self.video_id}]cancelled:{sender}')
def terminate(self): def terminate(self):
''' '''
@@ -309,10 +312,10 @@ class LiveChat:
if self._direct_mode == False: if self._direct_mode == False:
#bufferにダミーオブジェクトを入れてis_alive()を判定させる #bufferにダミーオブジェクトを入れてis_alive()を判定させる
self._buffer.put({'chatdata':'','timeout':0}) self._buffer.put({'chatdata':'','timeout':0})
logger.info(f'[{self.video_id}]finished.') self._logger.info(f'[{self.video_id}]finished.')
@classmethod @classmethod
def shutdown(cls, event, sig = None, handler=None): def shutdown(cls, event, sig = None, handler=None):
logger.debug("shutdown...") cls._logger.debug("shutdown...")
for t in LiveChat._listeners: for t in LiveChat._listeners:
t._is_alive = False t._is_alive = False

View File

@@ -5,16 +5,12 @@ Parser of live chat JSON.
""" """
import json import json
from .. import config
from .. exceptions import ( from .. exceptions import (
ResponseContextError, ResponseContextError,
NoContentsException, NoContentsException,
NoContinuationsException, NoContinuationsException,
ChatParseException ) ChatParseException )
logger = config.logger(__name__)
class Parser: class Parser:
__slots__ = ['is_replay'] __slots__ = ['is_replay']
@@ -65,8 +61,7 @@ class Parser:
raise ChatParseException('Finished chat data') raise ChatParseException('Finished chat data')
unknown = list(cont.keys())[0] unknown = list(cont.keys())[0]
if unknown: if unknown:
logger.debug(f"Received unknown continuation type:{unknown}") raise ChatParseException(f"Received unknown continuation type:{unknown}")
metadata = cont.get(unknown)
else: else:
raise ChatParseException('Cannot extract continuation data') raise ChatParseException('Cannot extract continuation data')
return self._create_data(metadata, contents) return self._create_data(metadata, contents)