PHP Debug-class漏洞工件在网页上

I have this Debug class

class Debug {       
    static function log($string){
        file_put_contents('log.txt', $string."
", FILE_APPEND);
    }
}

And im using it to dump some stuff to my debug file.

For example, i have this code:

    public function getActiveLocks($ship){

        Debug::log("EW for ship #".$ship->id); //$ship->id = 1 

        for ($i = 0; $i < sizeof($this->ships); $i++){    
            $target = $this->ships[$i]->getCurrentPosition();
            $dist = Math::getDist2($pos, $target);

            if ($dist <= $ew->dist){
                Debug::log("potential lock vs: #".$this->ships[$i]->id);
            }
            else {
                Debug::log("no lock vs: #".$this->ships[$i]->id);
            }
        }
    }
}

Which has, as you notice, 3 calls to Debug::log. Now, the logging does happen correctly. However, occasionally the class is apparently leaking HTML into the document body. In that specific example above, the very Debug::log call results in my webpage displaying "))" in the body (in addition to properly writing into the log file).

If i delete the first entry, my HTML will be displayed just fine. Deleing the second or third entry wont delete the leaking "))". So, for some reason, the very first Debug::log call (which is called once!). results in echoing )). I dont know why or how, but its a major annoyance that i cant figure out. Also, its breaking my scriping at times.

Please note i left some variables out to just show the base problem im facing. Im certain i didnt leave anything important out.

Please advice.