How to recognize that an SD card has been removed or inserted?

SD cards must be reinitialized if you remove power. If you remove an SD card while the file system is being updated you can cause a corrupt file system.

Newer SD cards buffer data in RAM so you must wait at least one second after the last SD access before removing power.

You should use a switch to tell the program you want to remove the card. When the program has closed the file and delayed for a second it can light an LED to tell you it is safe to remove the card.

Most SD sockets have a card detect switch. you could use the CD SW to detect when to initialize the card and reopen the file.

If you write a record about once a minute, the probability is low that you will corrupt the file system by just pulling the card since only a few ms is required to write the record. So if one in a few thousand are OK odds, just pull the card. You can connect the card detect switch to an interrupt.

I assume you flush data after every record.