Customer Logout programmatically in Magento 2.

Magento 2, You can log out customer programmatically by getting current customer session using the Model file, Magento\Customer\Model\Session

You need to call logout() function from above class dependency.
Call below dependency in __construct function,

<?php
public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Customer\Model\Session $customerSession
) {
    $this->redirect = $context->getRedirect();
    $this->customerSession = $customerSession;
}

public function customerLogout() {
    $customerId = $this->customerSession->getId();
    if($customerId) {
        $this->customerSession->logout()
             ->setBeforeAuthUrl($this->redirect->getRefererUrl())
             ->setLastCustomerId($customerId);
        return "Customer logout successfully";
    } else {
        return "Customer is not login";
    }
}

Call customerLogout() function from above file to programmatically log out a customer from the current session.

How to set redirect URL programmatically in magento 2?

In Magento 2, You can set redirect to any link URL by the Magento\Framework\Controller\Result\RedirectFactory Class.

How to get Product URL from product object Magento 2?

Magento 2, You can get Product URL from Product object. You can be assigned Product URL to any page in store for a redirect to Product page.

I hope you have Product Object, Get Product Object by SKU

If you want to get Only Url Key value, You can access by $product->getUrlKey() method.

You can product full URL by getProductUrl() method.

<?php 
$sku = '24-MB01';
$_product = $this->productRepository->get($sku);?>
<?php echo $_product->getProductUrl(); ?>