Configure Z2.log to show user name of logged in user

In the last couple of months there were many major changes to my application on the way to Python 3, like the update to Zope 4, the replacement of the no longer working user login with Products.PluggableAuthService and many more, and at one point my application stopped logging the user name in the Z2.log.

I always get the Anonymous user, instead of the logged in user, e.g.

xxx.xxx.xxx.xxx - Anonymous [13/Jan/2020:10:54:20 +0200] "GET /bliss/BlissFramework/BlissFramework/bliss_news HTTP/1.0" 200 39908 "http://172.22.51.74:8080/bliss" "Lynx/2.8.9rel.1 libwww-FM/2.14 SSL-MM/1.4.1 OpenSSL/1.0.2t"

( I used lynx to directly connect to Zope, so I could def. rule out Nginx/Ha -Proxy as a potential problem).

In my buildout.cfg access log is not configured explicitly, so I guess it uses the standard, ie. log to var/log/Z2-xxx.log

As a side, I use plone.recipe.zope2instance

Could you please advise me how to get back the logged in user name, which def. worked quite a while ago?

Thank you!

IIRC the username in the Z2 log worked OOTB just for HTTP Basic auth, not for cookie based ones (maybe Zope 4 changed something about this?)

On Plone 4 I used collective.usernamelogger for this. No idea if it works on Zope 4 without the ZServer (I bet on the "no").

We do like this (plone5.2 / py3)

<!-- ZPublish start -->
<subscriber
    for="ZPublisher.interfaces.IPubAfterTraversal"
    handler=".events.listening_after_traversal_publish"
/>

Then like this in the events.py

user_access_logger = logging.getLogger("DANBIO_USER")

def _remote_ip(request):
""" """
ip = request.get("HTTP_X_FORWARDED_FOR", "")
if not ip:
ip = request.get("REMOTE_ADDR", "")
return ip

def listening_after_traversal_publish(event):
""" """

request = event.request
logs = "\t".join(
    [
        str(request["AUTHENTICATED_USER"]),
        _remote_ip(request),
        request.getURL(),
        request.get("QUERY_STRING", ""),
    ]
)

user_access_logger.info(logs)

@jugmac00 Have you checked with the latest Plone 5.2.1 / python 3 / wsgi?

In Zope 4.1.3 (shipped with Plone 5.2.1) there is a fix that probably works for you (https://github.com/zopefoundation/Zope/pull/713)

If you are using Paste's translogger (default with plone.recipe.zope2instance), it should work without any further modification.

1 Like

Thank you all, and especially @mamico - you are right - I just verified it within my development environment - usernames get logged again with the latest Zope version.