Skip to main content

LogSystem

This document provides a detailed explanation of the LogSystem class from the PINGOJS namespace. The class is designed to manage logging operations, including structured log entries, blocklists, and diff tracking.

Namespace The class is defined in the namespace:

use PINGOJS\LogSystem;

Description LogSystem provides functionality for:

  • Logging messages with different severity levels.
  • Managing structured log entries including timestamps, context, and sources.
  • Maintaining a blocklist of failed logins.
  • Tracking differences between previous and updated states.

Constructor

Error types

  • error_php
  • blocklist
  • user_activity
$log = new LogSystem($type, $level, $msg);

Parameters:

  • $type (string): Type of log (used to create separate log files).
  • $level (string): Severity level (e.g., DEBUG, INFO, WARN, ERROR, FATAL).
  • $msg (string): Message to log.

Level

  1. DEBUG: Detailed information, mainly for troubleshooting.
  2. INFO: General information about normal operation.
  3. WARN: Warnings that indicate possible problems.
  4. ERROR: Errors that affect normal operation, but the system continues to run.
  5. FATAL: Critical errors that cause the system to crash.

Description:
Initializes a new log entry and calls the first() method to populate initial fields.

Source Content

example:

new LogSystem("error_php", "FATAL", "entry of a php error")->source(
[
"service" => $errstr,
"file" => basename($errfile),
"line" => $errline
]
);

add Message

$log->msg();

Description:
Saves the current log entry to the log file.

diff

diff($previous, $updated)

$log->diff($oldData, $newData)

Parameters:

  • $previous (mixed): The previous state of the data.
  • $updated (mixed): The updated state of the data.

Description:
Adds a diff array to the log entry to track changes between previous and updated data.

get Block list

$blocklist = $log->getBlocklist();

Returns:
An array of failed login entries structured as [ip_address => details].

Description:
Retrieves all failed login attempts from the database and returns them in a structured blocklist format.

Internal Method: first($level, $msg)

Description:
Populates the initial log entry fields:

  • timestamp — current date and time.
  • level — log level.
  • message — the log message.
  • context — includes user UUID and IP address. Usage: Automatically called by the constructor.