From c700b91c3d5f4a6f9aa854e76b22215475b96d5e Mon Sep 17 00:00:00 2001 From: jous_1110 Date: Fri, 27 Oct 2023 12:32:55 +0800 Subject: [PATCH] modification on DepthMod, sort in functions, add doctest function --- seispy/setuplog.py | 57 +++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/seispy/setuplog.py b/seispy/setuplog.py index c7674049..8f757309 100644 --- a/seispy/setuplog.py +++ b/seispy/setuplog.py @@ -3,41 +3,42 @@ class setuplog(object): + default_logs={ + "RF2depthlog":("RF2depth","INFO","stream_handler"), + "RFlog":("RF","INFO","stream_handler"), + "Batlog":("Bat","INFO","stream_handler","file_handler"), + "CCPlog":("CCP","INFO","stream_handler"), + "ModCreatorlog":("ModCreator","INFO","stream_handler"), + "PickDepthlog": ("PickDepth", "INFO","stream_handler") + } def __init__(self, filename=join(expanduser('~'), '.RF.log')): + """ + use default_logs to gen loggers + change default_logs for future changes if needed, + check logger level with logger.getEffectiveLevel + + """ self.filename = filename fh = logging.FileHandler(filename) ch = logging.StreamHandler() formatter = logging.Formatter('%(asctime)s [%(name)s] %(levelname)s: %(message)s') fh.setFormatter(formatter) ch.setFormatter(formatter) - self.RF2depthlog = logging.getLogger('RF2depth') - if not self.RF2depthlog.handlers: - self.RF2depthlog.setLevel(logging.INFO) - self.RF2depthlog.addHandler(ch) - self.RFlog = logging.getLogger('RF') - if not self.RFlog.handlers: - self.RFlog.setLevel(logging.INFO) - # self.RFlog.removeHandler(ch) - self.RFlog.addHandler(ch) - self.Batlog = logging.getLogger('Bat') - if not self.Batlog.handlers: - self.Batlog.setLevel(logging.INFO) - # self.Batlog.removeHandler(ch) - # self.Batlog.removeHandler(fh) - self.Batlog.addHandler(ch) - self.Batlog.addHandler(fh) - self.CCPlog = logging.getLogger('CCP') - if not self.CCPlog.handlers: - self.CCPlog.setLevel(logging.INFO) - self.CCPlog.addHandler(ch) - self.ModCreatorlog = logging.getLogger('ModCreator') - if not self.ModCreatorlog.handlers: - self.ModCreatorlog.setLevel(logging.INFO) - self.ModCreatorlog.addHandler(ch) - self.PickDepthlog = logging.getLogger('PickDepth') - if not self.PickDepthlog.handlers: - self.PickDepthlog.setLevel(logging.INFO) - # self.PickDepthlog.addHandler(ch) + + for loger_branch, config in self.default_logs.items(): + # init, setlevel + log = logging.getLogger(config[0]) + log.setLevel(config[1]) + next if log.hasHandlers else None + + # add handler + if "file_handler" in config: + log.addHandler(fh) + if "stream_handler" in config: + log.addHandler(ch) + + # attach to class + setattr(self,loger_branch,log) if __name__ == '__main__':