Issue Bug Closed

#94043Incorrect usage of PSR-3 logger

Larry Garfield 05/03/21, 16:27 updated 06/16/21, 15:01

Description

The logger service is frequently used like this:

```
$this->logger->notice(‘The singleton “’ . $classSchema->getClassName() . ‘” needs a prototype in the constructor.’);
```

That is incorrect according to PSR-3. The string for the message is supposed to be static. If it has variable data (like the class name here), the string should have a placeholder and the value provided in the `$context` array. That is, it should be more like this:

```
$this->logger->notice(‘The singleton “{class}” needs a prototype in the constructor.’, [‘class’ => $classSchema->getClassName()]);
```

This is similar in structure and concept to sprintf() and prepared statements for SQL, and for the similar reasons.

1. It allows the separate data value to be escaped if necessary in an output-dependent way, rather than inlined without escaping. (Technically not doing so is an injection vector, though I’ve not seen a real vector in here yet.)
2. The message may be extracted and translated in order to provide localization of log messages, especially if they are shown on screen.
3. It allows for more robust tracking of what errors are being generated. For instance, one could search a DB table of log messages for the template string.

I am not sure what all the logging backend is doing right now, so it may be more complicated than just fixing all the call sites. But fixing all the call sites would be a really good set of newbie-tasks once we’re confident the backend is ready for it.

Reviews 1