MagratheaPHP2
Up to date Narrative last written —; no source changes since. Method signatures below are reflected live.

Logger — File-based Logging

File: src/Logger.php Namespace: Magrathea2 Extends: Singleton

Writes log entries to files on disk. Supports custom log file names, path configuration, and error logging with stack traces.


Configuration

Set the log directory in your config file:

[logs]
path = /var/log/myapp/

The log directory must be writable by the web server process.


Properties

PropertyTypeDescription
$logPathstringDirectory where log files are stored
$activeLogFilestringCurrent log file name (without path)

Methods

Initialize(): void

Initialize the logger (loads path from config). Called automatically on first use.

LoadLogsPath(): Logger

Reload the log path from config.

SetLogPath(string $p): Logger

Override the log directory path programmatically.

Logger::Instance()->SetLogPath("/custom/log/dir/");

GetLogPath(): string|null

Returns the current log directory path.

SetLogFile(string $name): Logger

Set the log file name (the file within the log directory). No need to add path or extension — just the base name.

Logger::Instance()->SetLogFile("payments"); // logs to /var/log/myapp/payments.log

GetLogFile(): string

Returns the current log file name.

GetFullLogFile(): string

Returns the full absolute path to the log file.

Log(string $logThis): void

Write a line to the log file. Throws Exception if the file is not writable.

Logger::Instance()->Log("User 42 logged in from 192.168.1.1");

LogError(\Exception $error): void

Write a formatted exception entry (message + stack trace) to the log.

try {
    // risky operation
} catch (\Exception $e) {
    Logger::Instance()->LogError($e);
}

LogLastError(): void

Log the last PHP error (from error_get_last()).


Basic Usage

use Magrathea2\Logger;

$logger = Logger::Instance();
$logger->Log("Application started");
$logger->Log("Processing request: " . $_SERVER["REQUEST_URI"]);

Logging Exceptions

use Magrathea2\Logger;

try {
    $result = Database::Instance()->Query($sql);
} catch (\Exception $e) {
    Logger::Instance()->LogError($e);
    // respond with error...
}

Multiple Log Files

You can switch log files to organize logs by category:

$logger = Logger::Instance();

// Write to payments.log
$logger->SetLogFile("payments")->Log("Payment processed: $orderId");

// Write to auth.log
$logger->SetLogFile("auth")->Log("Login failed for: $email");

// Back to default
$logger->SetLogFile("app")->Log("Something else happened");

Integration with MagratheaPHP Modes

When you call MagratheaPHP::Instance()->Prod(), the framework redirects errors to the logger instead of displaying them. The logger becomes the primary visibility mechanism for production errors.

// In production, exceptions bubble up to Logger automatically
MagratheaPHP::Instance()
    ->Prod()    // suppresses error display
    ->Load()
    ->Connect();

Log File Format

Each Log() call appends a line in this format:

[2024-03-15 14:30:00] Your log message here

Error entries include the stack trace:

[2024-03-15 14:30:00] ERROR: Database connection failed
Stack trace:
#0 /src/DB/Database.php(42): mysqli->__construct(...)
#1 /src/MagratheaPHP.php(88): Database::OpenConnectionPlease()
...

Notes

Class Reference — Logger

Magrathea2\Logger extends Singleton /home/platypusweb/platypusweb.com.br/site/magratheaphp2/src/Logger.php

Magrathea class for logging anything By default, the message is written with a timestamp before it. - For *Log* function, the default file is saved with a timestamp in the name - For *LogError* function, by default, all the data is saved in a same file called *log_error.log*

GetFullLogFile(): string

Gets active log file

GetLogFile(): string

returns active log file name

GetLogPath(): ?string

Returns log path

Initialize(): void
LoadLogsPath(): Magrathea2\Logger

loads the path for saving log

Log($logThis)

Logs a message - any message

ParamTypeDefault
$logThis mixed required
LogError($error)

Logs an error

ParamTypeDefault
$error mixed required
LogLastError()

Logs the last error from PHP

SetLogFile($name): Magrathea2\Logger

Sets the active log file

ParamTypeDefault
$name mixed required
SetLogPath($p): Magrathea2\Logger

Sets log path

ParamTypeDefault
$p mixed required

Examples