public function write($level, $message, array $context = array())
    {
        $filename = $this->filenameLevels[$level] ?: $this->defaultFile;
        $context = array('__context' => $context);
        $log = $this->format($level, $message, $context);
        $logDir = dirname($filename);

        // Error are suppressed because an Exception will be thrown instead
        if (!is_dir($logDir)) {
            if (@mkdir($logDir, 0777, true) === false) {
                throw new RuntimeException('Failed to create log directory. Please check permissions.');
            }
        }

        if (@file_put_contents($filename, $log, FILE_APPEND | LOCK_EX) === false) {
            throw new RuntimeException('Failed to write log file. Please check permissions.');
        }

        $this->setLastLogLine($log);

        return true;
    }