-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
Trying to create custom monolog log channel Laravel 5.6 #25170
Comments
I believe here we see the problem: a string is passed which looks like from the default line formatter, but I don't see a formatter in your configuration, please see if this works: 'formatter' => \Monolog\Formatter\MongoDBFormatter::class, |
@mfn thank you! Didn't know that formatter is required. I'm done with clearing config cache. But now I'm trying to log something and I get
|
|
It seems that
The referenced class OTOH It seems that For you to move forward, my advice is to simply copy that I found your Monolog issue that you created about this problem, so maybe you can figure out what's with the formatter and maybe contribute back a version working with both clients. |
Oh, I just found out: Monologs master has an updated version: https://github.com/Seldaek/monolog/blob/master/src/Monolog/Formatter/MongoDBFormatter.php But it's a different version in the latest release (1.23.0). Monologs last release was >1 year ago and a lot has happened since then. I guess the 1.x version is maintenance only (it contains the version you're using, see https://github.com/Seldaek/monolog/blob/1.x/src/Monolog/Formatter/MongoDBFormatter.php ) and at somepoint there will probably a 2.x release with the version you need. In this case: see if you can directly copy over that version into your project and work with that. |
@mfn thank you a lot!
And it worked! |
Maybe it's reasonable to have newer version inside https://github.com/laravel/framework/blob/5.6/composer.json#L25 |
@Tarasovych there is no newer release of Monolog with what you need, so there's nothing to do for Laravel. Please close the issue. |
I got one more error connected with this. So my 'mongo' => [
'driver' => 'monolog',
'handler' => \Monolog\Handler\MongoDBHandler::class,
'handler_with' => [
'mongo' => new MongoDB\Client(), // MongoDB\Client()::class doesn't work
'database' => 'logs',
'collection' => 'prod'
],
'formatter' => App\MongoDBFormatter::class
] I can't execute
Exception trace:
1 Error::("Call to undefined method MongoDB\Driver\Manager::__set_state()")
...\bootstrap\cache\config.php:445
2 require()
...\vendor\laravel\framework\src\Illuminate\Foundation\Console\ConfigCacheCommand.php:64 I can run P. S. I've also tried 'mongo' => [
'manager' => MongoDB\Driver\Manager::class,
'uri' => 'mongodb://localhost:27017',
'typeMap' => [
'array' => MongoDB\Model\BSONArray::class,
'document' => MongoDB\Model\BSONDocument::class,
'root' => MongoDB\Model\BSONDocument::class
],
'writeConcern' => MongoDB\Driver\WriteConcern::class
], without any success. I understand why I got "not serializable", but that config works... What to do now? |
Instead of
did you try
(no parenthesis) |
@mfn IDE doesn't allow me to do that)
|
Sorry, typo: 'mongo' => MongoDB\Client::class, (no |
@mfn I tried that, doesn't work. P. S. I meant, there is no problem with config serialization, but nothing logs into mongo. |
I've also tried to call different methods from |
I think I now understand the issue better. You're creating The problem is: the arguments are passed literally, that's why the first argument, which is only class string reference to I think what you want to achieve is not possible directly with the What you need is more flexibility and therefore I suggest you create a 'mongo' => [
'driver' => 'custom',
'via' => YourOwnMongodbAdapter::class,
'name' => 'default',
'database' => 'logs',
'collection' => 'test',
], (see also https://laravel.com/docs/5.6/logging#creating-channels-via-factories ) Then <?php
use Monolog\Handler\MongoDBHandler;
class YourOwnMongodbAdapter
{
public function __invoke(array $config)
{
$handler = new MongoDBHandler(
new MongoDB\Client(),
$config['database'],
$config['collection']
);
$handler->setFormatter(new \App\MongoDBFormatter);
return new Monolog\Logger(
$config['name'],
[$handler]
);
}
} |
@mfn thanks!
Also I get 1970 year using UTCDateTime class for formatting data, but this is another question... |
So I went from protected function formatDate(\DateTime $value, $nestingLevel)
{
return new UTCDateTime($value->getTimestamp());
} to protected function formatDate(\DateTime $value, $nestingLevel)
{
return $value;
} and I have correct date. |
you can also try this : https://github.com/Amirhb/laravel-mongodb-log |
You can register the mongo client in service provider and live the $mongodb value empty:
// AppServiceProvider.php
// register function
$this->app->when(\Monolog\Handler\MongoDBHandler::class)
->needs('$mongodb')
->give(app(\MongoDB\Client::class)); // logging.php
'mongo' => [
'driver' => 'monolog',
'handler' => \Monolog\Handler\MongoDBHandler::class,
'formatter' =>\Monolog\Formatter\MongoDBFormatter::class,
'with' => [
'database' => 'logs',
// 'mongo' => new MongoDB\Client(), <-- Remove this key
'collection' => 'prod',
],
] |
Description:
My
config/logging.php
:My
.env
:LOG_CHANNEL=stack
.I'm sure that MongoDB database
logs
exists,test
collection too.I'm trying to execute
php artisan config:cache
, I get an error:What's wrong? I guess, my config was amde accordingly to documentation.
What I've tried so far:
same error.
The text was updated successfully, but these errors were encountered: