If the problem is related to connecting or disconnecting via Bluetooth, you can save a Bluetooth diagnostic log by entering a command in Terminal.
On macOS 12 Monterey or later, enter:
sudo /System/Library/Frameworks/IOBluetooth.framework/Resources/BluetoothReporter --dumpPacketLog ~/Desktop/Bluetooth.log
and find the Bluetooth.log file on your desktop.
On earlier versions of macOS, enter:
sudo /System/Library/Frameworks/IOBluetooth.framework/Resources/BluetoothReporter
Finder will then open a window showing the BluetoothReporter_<date>.tar.gz report file.
It’s also helpful to install a Bluetooth logging profile and then save a sysdiagnose, as described here. This requires an Apple developer account. If you don’t have a developer account, you can save a regular sysdiagnose by choosing View ‣ Run System Diagnostics… in the Activity Monitor app.