msgservice.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import logging
  2. from urllib3 import request,PoolManager
  3. from urllib3.util.timeout import Timeout
  4. from adm.constants import CTS as cts
  5. import os,sys
  6. import json
  7. import logging
  8. class HandlerUtil():
  9. def sendMsgHandler(self,strMsg,strCanal):
  10. data = {
  11. "blocks": [
  12. {
  13. "type": "section",
  14. "text": {
  15. "type": "mrkdwn",
  16. "text": strMsg
  17. }
  18. }
  19. ]
  20. }
  21. http = PoolManager()
  22. try:
  23. http.request('POST', strCanal,
  24. headers={'Content-Type': 'application/json'},
  25. body=json.dumps(data), timeout=Timeout(5))
  26. except BaseException as error:
  27. try:
  28. exc_type, exc_obj, exc_tb = sys.exc_info()
  29. fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
  30. strErro = "ERRO: %s | %s | %s | %s" % (error, exc_type, fname, exc_tb.tb_lineno)
  31. print(strErro)
  32. pass
  33. except:
  34. pass
  35. class MsgOperacaoHandler(logging.Handler):
  36. def emit(self, record):
  37. strCanal = cts.SLACK_HOOK_OPERACOES
  38. strAux = cts.GIT_PREF_USER
  39. if strAux!="":
  40. strAux = " (" + strAux.replace("-u ","") + ")"
  41. tipoMsg = cts.AMBIENTE_EC2 + strAux + " - Monitor"
  42. arrAssunto = self.format(record).split("|")
  43. self.formatter=None
  44. strMensagem = self.format(record)
  45. if len(arrAssunto)>1:
  46. strMensagem = arrAssunto[1] + "\n" + strMensagem
  47. icone="rotating_light"
  48. if arrAssunto[0].replace("\n","").replace(" ","")=="INFO":
  49. icone="clipboard"
  50. strMsg = ":" + icone + ": *" + tipoMsg + " " + arrAssunto[0] + "*\n" + strMensagem
  51. HandlerUtil().sendMsgHandler(strMsg,strCanal)
  52. class MsgServiceHandler(logging.Handler):
  53. def emit(self, record):
  54. strCanal = cts.SLACK_HOOK_LOG_BACKEND
  55. strAux = cts.GIT_PREF_USER
  56. if strAux!="":
  57. strAux = " (" + strAux.replace("-u ","") + ")"
  58. tipoMsg = cts.AMBIENTE_EC2 + strAux + " - LOG"
  59. arrAssunto = self.format(record).split("|")
  60. self.formatter=None
  61. strMensagem = self.format(record)
  62. if len(arrAssunto)>1:
  63. strMensagem = arrAssunto[1] + "\n" + strMensagem
  64. icone="rotating_light"
  65. if arrAssunto[0].replace("\n","").replace(" ","")=="INFO":
  66. icone="clipboard"
  67. #strMensagem = "Arquivo:%s\nLinha:%s\nMensagem:%s\nArgs:%s\n" % (record.filename, record.lineno,record.msg, record.args)
  68. strMsg = ":" + icone + ": *" + tipoMsg + " " + arrAssunto[0] + "*\n" + strMensagem
  69. HandlerUtil().sendMsgHandler(strMsg,strCanal)