I just read this article and it describes something as problematic which we do in serveral classes (e.g., the DB backend plugin): inside the static create() method, set $logger = $container->get('logger.factory')->get('search_api');.

Apparently, this can lead to problems in vaguely described (probably not even fully understood) circumstances, potentially involving AJAX (forms). The "proper" way of doing this is described in the handbook.

Questions:
- Does anyone know whether this really is a problem?
- Relatedly: Should we implement the "proper" way for this that's mentioned in the handbook?

Once decided, this is more or less a novice issue: just add the logger channels to the .services.yml files and then use them instead of the logger factory in constructors. (To find out which classes might use the wrong workflow: git grep -l 'logger.factory.*get.*search_api'.)

Comments

drunken monkey created an issue. See original summary.

borisson_’s picture

I'm not sure if this is a problem but it does look like a nicer way. So I'm all for this.

anicky’s picture

Assigned: Unassigned » anicky
Issue tags: +Novice, +DevDaysMilan

It seems to be a proper way for me too. I can handle this issue.

drunken monkey’s picture

OK, then let's do this. Thanks!

anicky’s picture

Status: Active » Needs review
StatusFileSize
new5.38 KB
drunken monkey’s picture

StatusFileSize
new8.9 KB
new11.28 KB

Great job, thanks a lot!
Already looks quite good, and seems like it should work. (Unfortunately, I don't think we're testing logging in any way at the moment, so it's hard to tell. But some quick manual testing worked fine, too.)
I just did a bit of clean-up and also replaced all \Drupal::logger() calls. Please see the attached patch.
Also removed a @todo comment and instead created a proper issue for it: #2753763: Add a logging trait which can also log exceptions.

If you're fine with these additional changes, I can commit it.

borisson_’s picture

Status: Needs review » Reviewed & tested by the community
chandeepkhosa’s picture

Assigned: anicky » Unassigned

  • drunken monkey committed 270a742 on 8.x-1.x authored by Anicky
    Issue #2716487 by Anicky, drunken monkey: Added module-specific logger...
drunken monkey’s picture

Status: Reviewed & tested by the community » Fixed

OK, thanks for reviewing!
Committed.
Thanks again, Anicky!

anicky’s picture

Status: Fixed » Needs review
StatusFileSize
new1.66 KB

Sorry about all the \Drupal::logger() calls. I only searched logger.factory...

It seems that you forgot some setLogger in your refactoring. I made a patch based on the latest, which was committed.

drunken monkey’s picture

Status: Needs review » Fixed

Sorry about all the </code> calls. I only searched <code>...

No problem at all, just noticed them myself – otherwise I'd have mentioned them in the IS.
Whether I inlined the container call was just based on whether there were other calls in the method – if all others use three lines, it seems weird if just one uses a single line. (Unifying this to always use a single line would be a different issue.)
Also, your patch contains trailing whitespace in the empty lines. But I think we'll just leave it at that.
Thanks again, in any case!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.