This works only for blog pages, pages, and single posts. You need to disable redirection on the login page. Here you can find how to detect the login page. Check if wp-login is current page
So to redirect globally as you want use:
if(!is_user_logged_in() && !is_wplogin() ) wp_safe_redirect( wp_login_url() );
Function is_wplogin() is from here: https://wordpress.stackexchange.com/a/237285/181863
I also prefer to do redirection after init:
function is_wplogin(){
$ABSPATH_MY = str_replace(array('\\',"https://wordpress.stackexchange.com/"), DIRECTORY_SEPARATOR, ABSPATH);
return ((in_array($ABSPATH_MY.'wp-login.php', get_included_files()) || in_array($ABSPATH_MY.'wp-register.php', get_included_files()) ) || (isset($_GLOBALS['pagenow']) && $GLOBALS['pagenow'] === 'wp-login.php') || $_SERVER['PHP_SELF']== '/wp-login.php');
}
add_action('init', function () {
if (!is_user_logged_in() && !is_wplogin()) {
wp_redirect(wp_login_url());
die();
}
});