Move common code lines in the prepare_log function