Find the last SQL statement executed for a given connection
You can enable "general query log", which will log each executed query, along with its session/connection ID, so if you grep the connection ID, you will have all the queries on one session. 

HOWEVER, 'sleep'ing connections are not the cause of max connection error, but a result of it. They are not harmful to stay there. In fact, they are established connections that can be re-used by future sessions. 

What would be more useful in this case is to search the slow query log, and check the innodb status to get information about locks and dead locks that have happened:

SHOW ENGINE INNODB STATUS;