craftcms / cms

Build bespoke content experiences with Craft.
https://craftcms.com
Other
3.27k stars 635 forks source link

[4.x]/[5.x]: `Craft::debug()` no longer prints to logs #15435

Closed seb-enovate closed 3 months ago

seb-enovate commented 3 months ago

What happened?

Description

We have an internal plugin that we use for many of our Craft CMS projects. It uses Craft::debug() to write debug logs in the dev environment. However, I've noticed that this no longer works in projects running Craft 4 or later.

Steps to reproduce

  1. Set up a fresh Craft CMS 5 install
  2. Use the php craft make module command to setup a test module
  3. Add a Craft::debug() line to the init() method of the module
  4. Run tail -f storage/logs/* | grep 'web.DEBUG' to watch the logs in the terminal
  5. Reload the index page in the browser and observer that no debug log entry is written

Expected behavior

debug entries are written to the log.

Actual behavior

No debug entries are written to the log.

Craft CMS version

4+

PHP version

No response

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

No response

timkelty commented 3 months ago

@seb-enovate Since Craft 4, when Craft started using Monolog for its logging, the default minimum level was changed to info when dev mode is enabled. You're right that in Craft 3, it was trace, aka debug.

To print Craft::debug() logs, you can change the default logging level in config/app.php like so:

<?php
use Psr\Log\LogLevel;

return [
    'components' => [
        'log' => [
            'monologTargetConfig' => [
                'level' => LogLevel::DEBUG,
            ],
        ],
    ],
];

Alternatively, if this is for a dedicated module/plugin, you can set up a dedicated log target with whatever level you specify. Examples of both of these configurations can be found in the docs.

seb-enovate commented 3 months ago

Thanks Tim. I'm just wondering if the docs ought to be tweaked slightly. For example in this section of the docs:

image

It states the convenience methods, which includes trace and debug, and then says:

By default, Craft logs all levels when devMode is enabled. Otherwise, anything lower than warning will be ignored.

To me, "Craft logs all levels" suggests that trace and debug would work, even though as you've explained this is intentionally not the case?

timkelty commented 3 months ago

Indeed, that is misleading, and likely leftover from when that was the case. We'll get it updated!: https://github.com/craftcms/docs/pull/639