It's mostly what I did in this post, but to actually change the log level you'll need to check if serial is connected and use a flag to make sure you only change it once when it goes from not being connected to being connected.
In my case I only print 1 thing when I connect it to serial, then return the logger back to whatever it's default was (which is LOG_LEVEL_NONE
in production).
void setup() {
...
// Init logging (LOG_LEVEL_NONE for production, LOG_LEVEL_ALL for development)
setLogging(LOG_LEVEL_NONE);
}
Then you'll want something like this in the loop
bool serialWasPreviouslyConnected = false;
void loop() {
...
if (Serial.isConnected() && !serialWasPreviouslyConnected)
{
serialWasPreviouslyConnected = true;
setLogging(LOG_LEVEL_INFO);
}
if (!Serial.isConnected() && serialWasPreviouslyConnected)
{
serialWasPreviouslyConnected = false;
resetLoggingLevel();
}
}