[4.x] How to register additional log targets files? #11079
-
I'm trying to figure out how to register additional log files (targets) in Craft 4. I'm writing a custom module for a site and I just want to be able to log errors specific to that module in a dedicated log file ( Craft::getLogger()->dispatcher->targets[] = new FileTarget([
'logFile' => App::parseEnv('@logs') . '/custom-module.log',
'categories' => ['custom-module']
]);
// used like this
Craft::getLogger()->log($message, Logger::LEVEL_INFO, 'custom-module'); The upgrade guide only mentions that custom log targets should be added via // config/app.php
return [
'components' => [
'log' => [
'targets' => [
'custom-module' => [
'class' => MonologTarget::class,
'name' => 'custom-module',
'enabled' => false,
'extractExceptionTrace' => !App::devMode(),
'allowLineBreaks' => App::devMode(),
'level' => App::devMode() ? LogLevel::INFO : LogLevel::WARNING,
'categories' => 'custom-module',
],
],
],
],
]; It's not causing any errors but isn't working either, everything I try to log just ends up in the On a broader note, is there any documentation on how to use the new logging component? Should I even use |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
We’re working on documentation for custom log targets that should be out shortly—I’ll follow up once they’re live! |
Beta Was this translation helpful? Give feedback.
-
@MoritzLost While we are working up updated the docs for this, here's some quick and dirty examples: https://gist.github.com/timkelty/c0530983c3fb450c15a5f136fb388d3d For you, it would look something like this:
<?php
use craft\helpers\App;
use Monolog\Formatter\LineFormatter;
use Psr\Log\LogLevel;
use yii\i18n\PhpMessageSource;
use yii\web\HttpException;
return [
'components' => [
'log' => [
'monologTargetConfig' => [
'except' => [
PhpMessageSource::class . ':*',
HttpException::class . ':40*',
'custom-module'
],
],
'targets' => [
// Add a traditional Yii file target
[
'class' => \yii\log\FileTarget::class,
'logFile' => '@storage/logs/custom-module.log',
'levels' => App::devMode() ? ['info', 'warning', 'error'] : ['warning', 'error'],
'categories' => ['custom-module'],
],
// Add a Monolog target
[
'class' => \craft\log\MonologTarget::class,
'name' => 'custom-module',
'extractExceptionTrace' => !App::devMode(),
'allowLineBreaks' => App::devMode(),
'level' => App::devMode() ? LogLevel::INFO : LogLevel::WARNING,
'categories' => ['custom-module'],
],
]
],
],
];
Basic usage hasn't changed. Alternatively, you can now pass a |
Beta Was this translation helpful? Give feedback.
-
There is a (relatively) new Logging page in the documentation, with a section dedicated to adding targets! |
Beta Was this translation helpful? Give feedback.
@MoritzLost While we are working up updated the docs for this, here's some quick and dirty examples: https://gist.github.com/timkelty/c0530983c3fb450c15a5f136fb388d3d
For you, it would look something like this:
monologTargetConfig
to filter out anycustom-module
messages.custom-module
.