mirror of
https://github.com/nextcloud/documentation.git
synced 2026-01-02 17:59:36 +07:00
Logging works out of the box with the DI
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
@@ -4,43 +4,10 @@ Logging
|
||||
|
||||
.. sectionauthor:: Bernhard Posselt <dev@bernhard-posselt.com>
|
||||
|
||||
The logger can be injected from the ServerContainer:
|
||||
The logger is present by default in the container. The app that is Logging is
|
||||
set automatically.
|
||||
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
<?php
|
||||
namespace OCA\MyApp\AppInfo;
|
||||
|
||||
use \OCP\AppFramework\App;
|
||||
|
||||
use \OCA\MyApp\Service\AuthorService;
|
||||
|
||||
|
||||
class Application extends App {
|
||||
|
||||
public function __construct(array $urlParams=array()){
|
||||
parent::__construct('myapp', $urlParams);
|
||||
|
||||
$container = $this->getContainer();
|
||||
|
||||
/**
|
||||
* Controllers
|
||||
*/
|
||||
$container->registerService('AuthorService', function($c) {
|
||||
return new AuthorService(
|
||||
$c->query('Logger'),
|
||||
$c->query('AppName')
|
||||
);
|
||||
});
|
||||
|
||||
$container->registerService('Logger', function($c) {
|
||||
return $c->query('ServerContainer')->getLogger();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
and then be used in the following way:
|
||||
The logger can be used in the following way:
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
@@ -55,13 +22,13 @@ and then be used in the following way:
|
||||
private $logger;
|
||||
private $appName;
|
||||
|
||||
public function __construct(ILogger $logger, $appName){
|
||||
public function __construct(ILogger $logger, string $appName){
|
||||
$this->logger = $logger;
|
||||
$this->appName = $appName;
|
||||
}
|
||||
|
||||
public function log($message) {
|
||||
$this->logger->error($message, array('app' => $this->appName));
|
||||
$this->logger->error($message, array('extra_context' => 'my extra context'));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user