From coreybrett at gmail.com Thu Jul 1 08:54:32 2021 From: coreybrett at gmail.com (Corey Boyle) Date: Thu, 1 Jul 2021 08:54:32 -0400 Subject: [Flask] insert current_user value into logger generated emails? Message-ID: I have the following code in my app factory... mh = SMTPHandler( mailhost=app.config["MAIL_SERVER"], fromaddr=app.config["MAIL_DEFAULT_SENDER"], toaddrs=app.config["ERRORS_EMAIL"], subject="Error", ) mh.setLevel(logging.ERROR) app.logger.addHandler(mh) Is there any way to get the value of "current_user" inserted into the email somewhere? -------------- next part -------------- An HTML attachment was scrubbed... URL: From canhosgor at gmail.com Fri Jul 2 15:05:08 2021 From: canhosgor at gmail.com (Can Hosgor) Date: Fri, 2 Jul 2021 22:05:08 +0300 Subject: [Flask] insert current_user value into logger generated emails? In-Reply-To: References: Message-ID: You can try writing a custom logging Filter that gets current user from the request context and puts it into the log record. Then it will be available for the log formatter. Check out the example here https://stackoverflow.com/a/17558764 On Thu, 1 Jul 2021 at 15:55 Corey Boyle wrote: > I have the following code in my app factory... > > mh = SMTPHandler( > mailhost=app.config["MAIL_SERVER"], > fromaddr=app.config["MAIL_DEFAULT_SENDER"], > toaddrs=app.config["ERRORS_EMAIL"], > subject="Error", > ) > mh.setLevel(logging.ERROR) > app.logger.addHandler(mh) > > Is there any way to get the value of "current_user" inserted into the > email somewhere? > _______________________________________________ > Flask mailing list > Flask at python.org > https://mail.python.org/mailman/listinfo/flask > -------------- next part -------------- An HTML attachment was scrubbed... URL: From felixramirezreyes at gmail.com Sun Jul 4 11:39:30 2021 From: felixramirezreyes at gmail.com (Felix Ramirez) Date: Sun, 4 Jul 2021 11:39:30 -0400 Subject: [Flask] insert current_user value into logger generated emails? In-Reply-To: References: Message-ID: Hi everyone, I'm interested in the Corey Boyle question too because I'm struggling with the same situations to add the current_user to logger but without success and the link that Can Hosgor doesn't answer her question but it's very interesting. I would appreciate any help with this. Thanks in advance to all. El vie, 2 de jul. de 2021 a la(s) 15:05, Can Hosgor (canhosgor at gmail.com) escribi?: > You can try writing a custom logging Filter that gets current user from > the request context and puts it into the log record. Then it will be > available for the log formatter. Check out the example here > https://stackoverflow.com/a/17558764 > > > On Thu, 1 Jul 2021 at 15:55 Corey Boyle wrote: > >> I have the following code in my app factory... >> >> mh = SMTPHandler( >> mailhost=app.config["MAIL_SERVER"], >> fromaddr=app.config["MAIL_DEFAULT_SENDER"], >> toaddrs=app.config["ERRORS_EMAIL"], >> subject="Error", >> ) >> mh.setLevel(logging.ERROR) >> app.logger.addHandler(mh) >> >> Is there any way to get the value of "current_user" inserted into the >> email somewhere? >> _______________________________________________ >> Flask mailing list >> Flask at python.org >> https://mail.python.org/mailman/listinfo/flask >> > _______________________________________________ > Flask mailing list > Flask at python.org > https://mail.python.org/mailman/listinfo/flask > -- *Felix Ramirez R.* -------------- next part -------------- An HTML attachment was scrubbed... URL: From coreybrett at gmail.com Sun Jul 4 11:51:35 2021 From: coreybrett at gmail.com (Corey Boyle) Date: Sun, 4 Jul 2021 11:51:35 -0400 Subject: [Flask] insert current_user value into logger generated emails? In-Reply-To: References: Message-ID: https://stackoverflow.com/questions/68246462/how-could-the-value-of-current-user-be-inserted-into-error-logs-emails-when-usin On Sun, Jul 4, 2021 at 11:39 AM Felix Ramirez wrote: > > Hi everyone, > I'm interested in the Corey Boyle question too because I'm struggling with the same situations to add the current_user to logger but without success and the link that Can Hosgor doesn't answer her question but it's very interesting. > I would appreciate any help with this. > > Thanks in advance to all. > > > El vie, 2 de jul. de 2021 a la(s) 15:05, Can Hosgor (canhosgor at gmail.com) escribi?: >> >> You can try writing a custom logging Filter that gets current user from the request context and puts it into the log record. Then it will be available for the log formatter. Check out the example here https://stackoverflow.com/a/17558764 >> >> >> On Thu, 1 Jul 2021 at 15:55 Corey Boyle wrote: >>> >>> I have the following code in my app factory... >>> >>> mh = SMTPHandler( >>> mailhost=app.config["MAIL_SERVER"], >>> fromaddr=app.config["MAIL_DEFAULT_SENDER"], >>> toaddrs=app.config["ERRORS_EMAIL"], >>> subject="Error", >>> ) >>> mh.setLevel(logging.ERROR) >>> app.logger.addHandler(mh) >>> >>> Is there any way to get the value of "current_user" inserted into the email somewhere? >>> _______________________________________________ >>> Flask mailing list >>> Flask at python.org >>> https://mail.python.org/mailman/listinfo/flask >> >> _______________________________________________ >> Flask mailing list >> Flask at python.org >> https://mail.python.org/mailman/listinfo/flask > > > > -- > Felix Ramirez R. From felixramirezreyes at gmail.com Sun Jul 4 12:49:50 2021 From: felixramirezreyes at gmail.com (Felix Ramirez) Date: Sun, 4 Jul 2021 12:49:50 -0400 Subject: [Flask] insert current_user value into logger generated emails? In-Reply-To: References: Message-ID: Thank Corey for posting the question in Stackoverflow El dom., 4 de julio de 2021 11:52 a. m., Corey Boyle escribi?: > > https://stackoverflow.com/questions/68246462/how-could-the-value-of-current-user-be-inserted-into-error-logs-emails-when-usin > > > On Sun, Jul 4, 2021 at 11:39 AM Felix Ramirez > wrote: > > > > Hi everyone, > > I'm interested in the Corey Boyle question too because I'm struggling > with the same situations to add the current_user to logger but without > success and the link that Can Hosgor doesn't answer her question but it's > very interesting. > > I would appreciate any help with this. > > > > Thanks in advance to all. > > > > > > El vie, 2 de jul. de 2021 a la(s) 15:05, Can Hosgor (canhosgor at gmail.com) > escribi?: > >> > >> You can try writing a custom logging Filter that gets current user from > the request context and puts it into the log record. Then it will be > available for the log formatter. Check out the example here > https://stackoverflow.com/a/17558764 > >> > >> > >> On Thu, 1 Jul 2021 at 15:55 Corey Boyle wrote: > >>> > >>> I have the following code in my app factory... > >>> > >>> mh = SMTPHandler( > >>> mailhost=app.config["MAIL_SERVER"], > >>> fromaddr=app.config["MAIL_DEFAULT_SENDER"], > >>> toaddrs=app.config["ERRORS_EMAIL"], > >>> subject="Error", > >>> ) > >>> mh.setLevel(logging.ERROR) > >>> app.logger.addHandler(mh) > >>> > >>> Is there any way to get the value of "current_user" inserted into the > email somewhere? > >>> _______________________________________________ > >>> Flask mailing list > >>> Flask at python.org > >>> https://mail.python.org/mailman/listinfo/flask > >> > >> _______________________________________________ > >> Flask mailing list > >> Flask at python.org > >> https://mail.python.org/mailman/listinfo/flask > > > > > > > > -- > > Felix Ramirez R. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From coreybrett at gmail.com Tue Jul 13 10:36:35 2021 From: coreybrett at gmail.com (Corey Boyle) Date: Tue, 13 Jul 2021 10:36:35 -0400 Subject: [Flask] insert current_user value into logger generated emails? In-Reply-To: References: Message-ID: Inserting the following into my app factory did the trick for me. if app.config["ENV"] == "development": pass else: mh = SMTPHandler( mailhost=app.config["MAIL_SERVER"], fromaddr=app.config["MAIL_DEFAULT_SENDER"], toaddrs=app.config["ERRORS_EMAIL"], subject="Integrity Error", ) mh.setLevel(logging.ERROR) mh.setFormatter( logging.Formatter( "%(user)s \n[%(asctime)s] \n%(levelname)s in %(module)s: %(message)s" ) ) app.logger.addHandler(mh) app.logger = LoggerAdapter(app.logger, {"user": current_user}) -------------- next part -------------- An HTML attachment was scrubbed... URL: