Logs with context

Logs with context

Pyday 2017 lighting talk.

66dc269635b1dbaaf2751ca5a5728525?s=128

Eloy Coto

June 29, 2017
Tweet

Transcript

  1. Logs with context

  2. @eloycoto 2008 2011 2013

  3. logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) log = logging.getLogger("pyday") @app.route("/auth/login", methods=['POST', 'GET']) def auth_login():

    if not login(request.form.get('username'), request.form.get('pwd')): log.warn("User can't log in") return Response("OK", 403) if is_user_disabled(request.form.get('username')): log.warn("User is disabled") return Response("OK", 403) log.info("User log in correctly") return Response("OK", 200) Typical login application
  4. INFO:pyday:User log in correctly INFO:pyday:User log in correctly INFO:pyday:User log

    in correctly WARNING:pyday:User can't log in WARNING:pyday:User can't log in INFO:pyday:User log in correctly INFO:pyday:User log in correctly WARNING:pyday:User can't log in WARNING:pyday:User is disabled WARNING:pyday:User can't log in Log output What? Who? Where? When?
  5. INFO:pyday:User='aindustriosa' is trying to log in. INFO:pyday:User='aindustriosa' login correctly @app.route("/auth/login",

    methods=['POST', 'GET']) def auth_login(): user = request.form.get('username') log.info("User='{0}' is trying to log in.".format(user)) if not login(user, request.form.get('pwd')): log.msg("User='{0}' can't log in") return Response("OK", 403) if is_user_disabled(request.form.get('username')): log.msg("User='{0}' is disabled".format(user)) return Response("OK", 403) log.info("User='{0}' login correctly".format(user)) return Response("OK", 200)
  6. Log output user = “Aindustriosa” log.info("User {0} is trying to

    log in.".format(user)) INFO:pyday:User Aindustriosa is trying to log in. user = “ Aindustriosa” log.info("User {0} is trying to log in.".format(user)) INFO:pyday:User Aindustriosa is trying to log in. user = “ Aindustriosa” log.info("User ‘{0}’ is trying to log in.".format(user)) INFO:pyday:User ‘ Aindustriosa’ is trying to log in.
  7. from structlog import get_logger slog = get_logger() @app.route("/auth/login", methods=['POST', 'GET'])

    @RID def auth_login(): user = request.form.get('username') log = slog.bind(user=user, rid=session.get('rid')) if not login(user, request.form.get('pwd')): log.warn("User can't log in") return Response("OK", 403) if is_user_disabled(request.form.get('username')): log.warn("User is disabled".format(user)) return Response("OK", 403) log.info("User login correctly".format(user)) return Response("OK", 200) Structlog
  8. 2017-06-10 11:51.27 User login correctly \n rid=a79f21dc-0f91-4667-8ec7-56e319047be7 \n user=u'aindustriosa' view=auth.login

    \n When What Who Where
  9. None
  10. Email: eloy.coto@acalustra.com Blog: http://acalustra.com Twitter: @eloycoto Graciñas!