[GH-ISSUE #1922] Override the source attribute in v3? #825

Closed
opened 2026-03-04 03:01:03 +03:00 by kerem · 4 comments
Owner

Originally created by @hcker2000 on GitHub (Nov 6, 2024).
Original GitHub issue: https://github.com/Seldaek/monolog/issues/1922

Monolog v3

We used to be able to use a source processor to override the source and host options but can no longer do this. This was required as the default source and host options inside docker are useless for logging across multiple servers as they all have the same ip and host name.

Here is the old processor code:

class SourceProcessor implements ProcessorInterface
{
    public function __invoke(array $record): array
    {
        $record['source'] = 'ops.' . $_ENV['ENVIRONMENT'];
        $record['host'] = 'ops.' . $_ENV['ENVIRONMENT'];

        return $record;
    }
}

How can we do this same thing with v3?

Originally created by @hcker2000 on GitHub (Nov 6, 2024). Original GitHub issue: https://github.com/Seldaek/monolog/issues/1922 Monolog v3 We used to be able to use a source processor to override the source and host options but can no longer do this. This was required as the default source and host options inside docker are useless for logging across multiple servers as they all have the same ip and host name. Here is the old processor code: ```php class SourceProcessor implements ProcessorInterface { public function __invoke(array $record): array { $record['source'] = 'ops.' . $_ENV['ENVIRONMENT']; $record['host'] = 'ops.' . $_ENV['ENVIRONMENT']; return $record; } } ``` **How can we do this same thing with v3?**
kerem 2026-03-04 03:01:03 +03:00
  • closed this issue
  • added the
    Support
    label
Author
Owner

@stof commented on GitHub (Nov 6, 2024):

which source and host options are you talking about ? there is no such options in Monolog.

<!-- gh-comment-id:2460269198 --> @stof commented on GitHub (Nov 6, 2024): which source and host options are you talking about ? there is no such options in Monolog.
Author
Owner

@hcker2000 commented on GitHub (Nov 6, 2024):

All I know is that the code above works with 2.7.0 and will not work with the latest changes. When I switch the $record parameter to have to be an instance of the \Monolog\LogRecord class you can no longer set a source and host that gets json encoded at the top level of the object like {source: "mythinghere", host:"myhosthere"}

My guess is that maybe the were not part of monolog but that inside that method it would allow me to add them to all my requests in a single place.

I need to be able to add those as properties at the top level of the object thats sent.

Is there any way to continue to do this?

<!-- gh-comment-id:2460387954 --> @hcker2000 commented on GitHub (Nov 6, 2024): All I know is that the code above works with 2.7.0 and will not work with the latest changes. When I switch the $record parameter to have to be an instance of the ```\Monolog\LogRecord``` class you can no longer set a source and host that gets json encoded at the top level of the object like ```{source: "mythinghere", host:"myhosthere"}``` My guess is that maybe the were not part of monolog but that inside that method it would allow me to add them to all my requests in a single place. I need to be able to add those as properties at the top level of the object thats sent. Is there any way to continue to do this?
Author
Owner

@Seldaek commented on GitHub (Nov 9, 2024):

You'd need to write those in context or ideally extra data from the Processor, then use a custom formatter that moves the extra data into top level keys (or even just use a formatter to set the keys and skip the processor entirely).

For v3 since records are objects you cannot write arbitrary data on them.

<!-- gh-comment-id:2466211815 --> @Seldaek commented on GitHub (Nov 9, 2024): You'd need to write those in context or ideally extra data from the Processor, then use a custom formatter that moves the extra data into top level keys (or even just use a formatter to set the keys and skip the processor entirely). For v3 since records are objects you cannot write arbitrary data on them.
Author
Owner

@hcker2000 commented on GitHub (Nov 12, 2024):

Thanks the custom formatter does the trick.

<?php

namespace App\Monolog\Formatter;
 
use Monolog\Formatter\FormatterInterface;
use Monolog\LogRecord;
use stdClass;

class JsonFormatter implements FormatterInterface
{
    public function format(LogRecord $record)
    {
        $output = new stdClass;
        $output->source = 'subdomainhere.' . $_ENV['ENVIRONMENT'];
        $output->host = 'subdomainhere.' . $_ENV['ENVIRONMENT'];
        $output->context = $record['context'];
        $output->message = $record['message'];
        $output->timestamp = date(DATE_ISO8601);

        return json_encode($output).PHP_EOL;
    }
 
    public function formatBatch(array $records)
    {
        foreach ($records as $key => $record) {
            $records[$key] = $this->format($record);
        }
 
        return $records;
    }
}
<!-- gh-comment-id:2469356926 --> @hcker2000 commented on GitHub (Nov 12, 2024): Thanks the custom formatter does the trick. ```php <?php namespace App\Monolog\Formatter; use Monolog\Formatter\FormatterInterface; use Monolog\LogRecord; use stdClass; class JsonFormatter implements FormatterInterface { public function format(LogRecord $record) { $output = new stdClass; $output->source = 'subdomainhere.' . $_ENV['ENVIRONMENT']; $output->host = 'subdomainhere.' . $_ENV['ENVIRONMENT']; $output->context = $record['context']; $output->message = $record['message']; $output->timestamp = date(DATE_ISO8601); return json_encode($output).PHP_EOL; } public function formatBatch(array $records) { foreach ($records as $key => $record) { $records[$key] = $this->format($record); } return $records; } }
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/monolog#825
No description provided.