the x<0 case is 0 which is in fact the correct gradient.

Not sure how you define “correct gradient” here? The function has no value there.

Why is it not possible to make both return nans or both return the correct gradients ?

We just use 1/x for the gradient of log. So when x is 0, it get to inf/nan. But when it is negative, you just get other wrong values.