Skip to content

Conversation

@cosmastech
Copy link
Contributor

@cosmastech cosmastech commented Aug 22, 2025

This is related to laravel/pail#52

If you call app(\Illuminate\Log\Logger::class)->debug("Totwells - Taylor Otwell's Arkansas style tater tots") using Pail (and I would bet Telescope too), it will record two separate log entries, because they react to the MessageLogged event.

Instantiating Logger sets the $logger instance to LogManager. Therefore, the Logger class and its $logger both dispatch the event.

@cosmastech cosmastech marked this pull request as draft August 22, 2025 12:50
@cosmastech cosmastech force-pushed the avoid-dispatching-MessageLogged-twice branch from 72e7b0b to 12d7c55 Compare August 22, 2025 12:56
@cosmastech cosmastech marked this pull request as ready for review August 22, 2025 12:56
@cosmastech cosmastech changed the title [12.x] Do not write MessageLogged twice [12.x] Do not dispatch MessageLogged twice Aug 22, 2025
@taylorotwell
Copy link
Member

Where and how do both dispatch the event?

@cosmastech
Copy link
Contributor Author

Where and how do both dispatch the event?

@taylorotwell

When you build the \Illuminate\Log\Logger, the container supplies a LogManager for the $logger instance.

Therefore, $logger->debug() calls $logger->logger->debug(). This means Logger calls LogManager which will call another Logger instance. So the original Logger instance in userland will dispatch the MessageLogged event AFTER the LogManager calls its Logger instance, which will have already dispatched the MessageLogged event.

@cosmastech
Copy link
Contributor Author

sequenceDiagram
    autonumber
    participant U as Userland code
    participant LA as Illuminate\Log\Logger (outer)
    participant LM as Illuminate\Log\LogManager
    participant LB as Illuminate\Log\Logger (inner/channel)
    participant M as Monolog\Logger
    participant E as Event Dispatcher

    U->>LA: debug("...")
    activate LA
    LA->>LM: debug("...")
    activate LM
    LM->>LB: resolve channel → debug("...")
    activate LB
    LB->>M: debug()
    M-->>LB: write complete
    LB->>E: dispatch MessageLogged (1st)
    deactivate LB
    LM-->>LA: return
    deactivate LM
    LA->>E: dispatch MessageLogged (2nd)
    deactivate LA
Loading

here's a chart that ChatGippity generated for me to demonstrate.

@taylorotwell taylorotwell merged commit ddeee72 into laravel:12.x Aug 22, 2025
59 of 60 checks passed
@cosmastech cosmastech deleted the avoid-dispatching-MessageLogged-twice branch August 22, 2025 14:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants