Add logging with LoggableInterface

VerifiedSafe

Adds the LoggableInterface to a PHP class and implements the toLog() method. Rules specify which properties to include (IDs, statuses, dates) and exclude (passwords, secrets). Helps standardize business logging for debugging or event tracking.

Sby Skills Guide Bot
DevelopmentIntermediate
606/2/2026
Claude Code
#php#logging#loggable-interface#code-generation

Recommended for

Our review

Automatically adds the LoggableInterface and toLog() method to a PHP class, following security and type rules.

Strengths

  • Detects related classes that already implement LoggableInterface to call them in toLog()
  • Automatically excludes sensitive data (passwords, tokens)
  • Handles collections with precise type annotations for PHPStan

Limitations

  • Works only with PHP classes using the Atournayre\Contracts\Log\LoggableInterface
  • Does not refactor if the class already has a toLog() method
  • Requires nested classes to be compliant for optimal results
When to use it

When you need to add structured logging to an existing PHP class in a project using LoggableInterface.

When not to use it

If you use a different logging system (e.g., Monolog without a custom interface) or if the class is already properly logged.

Security analysis

Safe
Quality score85/100

The skill instructs to read and edit PHP files to add a logging interface; no execution of shell commands, no network access, and no destructive actions. Therefore, it's safe.

No concerns found

Examples

Ajouter LoggableInterface à une commande
Ajoute LoggableInterface à la classe Commande dans src/Entity/Commande.php en suivant les règles de sécurité et d'inclusion définies.
Loguer une collection d'articles
Applique le pattern LoggableInterface à la classe Panier dans src/Entity/Panier.php, en incluant les articles sous forme de collection avec count et items.

name: dev:log description: Ajoute des fonctionnalités de log au fichier en cours argument-hint: [FICHIER] allowed-tools: [Read, Edit, Grep, Glob] version: 1.0.0 license: MIT

Ajout de logging avec LoggableInterface

Instructions à Exécuter

IMPORTANT : Exécute ce workflow étape par étape :

Fichier cible : $ARGUMENTS

Workflow

1. Lire et analyser le fichier

  • Lire le contenu du fichier PHP
  • Identifier la classe principale et ses propriétés
  • Vérifier si LoggableInterface est déjà implémentée

2. Chercher les dépendances LoggableInterface

Pour chaque type-hint de propriété ou paramètre :

  • Utiliser Grep pour chercher si la classe implémente LoggableInterface
  • Noter les classes qui l'implémentent pour appeler ->toLog() dessus

3. Ajouter LoggableInterface

Si la classe n'implémente pas encore LoggableInterface :

Ajouter l'import :

use Atournayre\Contracts\Log\LoggableInterface;

Ajouter l'interface à la déclaration de classe :

final class NomClasse implements LoggableInterface

Ajouter la méthode toLog() :

/**
 * @return array<string, mixed>
 */
public function toLog(): array
{
    return [
        // Propriétés clés de l'objet
    ];
}

4. Règles pour le contenu de toLog()

Propriétés à inclure :

  • id (si présent)
  • Identifiants métier (email, code, référence...)
  • États/statuts importants
  • Dates clés (createdAt, updatedAt...)

Propriétés à exclure :

  • Mots de passe, tokens, secrets
  • Données personnelles sensibles
  • Propriétés volumineuses (blobs, textes longs)

Pour les objets imbriqués :

  • Si l'objet implémente LoggableInterface : 'relation' => $this->relation->toLog()
  • Sinon : 'relationId' => $this->relation->getId() ou ignorer

Pour les collections :

/**
 * @return array{count: int, items: array<int, array<string, mixed>>}
 */
public function toLog(): array
{
    return [
        'count' => $this->count(),
        'items' => array_map(fn ($item) => $item->toLog(), $this->items),
    ];
}

5. Vérification PHPStan

  • Toujours ajouter @return array<string, mixed> sur la méthode
  • Pour les collections : utiliser le type array shape approprié

Exemple de résultat

use Atournayre\Contracts\Log\LoggableInterface;

final class Commande implements LoggableInterface
{
    private Uuid $id;
    private string $reference;
    private Client $client;
    private \DateTimeImmutable $createdAt;
    private string $password; // Sensible, à exclure

    /**
     * @return array<string, mixed>
     */
    public function toLog(): array
    {
        return [
            'id' => $this->id->toRfc4122(),
            'reference' => $this->reference,
            'client' => $this->client->toLog(),
            'createdAt' => $this->createdAt->format('c'),
            // password exclu intentionnellement
        ];
    }
}
Related skills