Original file: src/Fsm/Services/FsmLogger.php
The FsmLogger class is part of a PHP application that implements a finite state machine (FSM) logging mechanism. This class facilitates tracking FSM transitions and errors effectively through organized logging. It utilizes structured and unstructured logging based on configuration settings, and it assists in capturing relevant information about the context of each FSM transition.
The FsmLogger reads configuration settings regarding logging preferences, captures relevant state transition data, manages sensitive information filtering, and logs the transition events either as successes or failures. The integration with external logging libraries also allows the flexibility of different logging channels and formats.
private static function extractUserId($state): ?string
Extracts the user_id from a given state object regardless of its visibility (public or private).
object|null): The state object from which to extract the user_id. It can be null.The method ensures that it can extract the user_id from various scenarios:
user_id.getUserId.protected function logToChannel(array $data, bool $isFailure = false): void
Writes log information to the specified logging channel based on the application configuration.
array<string, mixed>): An associative array containing the logging data.bool): Indicates whether the log corresponds to a failed FSM transition.The method checks:
protected function normalizeState(FsmStateEnum|string $state): string
Normalizes the state value, ensuring it is consistently represented as a string.
FsmStateEnum|string): An FSM state that can either be an instance of FsmStateEnum or a string.This method verifies if the input state is an enumeration (instance of FsmStateEnum). If so, it retrieves the value corresponding to that enumeration; otherwise, it returns the state as-is.
protected function filterContextForLogging(?ArgonautDTOContract $context): ?array
Prepares context data for logging by filtering out any sensitive information.
ArgonautDTOContract|null): The context object to be filtered, which can be null.The method attempts to convert the DTO context into an array. It retrieves a list of sensitive keys from configuration and subsequently removes any sensitive data via recursive filtering.
protected function recursivelyRemoveSensitiveKeys(array $data, array $sensitiveKeys, string $prefix = ''): array
Recursively removes sensitive keys from an associative array, ensuring compliance with privacy constraints.
array<string, mixed>): The input data from which sensitive keys need to be removed.array<int, string>): An array of keys that should be filtered out.string): The current key prefix for nested arrays.The method performs a depth-first search through the data structure, checking each key against the list of sensitive keys and removing those that match or correspond to wildcard entries.
protected function subjectFromVerbs(): ?array
Retrieves subject information from the current Verbs state when applicable.
It checks whether the logging of user subject data from Verbs is enabled, attempts to access the current Verbs state, and extracts the user_id, aligning it with its model type for logging.
public function logSuccess(
Model $model,
string $columnName,
FsmStateEnum|string|null $fromState,
FsmStateEnum|string $toState,
?string $transitionEvent,
?ArgonautDTOContract $context,
?int $durationMs = null
): void
Records a successful FSM transition into the logs and optionally into the database.
Model): The model instance involved in the transition.string): The DB column related to the FSM.FsmStateEnum|string|null): The state before the transition, can be null.FsmStateEnum|string): The state after the transition.string|null): Optional event name that triggered the transition.ArgonautDTOContract|null): Optional context data associated with the transition.int|null): Optional duration of the transition in milliseconds.The method creates a structured log entry, adds to the database log, and then logs the data to the configured logging channel. It handles transitions cleanly and checks if logging is enabled through configuration.
public function logFailure(
Model $model,
string $columnName,
FsmStateEnum|string|null $fromState,
FsmStateEnum|string $to
How can I help you explore Laravel packages today?