_levels.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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: NamedConstant) -> None:
  13. """
  14. @param level: A log level from L{LogLevel}.
  15. """
  16. super().__init__(str(level))
  17. self.level = level
  18. class LogLevel(Names):
  19. """
  20. Constants describing log levels.
  21. @cvar debug: Debugging events: Information of use to a developer of the
  22. software, not generally of interest to someone running the software
  23. unless they are attempting to diagnose a software issue.
  24. @cvar info: Informational events: Routine information about the status of
  25. an application, such as incoming connections, startup of a subsystem,
  26. etc.
  27. @cvar warn: Warning events: Events that may require greater attention than
  28. informational events but are not a systemic failure condition, such as
  29. authorization failures, bad data from a network client, etc. Such
  30. events are of potential interest to system administrators, and should
  31. ideally be phrased in such a way, or documented, so as to indicate an
  32. action that an administrator might take to mitigate the warning.
  33. @cvar error: Error conditions: Events indicating a systemic failure, such
  34. as programming errors in the form of unhandled exceptions, loss of
  35. connectivity to an external system without which no useful work can
  36. proceed, such as a database or API endpoint, or resource exhaustion.
  37. Similarly to warnings, errors that are related to operational
  38. parameters may be actionable to system administrators and should
  39. provide references to resources which an administrator might use to
  40. resolve them.
  41. @cvar critical: Critical failures: Errors indicating systemic failure (ie.
  42. service outage), data corruption, imminent data loss, etc. which must
  43. be handled immediately. This includes errors unanticipated by the
  44. software, such as unhandled exceptions, wherein the cause and
  45. consequences are unknown.
  46. """
  47. debug = NamedConstant()
  48. info = NamedConstant()
  49. warn = NamedConstant()
  50. error = NamedConstant()
  51. critical = NamedConstant()
  52. @classmethod
  53. def levelWithName(cls, name: str) -> NamedConstant:
  54. """
  55. Get the log level with the given name.
  56. @param name: The name of a log level.
  57. @return: The L{LogLevel} with the specified C{name}.
  58. @raise InvalidLogLevelError: if the C{name} does not name a valid log
  59. level.
  60. """
  61. try:
  62. return cls.lookupByName(name)
  63. except ValueError:
  64. raise InvalidLogLevelError(name)