ソースを参照

Ajoute smtp logging (mail)

olivier.massot 8 年 前
コミット
46596d7cce
4 ファイル変更61 行追加22 行削除
  1. 51 0
      core/BufferingSMTPHandler.py
  2. 3 3
      core/logconf.py
  3. 6 18
      core/logging.yaml
  4. 1 1
      gf2analytique.py

+ 51 - 0
core/BufferingSMTPHandler.py

@@ -0,0 +1,51 @@
+#!/usr/bin/env python
+#
+# Copyright 2001-2002 by Vinay Sajip. All Rights Reserved.
+#
+# Permission to use, copy, modify, and distribute this software and its
+# documentation for any purpose and without fee is hereby granted,
+# provided that the above copyright notice appear in all copies and that
+# both that copyright notice and this permission notice appear in
+# supporting documentation, and that the name of Vinay Sajip
+# not be used in advertising or publicity pertaining to distribution
+# of the software without specific, written prior permission.
+# VINAY SAJIP DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
+# ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
+# VINAY SAJIP BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
+# ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
+# IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
+# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+#
+# This file is part of the Python logging distribution. See
+# http://www.red-dove.com/python_logging.html
+#
+"""Test harness for the logging module. Tests BufferingSMTPHandler, an alternative implementation
+of SMTPHandler.
+Copyright (C) 2001-2002 Vinay Sajip. All Rights Reserved.
+"""
+import logging.handlers
+import smtplib
+
+class BufferingSMTPHandler(logging.handlers.BufferingHandler):
+    def __init__(self, mailhost, fromaddr, toaddrs, subject, capacity):
+        logging.handlers.BufferingHandler.__init__(self, capacity)
+        self.mailhost = mailhost
+        self.mailport = None
+        self.fromaddr = fromaddr
+        self.toaddrs = toaddrs
+        self.subject = subject
+        self.setFormatter(logging.Formatter("%(asctime)s %(levelname)-5s %(message)s"))
+
+    def flush(self):
+        if len(self.buffer) > 0:
+            port = self.mailport if self.mailport else smtplib.SMTP_PORT
+
+            smtp = smtplib.SMTP(self.mailhost, port)
+            msg = "From: {}\nTo: {}\nSubject: {}\n\n".format(self.fromaddr, ",".join(self.toaddrs), self.subject)
+            for record in self.buffer:
+                msg += self.format(record)
+                msg += "\n"
+
+            smtp.sendmail(self.fromaddr, self.toaddrs, msg)
+            smtp.quit()
+            self.buffer = []

+ 3 - 3
core/logconf.py

@@ -18,9 +18,9 @@ def start(name="main", level=0, filename=""):
     if level:
         conf["loggers"][name]["level"] = level
 
-    if not filename:
-        filename = r'log\{}_{:%Y%m%d_%H%M}.log'.format(name, datetime.now())
-    conf["handlers"]["file"]["filename"] = filename
+#     if not filename:
+#         filename = r'log\{}_{:%Y%m%d_%H%M}.log'.format(name, datetime.now())
+#     conf["handlers"]["file"]["filename"] = filename
 
     logging.config.dictConfig(conf)
 

+ 6 - 18
core/logging.yaml

@@ -16,7 +16,6 @@ handlers:
         level: INFO
         formatter: message_only
         stream: ext://sys.stdout
-
     file:
         class: logging.handlers.RotatingFileHandler
         level: DEBUG
@@ -25,39 +24,28 @@ handlers:
         maxBytes: 100000
         backupCount: 1
         encoding: utf8
-    mailbuffer:
-        class: logging.handlers.MemoryHandler
-        level: INFO
-        formatter: complete
-        capacity: 100000
-        flushLevel: CRITICAL
-        target: mail
     mail:
-        class: logging.handlers.SMTPHandler
+        class: core.BufferingSMTPHandler.BufferingSMTPHandler
         level: INFO
         formatter: complete
-        mailhost: https://webmail.cg67.fr/owa/
+        mailhost: smtp.bas-rhin.fr
         fromaddr: olivier.massot@bas-rhin.fr
         toaddrs: [olivier.massot@bas-rhin.fr]
         subject: log
+        capacity: 1000000
         
 loggers:
     gf2analytique:
         level: INFO
-        handlers: [console, file]
+        handlers: [console, file, mail]
         propagate: no
 
     gf2factures:
         level: INFO
-        handlers: [console, file]
+        handlers: [console, file, mail]
         propagate: no
     
-    test:
-        level: INFO
-        handlers: [console, mailbuffer]
-        propagate: no   
-        
 root:
     level: INFO
-    handlers: [console, file]
+    handlers: [console]
     propagate: yes

+ 1 - 1
gf2analytique.py

@@ -357,4 +357,4 @@ if __name__ == "__main__":
         res = Facture.process([Facture.from_webservice(wsdata) for wsdata in ws])
         logger.info("> {} lignes traitées / {} importées / {} erreurs".format(res[0], res[1], res[2]))
 
-
+    logging.shutdown()