Database::setActiveConnection not working as expected? kint(Database::setActiveConnection('preview'));
\Drupal::logger('Little kitties')-&gt;info('Mau');
kint(Database::setActiveConnection('default'));


As my expectation this would create a watchdog entry in preview database. 
The entry got created in the default database instead. 

This means, setActiveConnection() only affects direct queries (select and db_query..)? That would be really sad.
Database::setActiveConnection() doesn't influence those objects that already have a \Drupal\Core\Database\Connection object, which means that the following code will not use the preview connection for altering the database table.

use \Drupal\Core\Database\Database;

$schema = Database::getConnection()-&gt;schema();
$old_key = Database::setActiveConnection('preview');
$schema-&gt;addField($table, $field, $spec, $keys_new);
Database::setActiveConnection($old_key);


When you call \Drupal::logger(), you get a DbLog object that was already initialised before Database::setActiveConnection('preview') is executed, which means it will use the old connection.

To be able to use the new connection for logging, you should try creating a custom logger that uses the connection you want, without you setting the connection before calling its methods.

As side note, Database::setActiveConnection() returns the old key; it's easy to restore it: Just call again Database::setActiveConnection() passing the value returned from the previous call, as I did with the code I shown.