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

_format_msg_json doesn't merge arguments #170

Open
phdesign opened this issue Nov 3, 2020 · 2 comments
Open

_format_msg_json doesn't merge arguments #170

phdesign opened this issue Nov 3, 2020 · 2 comments

Comments

@phdesign
Copy link

phdesign commented Nov 3, 2020

I'm using the FluentHandler class for Python logging module. I was hoping to pass a json string to the logger and have it interpreted as json, which it does, but the FluentRecordFormatter doesn't merge logged arguments in.

Example

logger = logging.getLogger('fluent.test')
logger.info('{"from": "%(a)s", "to": "%(b)s"}', {"a":"userA","b":"userB"})

Expected result

{"type":"INFO","from": "userA", "to": "userB"}

Actual result

{"type":"INFO","from":"%(a)s","to":"%(b)s"}

I expected the arguments to be merged in based on the behaviour of the default Python formatter which calls record.getMessage which merges in arguments

if self.args:
    msg = msg % self.args

However, the behaviour of the FluentRecordFormatter is to json.loads(str(msg)) the message, and ignore any arguments.

Would it be suitable to merge in the arguments when loading the json? I'd be happy to contribute a PR for it. Here's a test that would cover it.

    def test_json_encoded_message_with_args(self):
        handler = fluent.handler.FluentHandler('app.follow', port=self._port)

        with handler:
            logging.basicConfig(level=logging.INFO)
            log = logging.getLogger('fluent.test')
            handler.setFormatter(fluent.handler.FluentRecordFormatter())
            log.addHandler(handler)

            log.info('{"key": "%(a)s", "param": "value"}', {"a": "hello world!"})

            log.removeHandler(handler)

        data = self.get_data()
        self.assertTrue('key' in data[0][2])
        self.assertEqual('hello world!', data[0][2]['key'])
@phdesign
Copy link
Author

Adding some more context to this. In the call to format(), the code does call the underlying implementation of record.getMessage

    def format(self, record):
        # Compute attributes handled by parent class.
        super(FluentRecordFormatter, self).format(record)  # <------ record.messge = record.msg % record.args
        # Add ours
        record.hostname = self.hostname

        # Apply format
        data = self._formatter(record)

        self._structuring(data, record)
        return data

which does the merge, however the call to self._structuring() eventually loads the json from the msg property, not the message property which is the merged value. e.g.

json_msg = json.loads(str(msg))

@arcivanov
Copy link
Member

Sorry, I've been busy, I'll take a look soon once I get a break.

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

No branches or pull requests

2 participants