Python Forum
how to log ip addresses in loguru - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: how to log ip addresses in loguru (/thread-42476.html)



how to log ip addresses in loguru - robertkwild - Jul-19-2024

Hi all,

I want to keep track in the logs as well as time message level etc, I want to log down the clients ip address

Is this possible

Thanks,
Rob


RE: how to log ip addresses in loguru - robertkwild - Jul-22-2024

ie when i run the software and in console i get the ip address of the pc accessing the web server

127.0.0.1 - - [21/Jul/2024 20:40:11] "POST /password HTTP/1.1" 200 -
obviously im accessing the web server from the same host but i do get different ips when i try to access the webserver from another LAN pc


RE: how to log ip addresses in loguru - robertkwild - Jul-22-2024

worked it out

@app.route('/password', methods=['GET', 'POST'])
def password():
ip_addr = request.remote_addr
form = PasswordForm()
if request.method == 'POST' and form.validate():
logger.warning(f'{ip_addr} {form.un.data} your current password is wrong, please click back and try again')
and in log file i get this

2024-07-22-12-55-27 | WARNING | 127.0.0.1 robert.wild your current password is wrong, please click back and try again