pohja/lib/Logger.php

40 lines
No EOL
1.3 KiB
PHP

<?php
class Logger {
const INFO = "INFO";
const WARNING = "WARNING";
const ERROR = "ERROR";
const DEBUG = "DEBUG";
const IOREAD = "IOREAD";
const IOWRITE = "IOWRITE";
const METRICS = "METRICS";
public static function getColor(string $level):string {
match ($level) {
self::INFO => $levelColor = "\033[0;32m",
self::WARNING => $levelColor = "\033[0;33m",
self::ERROR => $levelColor = "\033[0;31m",
self::DEBUG => $levelColor = "\033[0;34m",
self::IOREAD => $levelColor = "\033[0;35m",
self::IOWRITE => $levelColor = "\033[0;36m",
self::METRICS => $levelColor = "\033[0;41m",
default => $levelColor = "\033[0;37m"
};
return $levelColor;
}
public static function log(string $message, string $level = "INFO", string $context = "") {
$db = new Database();
$stmt = $db::$handle->prepare("INSERT INTO `logs` (`level`, `message`, `context`, `created`) VALUES (:level, :message, :context, strftime('%Y-%m-%d %H:%M:%S','now'))");
$stmt->bindValue(":level", $level, SQLITE3_TEXT);
$stmt->bindValue(":message", $message, SQLITE3_TEXT);
$stmt->bindValue(":context", $context, SQLITE3_TEXT);
@$stmt->execute();
$levelColor = self::getColor($level);
echo "\033[0;37m[" . date("Y-m-d H:i:s") . "] " . $levelColor . $level . "\033[0;37m: " . $message . "\033[0m" . PHP_EOL;
}
}