# %%
      import logging
      
      def get_logger(log_file, logger='default'):
        logger = logging.getLogger()  # d8
      e
       e
      g'0h.>g=.root logger
        logger.setLevel(logging.INFO)
        formatter = logging.Formatter(
          '%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s',
          datefmt='%Y-%m-%d %H:%M:%S')
        # d=?g(FileHandlerh>e:e0fd;6
        fh = logging.FileHandler(log_file)
        fh.setLevel(logging.INFO)
        fh.setFormatter(formatter)
      
        # d=?g(StreamHandlerh>e:e0e1e9
        ch = logging.StreamHandler()
        ch.setLevel(logging.INFO)
        ch.setFormatter(formatter)
      
        logger.addHandler(ch)
        logger.addHandler(fh)
        return logger
      
      
      # %%
      from logging import config
      import logging
      
      def get_log_cfg(log_file: str):
        return {
          "version": 1,
          "disable_existing_loggers": True,
          "formatters": {
            "simple": {
              "format": "%(asctime)s %(name)s %(levelname)s %(message)s",
              "datefmt": "%Y-%m-%d %H:%M:%S",
            }
          },
          "handlers": {
            "console": {
              "class": "logging.StreamHandler",
              "level": "INFO",
              "formatter": "simple",
              "stream": "ext://sys.stdout",
            },
            "info_file_handler": {
              "class": "logging.handlers.TimedRotatingFileHandler",
              "level": "INFO",
              "formatter": "simple",
              "filename": log_file,
              "when": "D",
              "backupCount": 3,
              "encoding": "utf8",
            },
          },
          "root": {"level": "INFO", "handlers": ["console", "info_file_handler"]},
        }
      
      config.dictConfig(get_log_cfg("log.log"))
      logger = logging.getLogger("main")

      Python language logo
      python log

      logger.setLevel(logging.INFO) logger.addHandler(logging.StreamHandler()) logger.addHandler(logging.FileHandler("log.log"))

      def main(): logger = get_logger("main") for filename in os.listdir("":): if filename not in logger.handlers: logger.addHandler(logging.StreamHandler()) elif filename.endswith("*.log"): logger.addHandler(logging.FileHandler("log.log" + filename)) else: logger.addHandler(logging.

      0 Comments

        Add Comment

        Log in to add a comment

        Codiga - All rights reserved 2022.