Logging of user actions

195 views
Skip to first unread message

Joseph.Piron

unread,
May 17, 2011, 5:59:10 PM5/17/11
to web2py-users
Hi all,

I was wondering what would be the best way to log actions with the
username of the logger user.
I found several leads but none very conclusive.

For example, define a filter in my model:

import logging
class ContextFilter(logging.Filter):
def filter(self, record):
record.user = auth.user.username if hasattr(auth.user,
'username') else "<None>"
return True

logger = logging.getLogger("web2py.app." +
request.application.lower())
if len(logger.filters) == 0:
logger.addFilter(ContextFilter())

but two issues: auth.user = None in runtime when the context is the
filter method (tried with eclipse debugger)
and moreover, would it work, I would have to define new handlers to
get a new formatter using this new attribute.. not very handy.

What could be the best option here ?

Thanks in advance for your advices!

Joseph.Piron

unread,
May 18, 2011, 6:22:34 PM5/18/11
to web2py-users
Has noone ever had this king of need ??

pbreit

unread,
May 18, 2011, 7:23:52 PM5/18/11
to web...@googlegroups.com
Not sure exactly what you need but, yes, I think it's kind of rare.

Maybe this will work for you: by default, Web2py creates an auth_event table and automatically records a bunch of actions like register, login, etc.

You can add your own actions with this one-liner:
auth.log_event(description='this happened', origin='auth')

Both description and origin can be anything I believe.

Joseph.Piron

unread,
May 19, 2011, 5:17:11 PM5/19/11
to web2py-users
Oh, the auth.log_event I didn't know.. maybe interesting, but i don't
want to log in the db, I need to use my logger config.

But I can't possibly believe it's rare to log what users do..
Imagine, I have a db of specifications, I want to know who modified
which field at what time.. What should be the best way to track this
except log it ?

Anyway, thanks for the answer :)

pbreit

unread,
May 19, 2011, 5:23:04 PM5/19/11
to web...@googlegroups.com
I got this from here awhile back:

def _init_log():
    import os,logging,logging.handlers,time 
    logger = logging.getLogger(request.application) 
    logger.setLevel(logging.INFO) 
    handler = logging.handlers.RotatingFileHandler(os.path.join(
        request.folder,'logs','applog.log'),'a',1024*1024,1) 
    handler.setLevel(logging.INFO) #or DEBUG
    handler.setFormatter(logging.Formatter( 
        '%(asctime)s %(levelname)s %(filename)s %(lineno)d %(funcName)s(): %(message)s')) 
    logger.addHandler(handler) 
    return logger
    
app_logging = cache.ram('app_wide_log',lambda:_init_log(),time_expire=None)

Then you can do this from anywhere:
app_logging.info(log_this_data)

howesc

unread,
May 20, 2011, 6:30:46 PM5/20/11
to web...@googlegroups.com
in that case i would put it right in the DB, using CRUD's archive tools.  then you know who made what changes to what rows and when, and you can go back an restore previous versions.  http://web2py.com/book/default/chapter/07?search=archive

much easier to parse than text logs (in my opinion)

cfh

Joseph.Piron

unread,
May 20, 2011, 7:03:46 PM5/20/11
to web2py-users
Ah nice trick also, didn't think about the cache, but where do you put
the code to defie the _init_log ?

pbreit

unread,
May 21, 2011, 1:34:48 AM5/21/11
to web...@googlegroups.com
I have it in a model so it's available everywhere.
Reply all
Reply to author
Forward
0 new messages