0

I'm currently logging all interactions to my database (added user, updated user, ...) in one table.

An example from my userService:

public function addUserAction() 
{
    $this->logger->alert("Added user!");

    // Code that adds the user to the database (not relevant for my question)
    // ...
}

public function updateUserAction($user)
{
    $this->logger->alert("Updated user!");

    // Code that updates the user to the database (not relevant for my question)
    // ...
}

Now I'd like to have separate tables as followed: log_added_users, log_updated_users, etc ...

I have one channel and one handler configured in my config.yml I have injected the logger into the userService and tagged it with my channel.

services:
    userService:
        class: Acme\Services\UserService
        arguments: ["@logger"]
        tags:
            - { name: monolog.logger, channel: my_channel }

I'm wondering where I should define in which table to add a certain log entry? I don't think I need more channels as I can only define one for the injected logger (as seen in the config snippet above)

So I'm thinking to add more handlers (one per db table). The thing is: when the log is being written, it will go through all handlers and thus add the same log to all tables?

Or should I just put my updateUserAction in another service, so I will be able to work with another channel?

Any help is appreciated :)

mattyh88
  • 1,585
  • 5
  • 26
  • 48

1 Answers1

0

I found out that, for every channel you create, a service gets created automatically for you. So I just injected another logger into the userService and tagged them with the new channel.

That way I can say for example:

public function addUserAction()
{
    $this->logger->alert("user added");
}

public function updateUserAction($user)
{
    $this->updateLogger->alert("user updated");
}
mattyh88
  • 1,585
  • 5
  • 26
  • 48