How to add a username to logging?

Could you tell me how to add username, ip to logging? Or nobody does that?
After all, we need to know full information about errors.

Not everything that gets logged happens as a result of an http request. Also, there are parts of code where the request doesn’t normally get passed, so you can’t even be sure of having this information available at the point of being logged.

You could include that information in your log formats, but then you need to ensure that every log statement either includes that information or passes “dummy” data as a result.

We took the simpler route. We include the username (when appropriate) within the text of the log message itself. e.g.:'{request.user.username} was here.')

As far as IP address is concerned, we do log that at the web server level (nginx), but that’s only to identify and block attempted hostile activity. We’ve never seen a value in trying to track that within the application itself. Between proxies, VPNs, firewalls, etc, it provides useful information less than half the time.

1 Like