Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Logs with context

Logs with context

Pyday 2017 lighting talk.

Eloy Coto

June 29, 2017
Tweet

More Decks by Eloy Coto

Other Decks in Programming

Transcript

  1. 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
  2. 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?
  3. 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)
  4. 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.
  5. 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