MMOframework
Pourquoi l'IA Playground Bibliothèques Documentation Rechercher Agence
FR EN
Index

Log

MMO\core\events · 1 symbole · générée le 27 Oct 2026

class

Log

Log.php:18

A single log entry -- carries a level (one of the iLogLevel flags), a printf-style message + context, where it originated (file/line, and an optional call stack), and renders itself either as a plain apache-log-style line (__toString) or as a JSON-serializable structure (jsonSerialize).

class MMO\core\events\Log implements MMO\core\interfaces\iEvent, MMO\core\interfaces\iStoppableEvent, MMO\core\interfaces\iLogLevel, JsonSerializable, Stringable use MMO\core\classes\tEvent, MMO\core\classes\tStoppableEvent
Espace de noms
MMO\core\events
Implémente
iEvent, iStoppableEvent, iLogLevel, JsonSerializable, Stringable
Utilise les traits
tEvent, tStoppableEvent
Dispatched (via `\MMO\log()`/`Log::Send()`) so listeners (e.g. a file logger, a console printer) can react to it -- being an [`iStoppableEvent`](https://mmoframework.fr/fr/documentation/mmo.core/php/interfaces/iStoppableEvent.md#istoppableevent), an early listener can stop propagation to prevent later ones from also handling it.
Log::AddListener(fn(Log $log) => file_put_contents('app.log', (string)$log, FILE_APPEND)); new Log(iLogLevel::ERROR, 'user {id} not found', ['id' => 42]); // ->Send() is normally called for you, e.g. by \MMO\log()

Index

code readonly
context readonly
file readonly
line readonly
Listeners static
message readonly
num readonlya unique, per-request, incrementing number (starts from 1), useful to tell same-timestamp entries apart.
stopped
time readonlyunix timestamp (seconds) this entry was created at.
time_ms readonlythe sub-second part of $time, in microseconds (0-999999) -- despite the name, NOT milliseconds.
traces
AddListener($handler, $order) staticRegister a listener directly (as opposed to ConnectListeners's attribute-based discovery).
clearTraces()Discard $traces (e.g.
ConnectListeners($obj) staticScan $obj's methods for #[Listen(EventClass::class)] attributes and register each one as a listener on the matching event class.
DisconnectListeners($obj) staticUndo a previous ConnectListeners: removes every listener that connect had registered for $obj on this event class.
dispatch()Invoke every GetListeners listener with $this.
FromException($exception, $context, $level) staticBuild a Log entry from a caught exception, using the exception's own message and stack trace.
getLevel()
GetListeners() staticCollect every listener for this class, including those registered on its parent classes (so a subclass event is also caught by listeners of its parent event), sorted by ascending $order (ties keep …
getMessage($withOrigin)
getOrigin()
getTraceAsString($separator)
HasListener($handler) static
isPropagationStopped()
jsonSerialize()
RemoveListener($handler) staticRemove the last added handler from the listeners.
Send($args) staticConstruct a new instance of this class with $args, then dispatch it to every listener.
SetListenerOrder($handler, $order) staticChange the order of an already-registered listener, whether added directly via AddListener or indirectly via ConnectListeners (which also stores its [$obj, $methodName] callables here).
stopPropagation()Prevent any remaining listener in the current dispatch chain from being called.
__clone()A cloned event starts fresh: propagation is never considered stopped on the clone, even if it was on the original instance.
__construct($code, $message, $context)
__toString()

Propriétés11

code

readonly
readonly int $code
Type
int

context

readonly
readonly array $context
Type
array

file

readonly
readonly string $file
Type
string

line

readonly
readonly int $line
Type
int

Listeners

static
protected static array $Listeners = []
Type
array

message

readonly
readonly array|string $message
Type
array | string

num

readonly

a unique, per-request, incrementing number (starts from 1), useful to tell same-timestamp entries apart.

readonly int $num
Type
int

stopped

protected $stopped = false

time

readonly

unix timestamp (seconds) this entry was created at.

readonly int $time
Type
int

time_ms

readonly

the sub-second part of $time, in microseconds (0-999999) -- despite the name, NOT milliseconds.

readonly int $time_ms
Type
int

traces

array $traces
Type
array

Méthodes21

AddListener($handler, $order)

statictEvent.php:62

Register a listener directly (as opposed to ConnectListeners's attribute-based discovery).

static function AddListener(callable $handler, int $order = 0): void
$handlercallablecalled as $handler($eventInstance) when this event class is Send / dispatch-ed
$order = 0intlisteners run in ascending order (lower first); listeners with an equal order run in the order they were added
retournevoid
UserCreated::AddListener(function (UserCreated $e) {...}, 10);

clearTraces()

Log.php:183

Discard $traces (e.g. to shrink a Log entry before storing/transmitting it).

function clearTraces(): void
retournevoid

ConnectListeners($obj)

statictEvent.php:37

Scan $obj's methods for #[Listen(EventClass::class)] attributes and register each one as a listener on the matching event class. connect does the actual reflection/registration.

static function ConnectListeners(object|string $obj): void
$objobject | stringan instance (or class name, for static-only listener methods)
retournevoid
lève
class UserService { #[Listen(UserCreated::class, order: 1)] public function onUserCreated(UserCreated $e): void {...} } UserCreated::ConnectListeners(new UserService()); // onUserCreated() is now called on every UserCreated::Send()

DisconnectListeners($obj)

statictEvent.php:49

Undo a previous ConnectListeners: removes every listener that connect had registered for $obj on this event class.

static function DisconnectListeners(object|string $obj): void
$objobject | string
retournevoid
lève
UserCreated::DisconnectListeners($service); // $service's #[Listen] methods stop firing

dispatch()

tEvent.php:153

Invoke every GetListeners listener with $this. If the class also implements PSR-14's StoppableEventInterface, dispatch stops early as soon as isPropagationStopped() becomes true.

function dispatch(): static
retournestatic$this, for chaining
(new UserCreated('Marc'))->dispatch(); // what Send() does internally

FromException($exception, $context, $level)

staticLog.php:122

Build a Log entry from a caught exception, using the exception's own message and stack trace.

static function FromException(Throwable $exception, $context = [], $level = self::CRITICAL): self
$exceptionThrowable
$context = []extra context (e.g. ['origin' => $this]) -- 'exception' is set for you.
$level = self::CRITICALthe iLogLevel level to log at (TRACE is always added, to capture $exception's trace)
retourneself
Log::FromException($e, ['origin' => $this])->Send();

getLevel()

Log.php:193
function getLevel(): string
retournestringthe PSR-3 level name for $code (e.g. 'error', 'critical').

GetListeners()

statictEvent.php:123

Collect every listener for this class, including those registered on its parent classes (so a subclass event is also caught by listeners of its parent event), sorted by ascending $order (ties keep insertion order).

static function GetListeners(): array
retournearray[[callable $handler, int $order], ...]
UserCreated::GetListeners(); // [[$handlerB, 5], [$handlerA, 10]] -- lower order first

getMessage($withOrigin)

Log.php:158
function getMessage(bool $withOrigin = true): string
$withOrigin = trueboolif true (default) and an 'origin' was given in the context, prefix the message with it.
retournestringthe message, resolved against $context (printf-style, via printfd)
$log->getMessage(); // "MyOrigin: user 42 not found"

getOrigin()

Log.php:188
function getOrigin(): object|string
retourneobject | stringthe 'origin' given in the context, or the literal string 'UNKNOWN' if none was given.

getTraceAsString($separator)

Log.php:166
function getTraceAsString(string $separator = MMO\core\events\PHP_EOL): string
$separator = MMO\core\events\PHP_EOLstring
retournestring$traces rendered as "file:line name(args)" lines (oldest call first), joined by $separator.

HasListener($handler)

statictEvent.php:109
static function HasListener(callable|object $handler): bool
$handlercallable | objecteither the exact callable passed to AddListener, or (for a [$obj, $methodName] listener registered via ConnectListeners) just the object, to check whether any of its methods are currently listening
retournebool
UserCreated::HasListener($handler); // true if $handler is currently registeredUserCreated::HasListener($service); // true if any of $service's methods were connected via #[Listen]

jsonSerialize()

Log.php:128
function jsonSerialize(): mixed
retournemixedthe data used when this Log is JSON-encoded (level, num, time, file, line, message, traces).

RemoveListener($handler)

statictEvent.php:91

Remove the last added handler from the listeners.

static function RemoveListener(callable $handler): boolNOTE: if you added the same handler multiple time, you need to call this function the same amount of times.
$handlercallable
retournebooltrue if a listener was removed

Send($args)

statictEvent.php:143

Construct a new instance of this class with $args, then dispatch it to every listener.

static function Send(mixed ...$args): static
...$argsmixed
retournestaticthe constructed (and dispatched) event instance
UserCreated::Send('Marc'); // equivalent to (new UserCreated('Marc'))->dispatch()

SetListenerOrder($handler, $order)

statictEvent.php:75

Change the order of an already-registered listener, whether added directly via AddListener or indirectly via ConnectListeners (which also stores its [$obj, $methodName] callables here).

static function SetListenerOrder(callable $handler, int $order): bool
$handlercallable
$orderint
retournebooltrue if $handler was found and its order changed
UserCreated::SetListenerOrder($handler, 20); // moves $handler later in the dispatch order

stopPropagation()

tStoppableEvent.php:29

Prevent any remaining listener in the current dispatch chain from being called.

function stopPropagation()
retourne

__clone()

tStoppableEvent.php:38

A cloned event starts fresh: propagation is never considered stopped on the clone, even if it was on the original instance.

function __clone()
retourne

__construct($code, $message, $context)

Log.php:55
function __construct($code, string $message, array $context = [])
$codea bitmask combining one iLogLevel level constant with any of its flags (e.g. TRACE)
$messagestringa printf-style message (see printfd), resolved against $context
$context = []arrayprintf arguments, plus optional special keys: 'origin' (the object/string this log is about, used by getMessage/getOrigin), 'exception' (a \Throwable to build $traces from when TRACE is set), '$file'/'$line' (explicit origin location, overriding the auto-detected one)

__toString()

Log.php:141
function __toString(): string
retournestringthis entry rendered as a single apache-log-style line (with a trailing newline).
Membres hérités (19)
ConstantesDéclaré parDescription
ALERTiLogLevelAlert: Action must be taken immediately.
ALLiLogLevelMask for all
ALL_PLUSiLogLeveldebug everything
BACKTRACEiLogLevel
CRITICALiLogLevelCritical: Critical conditions.
DEBUGiLogLevelDebug: Detailed debug information.
DEEPiLogLeveldebug everything with arguments
EMERGENCYiLogLevelEmergency: System is unusable.
ERRORiLogLevelError: Runtime errors that do not require immediate action but should typically be logged and monitored.
ERROR_PLUSiLogLevelMask for all error
FATALiLogLevelMask for all fatal error
INFOiLogLevelInfo: Interesting events.
INFO_PLUSiLogLevelMask for all errors/warning/notices/info
MEMORYiLogLevelinclude backtrace object
NOTICEiLogLevelNotice: Normal but significant events.
NOTICE_PLUSiLogLevelMask for all errors/warning/notices/info
TRACEiLogLevelinclude backtrace as origins
WARNINGiLogLevelWarning: Exceptional occurrences that are not errors.
WARNING_PLUSiLogLevelMask for all errors and warnings