You’re not allowed to do that without reason. If you have a reason, and tell your users, there’s nothing wrong with it.
Some anecdata that proves that:
https://gdpr.eu/privacy-policy/:
“IP and browser user agent string: this data is collected when you leave a comment.”
https://commission.europa.eu/privacy-policy-websites-managed...:
“In addition, IP addresses and device IDs might be saved for one year in the log files of the Directorate-General for Informatics operational environment for security or other purposes (see DPR-EC-02886 DIGIT IT security operations and services for more information).”