Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

58 rindas
1.9 KiB

  1. # Copyright 2017 New Vector Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import logging
  15. import traceback
  16. from io import StringIO
  17. from types import TracebackType
  18. from typing import Optional, Tuple, Type
  19. class LogFormatter(logging.Formatter):
  20. """Log formatter which gives more detail for exceptions
  21. This is the same as the standard log formatter, except that when logging
  22. exceptions [typically via log.foo("msg", exc_info=1)], it prints the
  23. sequence that led up to the point at which the exception was caught.
  24. (Normally only stack frames between the point the exception was raised and
  25. where it was caught are logged).
  26. """
  27. def formatException(
  28. self,
  29. ei: Tuple[
  30. Optional[Type[BaseException]],
  31. Optional[BaseException],
  32. Optional[TracebackType],
  33. ],
  34. ) -> str:
  35. sio = StringIO()
  36. (typ, val, tb) = ei
  37. # log the stack above the exception capture point if possible, but
  38. # check that we actually have an f_back attribute to work around
  39. # https://twistedmatrix.com/trac/ticket/9305
  40. if tb and hasattr(tb.tb_frame, "f_back"):
  41. sio.write("Capture point (most recent call last):\n")
  42. traceback.print_stack(tb.tb_frame.f_back, None, sio)
  43. traceback.print_exception(typ, val, tb, None, sio)
  44. s = sio.getvalue()
  45. sio.close()
  46. if s[-1:] == "\n":
  47. s = s[:-1]
  48. return s