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

logging: Stringify message in StepFormatter #1593

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

cidlik
Copy link

@cidlik cidlik commented Feb 2, 2025

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 c609d2d). 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
  • PR has been tested
  • Man pages have been regenerated

@Emantor
Copy link
Member

Emantor commented Feb 4, 2025

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.

Copy link

codecov bot commented Feb 4, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 55.8%. Comparing base (02fa1ef) to head (60424d0).
Report is 2 commits behind head on master.

✅ All tests successful. No failed tests found.

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

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@cidlik cidlik force-pushed the step-formatter-issue branch from 6449c4d to 35a2bdb Compare February 4, 2025 18:28
@cidlik cidlik requested a review from Emantor February 6, 2025 07:07
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.

Change-Id: I0f5187dcd9389ff486aac2f7e0e029ecaa323825
Signed-off-by: Roman Kuznecov <[email protected]>
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

Successfully merging this pull request may close these issues.

2 participants