Skip to content
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

Closed
wants to merge 13 commits into from

Conversation

aitboudad
Copy link
Contributor

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);
Copy link
Contributor

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.

@stloyd
Copy link
Contributor

stloyd commented Dec 14, 2013

You should add dev dependency for logger in composer.json.

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));
Copy link
Contributor

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)
Copy link
Member

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.

Copy link
Contributor

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;
Copy link
Member

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.
Copy link
Member

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"

Copy link
Member

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()
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

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.

Copy link
Contributor Author

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 :) ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, looks good

@fabpot
Copy link
Member

fabpot commented Dec 29, 2013

Can you rebase on current master?

@fabpot
Copy link
Member

fabpot commented Dec 29, 2013

Actually, as the trans and transChoice methods are called a lot on a typical website, I'd like to see how the new code impact the performance of these methods (with a logger that does nothing for the added channel as it will probably be the most common use case). So, I'm -1 on this PR.

@aitboudad
Copy link
Contributor Author

Well what do you think about injecting a logger only on mode debug ??

@fabpot
Copy link
Member

fabpot commented Jan 6, 2014

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()));
Copy link
Member

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

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sets

@fabpot
Copy link
Member

fabpot commented Jan 6, 2014

You should also adde some unit tests for this new feature and a CHANGELOG entry (in the component dir)

@aitboudad
Copy link
Contributor Author

closed in favor of #10014

@aitboudad aitboudad closed this Jan 13, 2014
@aitboudad aitboudad deleted the ticket_3015 branch January 13, 2014 23:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants