# How do I write to the log?

**URL:** <https://help.nextcloud.com/t/how-do-i-write-to-the-log/131024>\
**Category:** 💻 Development\
**Created:** [January 13, 2022, 1:29am UTC](https://help.nextcloud.com/t/how-do-i-write-to-the-log/131024 "2022-01-13T01:29:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![robbie](https://help.nextcloud.com/user_avatar/help.nextcloud.com/robbie/32/9608_2.png) [@robbie](https://help.nextcloud.com/u/robbie)\
**Post date:** [January 13, 2022, 1:29am UTC](https://help.nextcloud.com/t/how-do-i-write-to-the-log/131024/1 "2022-01-13T01:29:40Z")

</div>

I feel stupid for even asking, but how can I get my API code to write debug logs? The docs here are remarkably obtuse.

[https://docs.nextcloud.com/server/latest/developer\_manual/basics/logging.html?highlight=logging](https://docs.nextcloud.com/server/latest/developer_manual/basics/logging.html?highlight=logging)

---

<div class="post-metadata">

**Author:** ![gas85](https://help.nextcloud.com/user_avatar/help.nextcloud.com/gas85/32/7772_2.png) [@gas85](https://help.nextcloud.com/u/gas85)\
**Post date:** [January 13, 2022, 7:46am UTC](https://help.nextcloud.com/t/how-do-i-write-to-the-log/131024/2 "2022-01-13T07:46:35Z")

</div>

Yeah, a bit of reverse engineering needed. Basically it is JSON, but with few mandatory fields, e.g. example from [preview generator](https://help.nextcloud.com/t/how-to-know-if-preview-generator-is-working/41072/6):

> <https://github.com/GAS85/nextcloud_scripts/blob/master/nextcloud-preview.sh#L98>

Where

- `reqid` is some random unique string for the transaction, e.g. `reqId=$(< /dev/urandom tr -dc A-Za-z0-9 | head -c20)`
- `user` is user, or none if script executed globally
- `app` is an application name that works
- `url` basically is the message body and here you can put whatever output you need
- `level` in your case could be `debug`
- `time` is a timestamp in standard `+%Y-%m-%dT%H:%M:%S%:z` or customized in `config.php` format.

UPDATE:

Sorry, I see that you asked from the PHP api…

---

<div class="post-metadata">

**Author:** ![robbie](https://help.nextcloud.com/user_avatar/help.nextcloud.com/robbie/32/9608_2.png) [@robbie](https://help.nextcloud.com/u/robbie)\
**Post date:** [January 13, 2022, 2:41pm UTC](https://help.nextcloud.com/t/how-do-i-write-to-the-log/131024/3 "2022-01-13T14:41:37Z")

</div>

Hah yes, in the PHP code. I was trying to add some debugging to the REST API controller, but can’t figure out how to get access to the `$logger`. I made do with throwing an exception, but that’s going to make debugging harder.
