logging_configurator.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. import abc
  18. import logging
  19. from logging.handlers import TimedRotatingFileHandler
  20. import flask.app
  21. import flask.config
  22. logger = logging.getLogger(__name__)
  23. # pylint: disable=too-few-public-methods
  24. class LoggingConfigurator(abc.ABC):
  25. @abc.abstractmethod
  26. def configure_logging(
  27. self, app_config: flask.config.Config, debug_mode: bool
  28. ) -> None:
  29. pass
  30. class DefaultLoggingConfigurator(LoggingConfigurator):
  31. def configure_logging(
  32. self, app_config: flask.config.Config, debug_mode: bool
  33. ) -> None:
  34. if app_config["SILENCE_FAB"]:
  35. logging.getLogger("flask_appbuilder").setLevel(logging.ERROR)
  36. # configure superset app logger
  37. superset_logger = logging.getLogger("superset")
  38. if debug_mode:
  39. superset_logger.setLevel(logging.DEBUG)
  40. else:
  41. # In production mode, add log handler to sys.stderr.
  42. superset_logger.addHandler(
  43. logging.StreamHandler()
  44. ) # pylint: disable=no-member
  45. superset_logger.setLevel(logging.INFO) # pylint: disable=no-member
  46. logging.getLogger("pyhive.presto").setLevel(logging.INFO)
  47. logging.basicConfig(format=app_config["LOG_FORMAT"])
  48. logging.getLogger().setLevel(app_config["LOG_LEVEL"])
  49. if app_config["ENABLE_TIME_ROTATE"]:
  50. logging.getLogger().setLevel(app_config["TIME_ROTATE_LOG_LEVEL"])
  51. handler = TimedRotatingFileHandler( # type: ignore
  52. app_config["FILENAME"],
  53. when=app_config["ROLLOVER"],
  54. interval=app_config["INTERVAL"],
  55. backupCount=app_config["BACKUP_COUNT"],
  56. )
  57. logging.getLogger().addHandler(handler)
  58. logger.info("logging was configured successfully")