log.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #-----------------------------------------------------------------------------
  2. # Copyright (c) 2013-2023, PyInstaller Development Team.
  3. #
  4. # Distributed under the terms of the GNU General Public License (version 2
  5. # or later) with exception for distributing the bootloader.
  6. #
  7. # The full license is in the file COPYING.txt, distributed with this software.
  8. #
  9. # SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
  10. #-----------------------------------------------------------------------------
  11. """
  12. Logging module for PyInstaller.
  13. """
  14. __all__ = ['getLogger', 'INFO', 'WARN', 'DEBUG', 'TRACE', 'ERROR', 'FATAL', 'DEPRECATION']
  15. import os
  16. import logging
  17. from logging import DEBUG, ERROR, FATAL, INFO, WARN, getLogger
  18. TRACE = DEBUG - 5
  19. logging.addLevelName(TRACE, 'TRACE')
  20. DEPRECATION = WARN + 5
  21. logging.addLevelName(DEPRECATION, 'DEPRECATION')
  22. LEVELS = {
  23. 'TRACE': TRACE,
  24. 'DEBUG': DEBUG,
  25. 'INFO': INFO,
  26. 'WARN': WARN,
  27. 'DEPRECATION': DEPRECATION,
  28. 'ERROR': ERROR,
  29. 'FATAL': FATAL,
  30. }
  31. FORMAT = '%(relativeCreated)d %(levelname)s: %(message)s'
  32. _env_level = os.environ.get("PYI_LOG_LEVEL", "INFO")
  33. try:
  34. level = LEVELS[_env_level.upper()]
  35. except KeyError:
  36. raise SystemExit(f"ERROR: Invalid PYI_LOG_LEVEL value '{_env_level}'. Should be one of {list(LEVELS)}.")
  37. logging.basicConfig(format=FORMAT, level=level)
  38. logger = getLogger('PyInstaller')
  39. def __add_options(parser):
  40. parser.add_argument(
  41. '--log-level',
  42. choices=LEVELS,
  43. metavar="LEVEL",
  44. dest='loglevel',
  45. help='Amount of detail in build-time console messages. LEVEL may be one of %s (default: INFO). '
  46. 'Also settable via and overrides the PYI_LOG_LEVEL environment variable.' % ', '.join(LEVELS),
  47. )
  48. def __process_options(parser, opts):
  49. if opts.loglevel:
  50. try:
  51. level = opts.loglevel.upper()
  52. _level = LEVELS[level]
  53. except KeyError:
  54. parser.error('Unknown log level `%s`' % opts.loglevel)
  55. logger.setLevel(_level)
  56. os.environ["PYI_LOG_LEVEL"] = level