L.A. Superior Court Commissioner Scott Gordon gave no reasons in his written decision, issued after lawyers met with him in a private session requested by Federline's lawyer. The ruling says that Federline gets custody starting Wednesday.
But the ruling came after the filing of misdemeanor charges against Spears – hit-and-run and driving without a valid California license – and claims by her ex-bodyguard that she used drugs and is a danger to her children, Sean Preston, 2, and Jayden James, 1.
Gordon also previously found that Spears is a "habitual, frequent and continuous" user of alcohol and controlled substances, and ordered her to submit to random twice-weekly testing.
Neither side would comment. But a source tells PEOPLE: "It's been leading up to this all along. Nothing is surprising to anyone. The judge laid out a specific plan for each person to follow. She clearly could not."
Another hearing is set for Wednesday.
"She seemed fine," the restaurant's owner, Tony Koursaris, says. "She was very comfortable having dinner here with her boys. ... The kids had a good time. Then we had to fight the paparazzi, of course."
The photographers, however, did come in handy for Spears at one point over the weekend. Around midnight Saturday, her Mercedes ran out of gas during a supermarket run. The photographers went to the gas station and helped Spears tank up her vehicle.
Reporting by PERNILLA CEDENHEIM, KEN LEE and MARY MARGARET
Who's turning 40? Which celebs are high school dropouts? What's hot and definitely not in fall fashion? Check out PEOPLE's new digital magazine now!