Logging

The logger is present by default in the container. The app that is logging is set automatically. Nextcloud uses a PSR3 logger.

The logger can be used in the following way:

<?php
namespace OCA\MyApp\Service;

use Psr\Log\LoggerInterface;

class AuthorService {
    private LoggerInterface $logger;
    private string $appName;

    public function __construct(LoggerInterface $logger, string $appName){
        $this->logger = $logger;
        $this->appName = $appName;
    }

    public function log($message) {
        $this->logger->error($message, ['extra_context' => 'my extra context']);
    }
}

In cases where you can not inject a logger into a class, you can use the \OCP\Log\logger function to acquire a logger instance. As a first argument you need to pass the app ID.

<?php

use function OCP\Log\logger;

logger('calendar')->warning('look, no dependency injection');

Admin audit logging

If you want to log things less for system administration but for compliance reasons, e.g. who accessed which file, who changed the password of an item or made it public, the admin audit log is the correct place.

You can easily add a log by simply emitting an OCP\Log\Audit\CriticalActionPerformedEvent event:

<?php

$dispatcher = \OCP\Server::get(\OCP\EventDispatcher\IEventDispatcher::class);

$event = new \OCP\Log\Audit\CriticalActionPerformedEvent(
    'My critical action for app %s',
    ['name' => 'My App ID']
);
$dispatcher->dispatchTyped($event);