move log to a function, validate possible nil params