Los Angeles, California (CNN) -- Actress Lindsay Lohan must remain in a drug rehabilitation program until January 3 instead of going to jail for violating her probation with a failed drug test, a judge ruled Friday.
If Lohan violates the rules of her rehab program or fails a drug test before her next court date on February 25, she will be sent to jail for 180 days, Los Angeles Superior Court Judge Elden Fox told her.
Staying clean and sober until then would end Lohan's supervised probation, and she can "put this long episode behind you," Fox said.
"Do you understand?" the judge asked her.
"Yes, sir," a sobbing Lohan said.
Fox had ordered the actress to prison for a month in September, but she was freed 14 hours after reporting to the facility when another judge granted her a $300,000 bail. Three days later, Lohan checked into the Betty Ford Center in Rancho Mirage, California.
"I wish that she could get it together, but I'm not quite sure we've gotten her attention yet," prosecutor Danette Meyers said Friday as she asked the judge to send the actress to jail for 180 days.
Meyers argued that Lohan has a pattern of checking into rehab "every time she has a violation and jail is leering over her head."
Defense lawyer Shawn Chapman Holley told the judge that Lohan "is trying her best" but "relapse is a part of recovery."
"She remains committed to the treatment program," Holley said.
Her probation officer recommended that Lohan be allowed to remain in rehab, Holley said.
While Lohan did not speak beyond short "yes" and "no" answers in court, she did send a handwritten letter to the judge. The contents were not immediately disclosed.
Fox, who took over Lohan's three-year-long case in August, said he thought Lohan's "epiphany may have been encouraged by the fact that (she) went to jail."
"I'm not going to be manipulated," the judge said as he began to lecture the 24-year-old actress. "You are an addict," Fox said. "I hope you understand this. You have to change your lifestyle."
Otherwise, Lohan will destroy her reputation and be "unemployable and uninsurable," he warned.
Lohan acknowledged in a series of Twitter messages last month that she failed the test and has a drug problem.
"Substance abuse is a disease, which unfortunately doesn't go away over night," Lohan wrote. "I am working hard to overcome it and am taking positive steps."
The Beverly Hills Courthouse has been the scene of several Lohan appearances over the past year related to violations of her probation for a 2007 drunken driving conviction.
In July, Judge Marsha Revel ordered the actress to serve 90 days in jail and 90 days in rehab for violating her probation.
The sheriff released her from jail after just 13 days because of overcrowding and her rehab stint at Ronald Reagan UCLA Medical Center ended after 23 days.
"She has learned her lesson," Holley told Fox as he agreed to let her out of rehab early on August 25.
Fox allowed Lohan to live in her West Hollywood, California, home as long as she followed a new set of rules, including regular drug testing. If she was successful for just 67 days, her supervised probation would end, he said.
"She's very serious about her sobriety," Holley said then. "She looks forward to proving to the court that she is taking this seriously."
But she failed a drug test less than a month later, triggering Friday's probation violation hearing.
Lohan's legal troubles began when she was arrested twice in 2007 on charges of driving under the influence, and in the second incident, she was charged with cocaine possession.
The first arrest came after Lohan lost control of her Mercedes-Benz convertible and struck a curb in Beverly Hills, California.
Just two weeks after checking out of a Malibu, California, drug and alcohol rehabilitation facility, she was arrested again in July 2007 after a woman called Santa Monica, California, police, saying Lohan was trying to run her down with a car.
Lohan's acting career, which started at 10 on the soap opera "Another World," took off on the big screen a year later when she played identical twins in Disney's remake of "The Parent Trap."
CNN's Alan Duke contributed to this report.