_levels.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # -*- test-case-name: twisted.logger.test.test_levels -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Log levels.
  6. """
  7. from constantly import NamedConstant, Names
  8. class InvalidLogLevelError(Exception):
  9. """
  10. Someone tried to use a L{LogLevel} that is unknown to the logging system.
  11. """
  12. def __init__(self, level):
  13. """
  14. @param level: A log level.
  15. @type level: L{LogLevel}
  16. """
  17. super(InvalidLogLevelError, self).__init__(str(level))
  18. self.level = level
  19. class LogLevel(Names):
  20. """
  21. Constants describing log levels.
  22. @cvar debug: Debugging events: Information of use to a developer of the
  23. software, not generally of interest to someone running the software
  24. unless they are attempting to diagnose a software issue.
  25. @cvar info: Informational events: Routine information about the status of
  26. an application, such as incoming connections, startup of a subsystem,
  27. etc.
  28. @cvar warn: Warning events: Events that may require greater attention than
  29. informational events but are not a systemic failure condition, such as
  30. authorization failures, bad data from a network client, etc. Such
  31. events are of potential interest to system administrators, and should
  32. ideally be phrased in such a way, or documented, so as to indicate an
  33. action that an administrator might take to mitigate the warning.
  34. @cvar error: Error conditions: Events indicating a systemic failure, such
  35. as programming errors in the form of unhandled exceptions, loss of
  36. connectivity to an external system without which no useful work can
  37. proceed, such as a database or API endpoint, or resource exhaustion.
  38. Similarly to warnings, errors that are related to operational
  39. parameters may be actionable to system administrators and should
  40. provide references to resources which an administrator might use to
  41. resolve them.
  42. @cvar critical: Critical failures: Errors indicating systemic failure (ie.
  43. service outage), data corruption, imminent data loss, etc. which must
  44. be handled immediately. This includes errors unanticipated by the
  45. software, such as unhandled exceptions, wherein the cause and
  46. consequences are unknown.
  47. """
  48. debug = NamedConstant()
  49. info = NamedConstant()
  50. warn = NamedConstant()
  51. error = NamedConstant()
  52. critical = NamedConstant()
  53. @classmethod
  54. def levelWithName(cls, name):
  55. """
  56. Get the log level with the given name.
  57. @param name: The name of a log level.
  58. @type name: L{str} (native string)
  59. @return: The L{LogLevel} with the specified C{name}.
  60. @rtype: L{LogLevel}
  61. @raise InvalidLogLevelError: if the C{name} does not name a valid log
  62. level.
  63. """
  64. try:
  65. return cls.lookupByName(name)
  66. except ValueError:
  67. raise InvalidLogLevelError(name)
  68. @classmethod
  69. def _priorityForLevel(cls, level):
  70. """
  71. We want log levels to have defined ordering - the order of definition -
  72. but they aren't value constants (the only value is the name). This is
  73. arguably a bug in Twisted, so this is just a workaround for U{until
  74. this is fixed in some way
  75. <https://twistedmatrix.com/trac/ticket/6523>}.
  76. @param level: A log level.
  77. @type level: L{LogLevel}
  78. @return: A numeric index indicating priority (lower is higher level).
  79. @rtype: L{int}
  80. """
  81. return cls._levelPriorities[level]
  82. LogLevel._levelPriorities = dict(
  83. (level, index) for (index, level) in
  84. (enumerate(LogLevel.iterconstants()))
  85. )