Skip to content

Commit

Permalink
Setup logging. Closes #35
Browse files Browse the repository at this point in the history
  • Loading branch information
binrush committed Dec 20, 2015
1 parent 1a0cc5a commit 39a67c3
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 31 deletions.
59 changes: 32 additions & 27 deletions thousands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,39 @@
app.config.from_pyfile(os.path.join(os.environ['OPENSHIFT_DATA_DIR'],
'thousands.conf'))

loggers = [ app.logger, logging.getLogger('yoyo') ]

if 'LOGDIR' in app.config:
main_handler = logging.handlers.RotatingFileHandler(
os.path.join(app.config['LOGDIR'], 'thousands.log'),
maxBytes=1024*1024*1024,
backupCount=10,
encoding='utf-8')
else:
main_handler = logging.StreamHandler()

main_handler.setLevel(logging.DEBUG)
handlers = [main_handler]

if 'ADMIN_MAIL' in app.config:
mail_handler = SMTPHandler(app.config['SMTP_HOST'],
app.config['SMTP_FROMADDR'],
app.config['ADMIN_MAIL'],
'Thousands app failed',
(app.config['SMTP_USER'], app.config['SMTP_PASSWORD']),
())
mail_handler.setLevel(logging.ERROR)
handlers.append(mail_handler)

for logger in loggers:
for handler in handlers:
logger.addHandler(handler)
if not app.debug:
loggers = [ app.logger, logging.getLogger('yoyo') ]

if 'LOGDIR' in app.config:
main_handler = logging.handlers.RotatingFileHandler(
os.path.join(app.config['LOGDIR'], 'thousands.log'),
maxBytes=1024*1024*1024,
backupCount=10,
encoding='utf-8')
else:
main_handler = logging.StreamHandler()

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
main_handler.setFormatter(formatter)

map(lambda x: x.addHandler(main_handler), loggers)
app.logger.setLevel(logging.INFO)

if 'ADMIN_MAIL' in app.config:
mail_handler = SMTPHandler(
app.config['SMTP_HOST'],
app.config['SMTP_FROMADDR'],
app.config['ADMIN_MAIL'],
'Thousands app failed',
(app.config['SMTP_USER'], app.config['SMTP_PASSWORD']),
())
mail_handler.setLevel(logging.ERROR)
mail_handler.setFormatter(formatter)
map(lambda x: x.addHandler(mail_handler), loggers)

else:
logging.getLogger('yoyo').addHandler(logging.StreamHandler())


psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
pool = psycopg2.pool.SimpleConnectionPool(1, 10, app.config['PG_DSN'])
Expand Down
4 changes: 4 additions & 0 deletions thousands/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ def oauth_login(req, flow, get_user):
if user is not None:
login_user(user)
if created:
app.logger.info("New user registration uid=%s, src=%s, name=%s",
user.get_id(),
user.src,
user.name)
flash(u'Регистрация завершена')
return redirect(url_for('user', user_id=user.get_id()))
else:
Expand Down
4 changes: 4 additions & 0 deletions thousands/static/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,7 @@ a.anchor {
top: -60px;
visibility: hidden;
}

.flash-message {
margin-top: 20px;
}
2 changes: 1 addition & 1 deletion thousands/templates/summit.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{% block content %}
{% with messages = get_flashed_messages() %}
{% if messages %}
<div class="alert alert-success" role="alert">
<div class="alert alert-success flash-message" role="alert">
{% for message in messages %}
{{ message }}
{% endfor %}
Expand Down
4 changes: 2 additions & 2 deletions thousands/templates/user.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
{% endif %}
{% endblock %}
{% block content %}
<h1>{{ user.name }}</h1>
{% with messages = get_flashed_messages() %}
{% if messages %}
<div class="alert alert-success" role="alert">
<div class="alert alert-success flash-message" role="alert">
{% for message in messages %}
{{ message }}
{% endfor %}
</div>
{% endif %}
{% endwith %}
<h1>{{ user.name }}</h1>
<div class="row">
<div class="col-md-3 text-right">
{% if user.image_id %}
Expand Down
2 changes: 2 additions & 0 deletions thousands/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def climb_edit(summit_id, redirect_url):
f.summit_id.data,
f.date.data,
f.comment.data)
app.logger.info("Climb edited uid=%s, sid=%s", current_user.id, summit_id)
flash(u'Ваше восхождение отредактировано')
return redirect(redirect_url)
else:
Expand All @@ -184,6 +185,7 @@ def profile_climb_delete(summit_id):

def climb_delete(summit_id, redirect_url):
g.climbs_dao.delete(current_user.id, summit_id)
app.logger.info("Climb deleted uid=%s, sid=%s", current_user.id, summit_id)
flash(u'Ваше восхождение удалено')
return redirect(redirect_url)

Expand Down
2 changes: 1 addition & 1 deletion wsgi.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from thousands import app as application

if __name__ == '__main__':
application.run(host='0.0.0.0', debug=True)
application.run(host='0.0.0.0')

0 comments on commit 39a67c3

Please sign in to comment.