Cf: https://www.cnil.fr/sites/default/files/atoms/files/recomman... (in French, though)
You can freely collect data as a legal requirement, or for "legitimate interest" purposes such as fraud prevention. But you can't use the data you just collected for analytics without proper consent.
There is meant to be a sense of proportionality, but as many things with privacy laws it's subject to interpretation and intentionally left vague.
> Network and Information Security as Overriding Legitimate Interest
> stopping ‘denial of service’ attacks
Storing logs with IPs is no problem at all.
For instance, you can use this stored IP address to help identify whether your user has had their account breached, and prompt for extra verification before letting them log in. You can also do a full browser fingerprint for this purpose, this is all covered under legitimate basis.
However, once you use any of this data to market to the user then you are in breach of the GDPR as you did not have a consent basis for it. The storage was never a problem, it's the use of it that becomes a problem.
Unless you are a huge company or have a significant amount of customers in the UK/EU it's probably okay to ignore the GDPR.
You have the right to log IP addresses only if they are used for the two purposes you listed, otherwise you will need explicit consent.