[Flask] Flask Digest, Vol 8, Issue 2

Chukwudi Nwachukwu hsysinfo at gmail.com
Wed Feb 3 10:15:54 EST 2016


@Kelvin

I had the issue once and I solved it. See what you can glean from here
first: https://github.com/yandex-sysmon/elog/blob/master/elog/handlers.py

I think you should be able to do it.


iChux™
http://bit.ly/iscrape
*Behind every no entry sign, there is a door.*

On 2 February 2016 at 17:35, <flask-request at python.org> wrote:

> Send Flask mailing list submissions to
>         flask at python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>         https://mail.python.org/mailman/listinfo/flask
> or, via email, send a message with subject or body 'help' to
>         flask-request at python.org
>
> You can reach the person managing the list at
>         flask-owner at python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Flask digest..."
>
>
> Today's Topics:
>
>    1. Re: User specific log files (Kevin Stone)
>    2. Re: User specific log files (Kevin Stone)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Tue, 2 Feb 2016 15:21:36 +0000
> From: Kevin Stone <Kevin.Stone at jax.org>
> To: Kelvin M <kelvin.malyar at gmail.com>, Robert Grant
>         <robertlagrant at gmail.com>
> Cc: "flask at python.org" <flask at python.org>
> Subject: Re: [Flask] User specific log files
> Message-ID: <1454426498458.63267 at jax.org>
> Content-Type: text/plain; charset="iso-8859-1"
>
> We have a simple internal application for editing our database. There are
> about 20-30 users. We primarily want to see the user actions and SQL
> generated by the app, sometimes in realtime. ?
>
> ?
>
> When a user logs in and creates a session, I would like to be able to
> append a filehandler for that user to the main app logger. What I can't
> grasp in my head is that the app logger is global, but I only want this
> filehandler to act on that user's session. That's why when I tried it it
> logged all user actions to every log file.
>
>
> ________________________________
> From: Kelvin M <kelvin.malyar at gmail.com>
> Sent: Tuesday, February 2, 2016 10:12 AM
> To: Robert Grant
> Cc: Kevin Stone; flask at python.org
> Subject: Re: [Flask] User specific log files
>
> Hi Kevin,
>
> What are you trying to accomplish with multiple log files?  In the past,
> the approach that I/we've taken is to just use a log aggregator.  Either,
> Splunk or ELK (ElasticSearch, LogStash, Kibana).
>
> With both, you index all log messages and can then create reports or views
> for users independently.
>
> I know that ELK is really easy to set up and free.
>
> Would something like that work for you?
>
> Regards,
> Kelvin
>
> On Mon, Feb 1, 2016 at 5:26 PM, Robert Grant <robertlagrant at gmail.com
> <mailto:robertlagrant at gmail.com>> wrote:
> Hi Kevin
>
> Just a couple of quick questions:
>
> 1) Do you have enough users to just log to a database and record a
> username with each log line? Just a suggestion to consider.
> 2) Can you post a minimal, verifiable and complete example of your first
> attempt to give people something solid to critique?
>
> Thanks
>
>
> Rob
>
> On Mon, Feb 1, 2016 at 8:55 PM, Kevin Stone <Kevin.Stone at jax.org<mailto:
> Kevin.Stone at jax.org>> wrote:
>
> ?Hi,
>
>
> I am not the most familiar with the python logging framework, and I think
> I need some help on developing an approach for creating user specific log
> files in Flask. It would be even better if someone has already done this
> before and could share their pattern.
>
>
>
> The problem I want to solve is this:
>
> 1) user1 and user2 both log in to the app
>
> 2) user1's activity is logged in "user1.log". user2's activity is logged
> in "user2.log".
>
> 3) No other user's activity should appear in that user's log file.
>
>
>
> Ideally, I would like to tap into existing loggers that I have (e.g. the
> app logger and the SQLAlchemy logger), but I don't know if that is
> possible. My first naive attempt simply registers additional handlers at
> login time to the app.logger. This created the appropriate log files, but
> they had all activity from all users writing to them.
>
>
>
> Any help or advice would be appreciated.
>
> Thanks,
>
> -Kevin
>
> The information in this email, including attachments, may be confidential
> and is intended solely for the addressee(s). If you believe you received
> this email by mistake, please notify the sender by return email as soon as
> possible.
>
> _______________________________________________
> Flask mailing list
> Flask at python.org<mailto:Flask at python.org>
> https://mail.python.org/mailman/listinfo/flask
>
>
>
> _______________________________________________
> Flask mailing list
> Flask at python.org<mailto:Flask at python.org>
> https://mail.python.org/mailman/listinfo/flask
>
>
>
> The information in this email, including attachments, may be confidential
> and is intended solely for the addressee(s). If you believe you received
> this email by mistake, please notify the sender by return email as soon as
> possible.
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <
> http://mail.python.org/pipermail/flask/attachments/20160202/415b3aab/attachment-0001.html
> >
>
> ------------------------------
>
> Message: 2
> Date: Tue, 2 Feb 2016 16:35:27 +0000
> From: Kevin Stone <Kevin.Stone at jax.org>
> To: Kelvin M <kelvin.malyar at gmail.com>, Robert Grant
>         <robertlagrant at gmail.com>
> Cc: "flask at python.org" <flask at python.org>
> Subject: Re: [Flask] User specific log files
> Message-ID: <1454430929144.83414 at jax.org>
> Content-Type: text/plain; charset="utf-8"
>
> I have done some research and experimenting. There is a whole lot you can
> do with the python logging framework that I am unfamiliar with. I think I
> found something that will work for us, although maybe it's not the best
> solution.
>
>
> I was able to create a filehandler on user login that I attach a filter
> to. This filter tells the handler to only work when that user is logged
> into the session. Here is the filter code.
>
>
>
> class UserLoggingFilter(logging.Filter):
>
>     """
>
>     Create a filter to only log the current user
>
>         to its handler
>
>     """
>
>
>
>     def __init__(self, user):
>
>         self.user = user
>
>
>
>     def filter(self, record):
>
>         if 'user' in session and session['user'] == self.user:
>
>             return True
>
>         return False
>
>
>
> And here is more or less what the handler code is like.
>
>
>
> file_handler = FileHandler(user_log_file)
>
> file_handler.addFilter(UserLoggingFilter(user))
>
> app.logger.addHandler(file_handler)
>
> ?
>
>
> This seems to work just great. I appreciate the help, and welcome any
> alternative suggestions, but at least I have one way that works.
>
>
> -Kevin
>
>
> ________________________________
> From: Kevin Stone
> Sent: Tuesday, February 2, 2016 10:21 AM
> To: Kelvin M; Robert Grant
> Cc: flask at python.org
> Subject: Re: [Flask] User specific log files
>
>
> We have a simple internal application for editing our database. There are
> about 20-30 users. We primarily want to see the user actions and SQL
> generated by the app, sometimes in realtime. ?
>
> ?
>
> When a user logs in and creates a session, I would like to be able to
> append a filehandler for that user to the main app logger. What I can't
> grasp in my head is that the app logger is global, but I only want this
> filehandler to act on that user's session. That's why when I tried it it
> logged all user actions to every log file.
>
>
> ________________________________
> From: Kelvin M <kelvin.malyar at gmail.com>
> Sent: Tuesday, February 2, 2016 10:12 AM
> To: Robert Grant
> Cc: Kevin Stone; flask at python.org
> Subject: Re: [Flask] User specific log files
>
> Hi Kevin,
>
> What are you trying to accomplish with multiple log files?  In the past,
> the approach that I/we've taken is to just use a log aggregator.  Either,
> Splunk or ELK (ElasticSearch, LogStash, Kibana).
>
> With both, you index all log messages and can then create reports or views
> for users independently.
>
> I know that ELK is really easy to set up and free.
>
> Would something like that work for you?
>
> Regards,
> Kelvin
>
> On Mon, Feb 1, 2016 at 5:26 PM, Robert Grant <robertlagrant at gmail.com
> <mailto:robertlagrant at gmail.com>> wrote:
> Hi Kevin
>
> Just a couple of quick questions:
>
> 1) Do you have enough users to just log to a database and record a
> username with each log line? Just a suggestion to consider.
> 2) Can you post a minimal, verifiable and complete example of your first
> attempt to give people something solid to critique?
>
> Thanks
>
>
> Rob
>
> On Mon, Feb 1, 2016 at 8:55 PM, Kevin Stone <Kevin.Stone at jax.org<mailto:
> Kevin.Stone at jax.org>> wrote:
>
> ?Hi,
>
>
> I am not the most familiar with the python logging framework, and I think
> I need some help on developing an approach for creating user specific log
> files in Flask. It would be even better if someone has already done this
> before and could share their pattern.
>
>
>
> The problem I want to solve is this:
>
> 1) user1 and user2 both log in to the app
>
> 2) user1's activity is logged in "user1.log". user2's activity is logged
> in "user2.log".
>
> 3) No other user's activity should appear in that user's log file.
>
>
>
> Ideally, I would like to tap into existing loggers that I have (e.g. the
> app logger and the SQLAlchemy logger), but I don't know if that is
> possible. My first naive attempt simply registers additional handlers at
> login time to the app.logger. This created the appropriate log files, but
> they had all activity from all users writing to them.
>
>
>
> Any help or advice would be appreciated.
>
> Thanks,
>
> -Kevin
>
> The information in this email, including attachments, may be confidential
> and is intended solely for the addressee(s). If you believe you received
> this email by mistake, please notify the sender by return email as soon as
> possible.
>
> _______________________________________________
> Flask mailing list
> Flask at python.org<mailto:Flask at python.org>
> https://mail.python.org/mailman/listinfo/flask
>
>
>
> _______________________________________________
> Flask mailing list
> Flask at python.org<mailto:Flask at python.org>
> https://mail.python.org/mailman/listinfo/flask
>
>
>
> The information in this email, including attachments, may be confidential
> and is intended solely for the addressee(s). If you believe you received
> this email by mistake, please notify the sender by return email as soon as
> possible.
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <
> http://mail.python.org/pipermail/flask/attachments/20160202/8bc80018/attachment.html
> >
>
> ------------------------------
>
> Subject: Digest Footer
>
> _______________________________________________
> Flask mailing list
> Flask at python.org
> https://mail.python.org/mailman/listinfo/flask
>
>
> ------------------------------
>
> End of Flask Digest, Vol 8, Issue 2
> ***********************************
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20160203/96943089/attachment-0001.html>


More information about the Flask mailing list