Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x]: console command logs below warning are never logged #14280

Closed
julianschelker opened this issue Feb 2, 2024 · 4 comments · Fixed by #14434
Closed

[4.x]: console command logs below warning are never logged #14280

julianschelker opened this issue Feb 2, 2024 · 4 comments · Fixed by #14434
Assignees

Comments

@julianschelker
Copy link

What happened?

Description

Logging in console commands with level below warning is never logged. For some reason it was actively disabled, but now it can't be configured with log level anymore. This is confusing at least and doens't make much sense in my opinion.

Steps to reproduce

  1. Place a logging info statement inside a migration or other console command
Craft::info('some information about the migration', 'Migration');
  1. Set logging config to log info level
    My app.php configures:
'components' => [
'log' => [
	'monologTargetConfig' => [
		'logContext' => false,
		'level' => App::devMode() ? LogLevel::INFO : LogLevel::WARNING
	],
],
],

Expected behavior

Log is shown

Actual behavior

Log doesn't show up. The class MonologTarget:191 does not add a StreamHandler with correct logLevel incase the request is a console request. See:

$logger->pushHandler((new StreamHandler(
    'php://stderr',
    Logger::WARNING,
    bubble: false,
))->setFormatter($this->formatter));

// Don't pollute console request output
if (!Craft::$app->getRequest()->getIsConsoleRequest()) {      // <-- Why?
    $logger->pushHandler((new StreamHandler(
        'php://stdout',
        $this->level,
        bubble: false,
    ))->setFormatter($this->formatter));
}

Craft CMS version

Craft Pro 4.5.11.1

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

"codemix/yii2-streamlog": "1.3.1",
"codemonauts/craft-readonly-field": "2.0.1",
"craftcms/cms": "4.5.11.1",
"craftcms/element-api": "^3.0.1.1",
"craftcms/google-cloud": "^2.0.0",
"craftcms/redactor": "^3.0.4",
"dompdf/dompdf": "^2.0.3",
"doublesecretagency/craft-cpcss": "2.6.0",
"ether/tags": "2.0.0",
"firebase/php-jwt": "5.2.0",
"google/cloud-firestore": "^1.28",
"kreait/firebase-php": "5.26.3",
"mailjet/mailjet-apiv3-php": "^1.5",
"postfinancecheckout/sdk": "^3.0",
"promphp/prometheus_client_php": "^2.2",
"vlucas/phpdotenv": "^5.4.0"

@timkelty
Copy link
Contributor

@julianschelker The reasoning here is if you're running a console command and streaming logs to stderr/out, you'll have a mess of logs and actual command output (assuming you're running in devMode or with an explicit level).

That said, I can see how that would be confusing when expecting your own logs to show. I'm inclined to just take that condition out and just say the extra output is expected when running in devMode.

@julianschelker
Copy link
Author

@timkelty thanks, that would be great. I think if running a command shows too much logs then it's still possible for the user to just change the loglevel to get a cleaner output.

What i'm generally a bit missing in the framework is a way to configure logLevel per classes or package like Java/Spring Backends can do this. That also helps really good to get a better overview of logs. But that's rather high-level and probably out of scope of a simple ticket, but maybe this could be considered somewhere in the future.
Thanks!

@timkelty
Copy link
Contributor

@julianschelker also be aware that devMode will toggle the log level to info for you: https://craftcms.com/knowledge-base/what-dev-mode-does

My PR proposes additionally proposes to not log context by default for console commands, so it looks like if you set CRAFT_DEV_MODE=1, it would behave how you're hoping.

configure logLevel per classes or package like Java/Spring Backends can do this. That also helps really good to get a better overview of logs

That's absolutely possible! In Yii, that's done with additional log targets, and customizing the target's categories. Craft's docs show some examples of how to do this. Essentially you'd do something like this:

  • Add a new target in components.log.targets in your config/app.php file.
  • Set the categories property on the new target to something like ['foo\bar\Baz:*']
  • Optionally, if you want to omit those logs to the default targets, use monologTargetConfig.except
  • Log things as Craft::info('my log', __METHOD__) (2nd arg is the category)

brandonkelly added a commit that referenced this issue Mar 18, 2024
[ci skip]
timkelty pushed a commit that referenced this issue Mar 18, 2024
[ci skip]
@brandonkelly
Copy link
Member

Craft 4.9 and 5.1 are out with the changes in #14434.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants