Where Did It Come From?
The origin of the phrase "burn the midnight oil" is fairly obvious: it harks back to pre-electricity times when people mostly used oil lamps for lighting. The reason, however, that the idiom refers primarily to work done by lamplight rather than leisure activities is based in a concept that predates oil lamps.
Back in the day, there was a verb in English which meant to work late by candlelight: "elucubrate". It has obviously passed out of common usage now, but can be found in sources such as Henry Cockeram's 1623 English Dictionary: "Elucubrate, to doe a thing by candlelight."
"Wee spend our mid-day sweat, or mid-night oyle;
Wee tyre the night in thought; the day in toyle."