-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Translation] Added logging capability to translator #9770
Conversation
aitboudad
commented
Dec 14, 2013
Q | A |
---|---|
Bug fix? | no |
New feature? | yes |
BC breaks? | no |
Deprecations? | no |
Tests pass? | yes |
Fixed tickets | #3015 |
License | MIT |
@@ -185,6 +191,8 @@ public function trans($id, array $parameters = array(), $domain = null, $locale | |||
$this->loadCatalogue($locale); | |||
} | |||
|
|||
$catalogue = $this->filterCatalogueForId($this->catalogues[$locale], $id, $domain); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This $catalogue
variable is not used anywhere.
You should add dev dependency for logger in |
if ($catalogue->has($id, $domain)) { | ||
$this->logger->info('Translator: using fallback catalogue.', array('id' => $id, 'domain' => $domain)); | ||
} else { | ||
$this->logger->warn('Translator: translation not found.', array('id' => $id, 'domain' => $domain)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
be compliant with PRS logger interface -> warning
* @param string $domain | ||
* @return MessageCatalogueInterface | ||
*/ | ||
protected function filterCatalogueForId(MessageCatalogueInterface $catalogue, $id, $domain) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we don't use protected by default but private. also this method doesnt make much sense since it only returns $catalogue that is passed to it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think its better to only call this method when there is an actual logger, as calling a method which does nothing (in production) adds up the method-calls. And the trans(Choice) method is potentially called 20+ times or so. So any performance gain (even how small is) is a big plus.
@@ -54,6 +55,11 @@ class Translator implements TranslatorInterface | |||
private $selector; | |||
|
|||
/** | |||
* @var LoggerInterface|null | |||
*/ | |||
protected $logger; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be private
@@ -244,6 +271,22 @@ protected function loadCatalogue($locale) | |||
$this->loadFallbackCatalogues($locale); | |||
} | |||
|
|||
/** | |||
* Extension point for handling catalogue misses or fallbacks. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
its not an "extension point"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also the method name is quite strange. it does not "filter" anything. why not simply call it log
@@ -398,6 +398,7 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode) | |||
->canBeEnabled() | |||
->children() | |||
->scalarNode('fallback')->defaultValue('en')->end() | |||
->scalarNode('logger_id')->defaultNull()->end() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no point in providing the logger id IMO. It is totally inconsistent with all other places in Symfony. You should inject the logger
service instead, and use a dedicated Monolog channel through the tag.
the logging configuration should be a boolean to enable/disable it if you want it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well, using a different channel should be done in core, like for other services supporting logging.
Thus, most services don't even ask you whether they should enable logging or no. they just enable it all the time as soon as the logger
service is available.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stof I will delete logger_id, and add channel 'translation' in core.
are you agree :) ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, looks good
Can you rebase on current master? |
Actually, as the |
Well what do you think about injecting a logger only on mode debug ?? |
Injection the logger only in debug more is probably a better idea. |
private function log(MessageCatalogueInterface $catalogue, $id, $domain) | ||
{ | ||
if ($catalogue->has($id, $domain)) { | ||
$this->logger->debug('Translation use fallback catalogue.', array('id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logger can be null here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's checked in if (null !== $this->logger && !$catalogue->defines($id, $domain))
, but maybe it'll be a good idea to move checking if logger exists inside the method.
@@ -223,6 +240,16 @@ public function transChoice($id, $number, array $parameters = array(), $domain = | |||
} | |||
|
|||
/** | |||
* Set the logger. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sets
You should also adde some unit tests for this new feature and a CHANGELOG entry (in the component dir) |
closed in favor of #10014 |