python/mach/mach/test/test_logger.py
author Mozilla Releng Treescript <release+treescript@mozilla.org>
Fri, 27 May 2022 06:58:24 +0000
changeset 619020 ab65238291222763a2dd2f0ffc3c539d37a940f6
parent 554551 994ae8e4833c90447d91f0e26a718573cff5a514
permissions -rw-r--r--
no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD be -> 040b93d8fc0266fdc8696c86290152480570a8a8 eo -> 2aeb97991166e6e5d3514662ea7012744f724732 es-AR -> 3f2e985b37848b8d19ac9d01aa9ecd76895547bb it -> 12f68a74e363beee234126dcf25407f882d085ff

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from __future__ import absolute_import, unicode_literals

import logging
import time
import unittest

from mach.logging import StructuredHumanFormatter

from mozunit import main


class DummyLogger(logging.Logger):
    def __init__(self, cb):
        logging.Logger.__init__(self, "test")

        self._cb = cb

    def handle(self, record):
        self._cb(record)


class TestStructuredHumanFormatter(unittest.TestCase):
    def test_non_ascii_logging(self):
        # Ensures the formatter doesn't choke when non-ASCII characters are
        # present in printed parameters.
        formatter = StructuredHumanFormatter(time.time())

        def on_record(record):
            result = formatter.format(record)
            relevant = result[9:]

            self.assertEqual(relevant, "Test: s\xe9curit\xe9")

        logger = DummyLogger(on_record)

        value = "s\xe9curit\xe9"

        logger.log(
            logging.INFO,
            "Test: {utf}",
            extra={"action": "action", "params": {"utf": value}},
        )


if __name__ == "__main__":
    main()