When developing with Magento2 you have maybe come to the question were is my logger for example. This was the way in Magento before.
Mage::log('Some log message', null, 'file.log');
Maybe you have already found the logger here for example in the class Magento\Backup\Controller\Adminhtml\Index\Rollback.
$this->_objectManager->get('Psr\Log\LoggerInterface')->info($e->getMessage());
Today I don't want to show you the logger. More interesting for me are the shared instances inside of the Objectmanager that you can use with the fucntion get.
The general object manager can be found in this class Magento\Framework\ObjectManager\ObjectManager and it implements the interface \Magento\Framework\ObjectManagerInterface. Here you see the get function.
/**
* Retrieve cached object instance
*
* @param string $type
* @return mixed
*/
public function get($type)
{
$type = ltrim($type, '\\');
$type = $this->_config->getPreference($type);
if (!isset($this->_sharedInstances[$type])) {
$this->_sharedInstances[$type] = $this->_factory->create($type);
}
return $this->_sharedInstances[$type];
}
You can add your own shared instance with the preference tag in your etc/di.xml. For example here a snipped of magento/module-backend/etc/di.xml.
<?xml version="1.0"?>
<!--
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<!-- ... -->
<preference for="Magento\Framework\App\Config\Storage\WriterInterface" type="Magento\Framework\App\Config\Storage\Writer" />
<!-- ... -->
</config>
Here is a default list of shared instances that you can use with the get function.
type | instance |
---|---|
Psr\Log\LoggerInterface | Magento\Framework\Logger\Monolog |
Magento\Framework\View\Template\Html\MinifierInterface | Magento\Framework\View\Template\Html\Minifier |
Magento\Framework\ObjectManager\FactoryInterface | Magento\Framework\ObjectManager\Factory\Dynamic\Developer |
Magento\Framework\Search\Adapter\Mysql\Filter\PreprocessorInterface | Magento\Framework\Search\Adapter\Mysql\Filter\Preprocessor |
Magento\Framework\Search\Adapter\Mysql\Field\ResolverInterface | Magento\Framework\Search\Adapter\Mysql\Field\Resolver |
Magento\Framework\Search\Request\Aggregation\StatusInterface | Magento\Framework\Search\Request\Aggregation\Status |
Magento\Framework\Search\Adapter\Mysql\Field\FieldInterface | Magento\Framework\Search\Adapter\Mysql\Field\Field |
Magento\Framework\App\RequestInterface | Magento\Framework\App\Request\Http |
Magento\Framework\App\Request\PathInfoProcessorInterface | Magento\Store\App\Request\PathInfoProcessor |
Magento\Framework\App\ResponseInterface | Magento\Framework\App\Response\Http |
Magento\Framework\App\RouterListInterface | Magento\Framework\App\RouterList |
Magento\Framework\App\FrontControllerInterface | Magento\Framework\App\FrontController |
Magento\Framework\App\CacheInterface | Magento\Framework\App\Cache\Proxy |
Magento\Framework\App\Cache\StateInterface | Magento\Framework\App\Cache\State |
Magento\Framework\App\Cache\TypeListInterface | Magento\Framework\App\Cache\TypeList |
Magento\Store\Model\StoreManagerInterface | Magento\Store\Model\StoreManager |
Magento\Framework\View\DesignInterface | Magento\Theme\Model\View\Design\Proxy |
Magento\Framework\View\Design\ThemeInterface | Magento\Theme\Model\Theme |
Magento\Framework\View\Design\Theme\ResolverInterface | Magento\Theme\Model\Theme\Resolver |
Magento\Framework\View\ConfigInterface | Magento\Framework\View\Config |
Magento\Framework\View\Asset\Bundle\ConfigInterface | Magento\Framework\View\Asset\Bundle\Config |
Magento\Framework\Locale\ListsInterface | Magento\Framework\Locale\TranslatedLists |
Magento\Framework\Api\AttributeTypeResolverInterface | Magento\Framework\Reflection\AttributeTypeResolver |
Magento\Framework\Api\Search\SearchResultInterface | Magento\Framework\Api\Search\SearchResult |
Magento\Framework\Api\Search\SearchCriteriaInterface | Magento\Framework\Api\Search\SearchCriteria |
Magento\Framework\Api\Search\DocumentInterface | Magento\Framework\Api\Search\Document |
Magento\Framework\Api\Search\AggregationInterface | Magento\Framework\Search\Response\Aggregation |
Magento\Framework\Api\ExtensionAttribute\JoinDataInterface | Magento\Framework\Api\ExtensionAttribute\JoinData |
Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface | Magento\Framework\Api\ExtensionAttribute\JoinProcessor |
Magento\Framework\Locale\ConfigInterface | Magento\Framework\Locale\Config |
Magento\Framework\Notification\NotifierInterface | Magento\Framework\Notification\NotifierPool |
Magento\Framework\UrlInterface | Magento\Framework\Url |
Magento\Framework\Url\EncoderInterface | Magento\Framework\Url\Encoder |
Magento\Framework\Url\DecoderInterface | Magento\Framework\Url\Decoder |
Magento\Framework\Data\Collection\Db\FetchStrategyInterface | Magento\Framework\Data\Collection\Db\FetchStrategy\Query |
Magento\Framework\Config\ScopeInterface | Magento\Framework\Config\Scope |
Magento\Framework\Config\FileResolverInterface | Magento\Framework\App\Config\FileResolver |
Magento\Framework\Config\CacheInterface | Magento\Framework\App\Cache\Type\Config |
Magento\Framework\Config\ValidationStateInterface | Magento\Framework\App\Arguments\ValidationState |
Magento\Framework\Module\ModuleListInterface | Magento\Framework\Module\ModuleList |
Magento\Framework\Component\ComponentRegistrarInterface | Magento\Framework\Component\ComponentRegistrar |
Magento\Framework\Event\ConfigInterface | Magento\Framework\Event\Config |
Magento\Framework\Event\InvokerInterface | Magento\Framework\Event\Invoker\InvokerDefault |
Magento\Framework\Interception\PluginListInterface | Magento\Framework\Interception\PluginList\PluginList |
Magento\Framework\Event\ManagerInterface | Magento\Framework\Event\Manager\Proxy |
Magento\Framework\View\LayoutInterface | Magento\Framework\View\Layout |
Magento\Framework\View\Layout\ProcessorInterface | Magento\Framework\View\Model\Layout\Merge |
Magento\Framework\View\Url\ConfigInterface | Magento\Framework\View\Url\Config |
Magento\Framework\App\Route\ConfigInterface | Magento\Framework\App\Route\Config |
Magento\Framework\App\ResourceConnection\ConfigInterface | Magento\Framework\App\ResourceConnection\Config\Proxy |
Magento\Framework\Oauth\OauthInterface | Magento\Framework\Oauth\Oauth |
Magento\Framework\View\Design\Theme\Domain\PhysicalInterface | Magento\Theme\Model\Theme\Domain\Physical |
Magento\Framework\View\Design\Theme\Domain\VirtualInterface | Magento\Theme\Model\Theme\Domain\Virtual |
Magento\Framework\View\Design\Theme\Domain\StagingInterface | Magento\Theme\Model\Theme\Domain\Staging |
Magento\Framework\Json\EncoderInterface | Magento\Framework\Json\Encoder |
Magento\Framework\Json\DecoderInterface | Magento\Framework\Json\Decoder |
Magento\Framework\Message\ManagerInterface | Magento\Framework\Message\Manager |
Magento\Framework\App\Config\ValueInterface | Magento\Framework\App\Config\Value |
Magento\Framework\Interception\ChainInterface | Magento\Framework\Interception\Chain\Chain |
Magento\Framework\Module\Output\ConfigInterface | Magento\Framework\Module\Output\Config |
Magento\Framework\View\Design\Theme\CustomizationInterface | Magento\Framework\View\Design\Theme\Customization |
Magento\Framework\View\Asset\ConfigInterface | Magento\Framework\View\Asset\Config |
Magento\Framework\Image\Adapter\ConfigInterface | Magento\Framework\Image\Adapter\Config |
Magento\Framework\View\Design\Theme\Image\PathInterface | Magento\Theme\Model\Theme\Image\Path |
Magento\Framework\Session\Config\ConfigInterface | Magento\Framework\Session\Config |
Magento\Framework\Session\SidResolverInterface | Magento\Framework\Session\SidResolver\Proxy |
Magento\Framework\Stdlib\Cookie\CookieScopeInterface | Magento\Framework\Stdlib\Cookie\CookieScope |
Magento\Framework\Stdlib\Cookie\CookieReaderInterface | Magento\Framework\Stdlib\Cookie\PhpCookieReader |
Magento\Framework\Stdlib\CookieManagerInterface | Magento\Framework\Stdlib\Cookie\PhpCookieManager |
Magento\Framework\TranslateInterface | Magento\Framework\Translate |
Magento\Framework\Config\ScopeListInterface | interceptionConfigScope |
Magento\Framework\View\Design\Theme\Label\ListInterface | Magento\Theme\Model\ResourceModel\Theme\Collection |
Magento\Framework\Mview\ConfigInterface | Magento\Framework\Mview\Config |
Magento\Framework\Mview\ViewInterface | Magento\Framework\Mview\View |
Magento\Framework\Mview\ProcessorInterface | Magento\Framework\Mview\Processor |
Magento\Framework\Mview\View\CollectionInterface | Magento\Framework\Mview\View\Collection |
Magento\Framework\Mview\View\SubscriptionInterface | Magento\Framework\Mview\View\Subscription |
Magento\Framework\Mview\View\ChangelogInterface | Magento\Framework\Mview\View\Changelog |
Magento\Framework\Api\MetadataServiceInterface | Magento\Framework\Api\DefaultMetadataService |
Magento\Framework\Api\MetadataObjectInterface | Magento\Framework\Api\AttributeMetadata |
Magento\Framework\Api\SearchCriteriaInterface | Magento\Framework\Api\SearchCriteria |
Magento\Framework\App\Rss\UrlBuilderInterface | Magento\Framework\App\Rss\UrlBuilder |
Magento\Framework\DB\LoggerInterface | Magento\Framework\DB\Logger\Quiet |
Magento\Framework\App\ResourceConnection\ConnectionAdapterInterface | Magento\Framework\Model\ResourceModel\Type\Db\Pdo\Mysql |
Magento\Framework\DB\QueryInterface | Magento\Framework\DB\Query |
Magento\Framework\App\ProductMetadataInterface | Magento\Framework\App\ProductMetadata |
Magento\Framework\Acl\CacheInterface | Magento\Framework\Acl\Cache |
Magento\Framework\App\AreaInterface | Magento\Framework\App\Area |
Magento\Framework\Setup\ModuleDataSetupInterface | Magento\Setup\Module\DataSetup |
Magento\Framework\AuthorizationInterface | Magento\Framework\Authorization |
Magento\Framework\Authorization\PolicyInterface | Magento\Framework\Authorization\Policy\DefaultPolicy |
Magento\Framework\Authorization\RoleLocatorInterface | Magento\Framework\Authorization\RoleLocator\DefaultRoleLocator |
Magento\Framework\Session\SessionManagerInterface | Magento\Framework\Session\Generic |
Magento\Framework\App\Config\ScopeConfigInterface | Magento\Framework\App\Config |
Magento\Framework\App\Config\ReinitableConfigInterface | Magento\Framework\App\ReinitableConfig |
Magento\Framework\App\Config\MutableScopeConfigInterface | Magento\Framework\App\MutableScopeConfig |
Magento\Framework\App\Config\Storage\WriterInterface | Magento\Framework\App\Config\Storage\Writer |
Magento\Framework\Config\ConverterInterface | Magento\Framework\Config\Converter\Dom |
Magento\Framework\App\DefaultPathInterface | Magento\Framework\App\DefaultPath\DefaultPath |
Magento\Framework\Encryption\EncryptorInterface | Magento\Framework\Encryption\Encryptor |
Magento\Framework\Filter\Encrypt\AdapterInterface | Magento\Framework\Filter\Encrypt\Basic |
Magento\Framework\Cache\ConfigInterface | Magento\Framework\Cache\Config |
Magento\Framework\View\Asset\MergeStrategyInterface | Magento\Framework\View\Asset\MergeStrategy\Direct |
Magento\Framework\App\ViewInterface | Magento\Framework\App\View |
Magento\Framework\Data\Collection\EntityFactoryInterface | Magento\Framework\Data\Collection\EntityFactory |
Magento\Framework\Translate\InlineInterface | Magento\Framework\Translate\Inline |
Magento\Framework\Session\ValidatorInterface | Magento\Framework\Session\Validator |
Magento\Framework\Session\StorageInterface | Magento\Framework\Session\Storage |
Magento\Framework\Url\RouteParamsResolverInterface | Magento\Framework\Url\RouteParamsResolver |
Magento\Framework\Url\QueryParamsResolverInterface | Magento\Framework\Url\QueryParamsResolver |
Magento\Framework\Url\ScopeResolverInterface | Magento\Framework\Url\ScopeResolver |
Magento\Framework\Url\SecurityInfoInterface | Magento\Framework\Url\SecurityInfo\Proxy |
Magento\Framework\Locale\CurrencyInterface | Magento\Framework\Locale\Currency |
Magento\Framework\CurrencyInterface | Magento\Framework\Currency |
Magento\Framework\Locale\FormatInterface | Magento\Framework\Locale\Format |
Magento\Framework\Locale\ResolverInterface | Magento\Framework\Locale\Resolver |
Magento\Framework\Stdlib\DateTime\TimezoneInterface | Magento\Framework\Stdlib\DateTime\Timezone |
Magento\Framework\Module\ResourceInterface | Magento\Framework\Module\ModuleResource |
Magento\Framework\Pricing\Amount\AmountInterface | Magento\Framework\Pricing\Amount\Base |
Magento\Framework\Api\SearchResultsInterface | Magento\Framework\Api\SearchResults |
Magento\Framework\Api\AttributeInterface | Magento\Framework\Api\AttributeValue |
Magento\Framework\Model\ResourceModel\Db\TransactionManagerInterface | Magento\Framework\Model\ResourceModel\Db\TransactionManager |
Magento\Framework\Api\Data\ImageContentInterface | Magento\Framework\Api\ImageContent |
Magento\Framework\Api\ImageContentValidatorInterface | Magento\Framework\Api\ImageContentValidator |
Magento\Framework\Api\ImageProcessorInterface | Magento\Framework\Api\ImageProcessor |
Magento\Framework\Code\Reader\ClassReaderInterface | Magento\Framework\Code\Reader\ClassReader |
Magento\Framework\Stdlib\DateTime\DateTimeFormatterInterface | Magento\Framework\Stdlib\DateTime\DateTimeFormatter |
Magento\Framework\Api\Search\SearchInterface | Magento\Framework\Search\Search |
Magento\Framework\View\Design\FileResolution\Fallback\ResolverInterface | Magento\Framework\View\Design\FileResolution\Fallback\Resolver\Simple |
Magento\Framework\Session\SaveHandlerInterface | Magento\Framework\Session\SaveHandler |