labgrid icon indicating copy to clipboard operation
labgrid copied to clipboard

logging: Stringify message in StepFormatter

Open cidlik opened this issue 11 months ago • 2 comments

Description The usage of StepFormatter could provide errors:

self = <labgrid.logging.StepFormatter object at 0x7f63fdc59010>
record = <LogRecord: foo, 30, .../test_logger.py, 10, "{1: 2}">

    def format(self, record):
        old_msg = record.msg
        try:
            if self.indent:
                if hasattr(record, "indent_level"):
                    self.indent_level = record.indent_level

>               record.msg = (" " * self.indent_level) + record.msg
E               TypeError: can only concatenate str (not "dict") to str

if a message isn't a string.

Reproduction The test file test_logger.py:

import logging

logger = logging.getLogger("foo")

def test_foo():
    d = {1: 2}
    logger.debug(d)
    logger.info(d)
    logger.warning(d)
    logger.error(d)

I used clean upstream labgrid package (HEAD SHA c609d2d7a88016f4610f396a22cd29a676180045). Just in case I provide my env:

Virtual environment
ansicolors==1.1.8
attrs==25.1.0
certifi==2025.1.31
charset-normalizer==3.4.1
grpcio==1.70.0
grpcio-reflection==1.70.0
idna==3.10
iniconfig==2.0.0
Jinja2==3.1.5
labgrid @ file:///home/rkuznecov/3dpty/labgrid
MarkupSafe==3.0.2
packaging==24.2
pexpect==4.9.0
pluggy==1.5.0
protobuf==5.29.3
ptyprocess==0.7.0
pyserial-labgrid==3.5.0.2
pytest==8.3.4
pyudev==0.24.3
pyusb==1.3.1
PyYAML==6.0.2
requests==2.32.3
urllib3==2.3.0
xmodem==0.4.7

Run:

$ pytest test_logger.py
...
self = <labgrid.logging.StepFormatter object at 0x7f377f713ce0>
record = <LogRecord: foo, 30, /home/rkuznecov/3dpty/labgrid-tests/test_logger.py, 10, "{1: 2}">

    def format(self, record):
        old_msg = record.msg
        try:
            if self.indent:
                if hasattr(record, "indent_level"):
                    self.indent_level = record.indent_level

>               record.msg = (" " * self.indent_level) + record.msg
E               TypeError: can only concatenate str (not "dict") to str

venv/lib/python3.12/site-packages/labgrid/logging.py:40: TypeError
================================== short test summary info ===================================FAILED test_logger.py::test_foo - TypeError: can only concatenate str (not "dict") to str     
===================================== 1 failed in 0.09s =====================================

Checklist

  • [ ] Documentation for the feature
  • [ ] Tests for the feature
  • [ ] The arguments and description in doc/configuration.rst have been updated
  • [ ] Add a section on how to use the feature to doc/usage.rst
  • [ ] Add a section on how to use the feature to doc/development.rst
  • [x] PR has been tested
  • [ ] Man pages have been regenerated

cidlik avatar Feb 02 '25 09:02 cidlik

Thanks for the PR, this is missing a DCO, you can add this with git commit --amend -s and doing a force push to the branch.

Emantor avatar Feb 04 '25 07:02 Emantor

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 55.8%. Comparing base (6d8febb) to head (90491b9). :warning: Report is 121 commits behind head on master.

Additional details and impacted files
@@          Coverage Diff           @@
##           master   #1593   +/-   ##
======================================
  Coverage    55.8%   55.8%           
======================================
  Files         170     170           
  Lines       13388   13388           
======================================
  Hits         7472    7472           
  Misses       5916    5916           
Flag Coverage Δ
3.10 55.8% <100.0%> (ø)
3.11 55.8% <100.0%> (ø)
3.12 55.8% <100.0%> (ø)
3.13 55.7% <100.0%> (ø)
3.9 55.8% <100.0%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Feb 04 '25 07:02 codecov[bot]