Skip to content

Instantly share code, notes, and snippets.

@stephenfeather
Created September 9, 2022 00:59
Show Gist options
  • Save stephenfeather/3def5e52193488b31539b653e5f64082 to your computer and use it in GitHub Desktop.
Save stephenfeather/3def5e52193488b31539b653e5f64082 to your computer and use it in GitHub Desktop.

The logging class courtesy of https://github.com/thielicious/Logger

class Logger {

    private
        $file,
        $timestamp;

    public function __construct($filename) {
        $this->file = $filename;
    }

    public function setTimestamp($format) {
        $this->timestamp = date($format)." » ";
    }

    public function putLog($insert) {
        if (isset($this->timestamp)) {
            file_put_contents($this->file, $this->timestamp.$insert."<br>", FILE_APPEND);
        } else {
            trigger_error("Timestamp not set", E_USER_ERROR);
        }
    }

    public function getLog() {
        $content = @file_get_contents($this->file);
        return $content;
    }

}

Example: Saving:

$log = new Logger("log.txt");
$log->setTimestamp("D M d 'y h.i A");

if (user logs in) {
    $log->putLog("Successful Login: ".$_SESSION["user_name"]);
}
if (user logs out) {
    $log->putLog("Logout: ".$_SESSION["user_name"]);
}

Reading:

$log->getLog();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment