Skip to content

Commit

Permalink
added logging to mail and file
Browse files Browse the repository at this point in the history
  • Loading branch information
JayKayAce committed Jul 12, 2019
1 parent 563c18a commit 0c8dd0b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"request": "launch",
"module": "flask",
"env": {
"FLASK_APP": "dvdcollection.py"
"FLASK_APP": "dvdcollection.py",
"MAIL_SERVER":"localhost",
"MAIL_PORT":8025
},
"args": [
"run",
Expand Down
Binary file modified app.db
Binary file not shown.
42 changes: 41 additions & 1 deletion app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import os
import logging
from logging.handlers import SMTPHandler
from logging.handlers import RotatingFileHandler

from flask import Flask
from config import Config
from flask_sqlalchemy import SQLAlchemy
Expand All @@ -13,4 +18,39 @@

from app import routes
from app import models
from app import errors
from app import errors

if not app.debug:
if app.config["MAIL_SERVER"]:
auth = None
if app.config["MAIL_USERNAME"] or app.config["MAIL_PASSWORD"]:
auth = (app.config["MAIL_USERNAME"],app.config["MAIL_PASSWORD"])
secure = None
if app.config["MAIL_USE_TLS"]:
secure = ()
mail_handler = SMTPHandler(
mailhost=(app.config["MAIL_SERVER"], app.config["MAIL_PORT"]),
fromaddr="no-reply@" + app.config["MAIL_SERVER"],
toaddrs= app.config["ADMINS"],
subject="Microblog Failure",
credentials=auth,
secure=secure
)
mail_handler.setLevel(logging.ERROR)
app.logger.addHandler(mail_handler)

if not os.path.exists('logs'):
os.mkdir("logs")
file_handler = RotatingFileHandler(
"logs/microblog.log",
maxBytes=10240,
backupCount= 10
)
file_handler.setFormatter(logging.Formatter(
"%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]"
))
file_handler.setLevel(logging.INFO)
app.logger.addHandler(file_handler)

app.logger.setLevel(logging.INFO)
app.logger.info("Microblog started")

0 comments on commit 0c8dd0b

Please sign in to comment.