loguru icon indicating copy to clipboard operation
loguru copied to clipboard

Windows allure report error

Open dongfanger opened this issue 4 years ago • 6 comments

--- Logging error in Loguru Handler #0 --- Record was: {'elapsed': datetime.timedelta(microseconds=519611), 'exception': None, 'extra': {}, 'file': (name='login_test.py', path='C:\Users\Desktop\test\tepdemo\tepdemo070\tests\sample\login_test.py'), 'function': 'test_login', 'level': (name='INFO', no=20, icon='ℹ️'), 'line': 8, 'message': 'login.token', 'module': 'login_test', 'name': 'login_test', 'process': (id=14140, name='MainProcess'), 'thread': (id=16052, name='MainThread'), 'time': datetime(2021, 5, 27, 10, 31, 52, 693888, tzinfo=datetime.timezone(datetime.timedelta(seconds=28800), '中国标准时间'))} Traceback (most recent call last): File "d:\program files (x86)\python38-32\lib\site-packages\loguru_handler.py", line 176, in emit self._sink.write(str_record) File "d:\program files (x86)\python38-32\lib\site-packages\loguru_simple_sinks.py", line 26, in write self._stream.write(message) File "C:\Users\class100\AppData\Roaming\Python\Python38\site-packages\colorama\ansitowin32.py", line 41, in write self.__convertor.write(text) File "C:\Users\class100\AppData\Roaming\Python\Python38\site-packages\colorama\ansitowin32.py", line 162, in write self.write_and_convert(text) File "C:\Users\class100\AppData\Roaming\Python\Python38\site-packages\colorama\ansitowin32.py", line 187, in write_and_convert self.write_plain_text(text, cursor, start) File "C:\Users\class100\AppData\Roaming\Python\Python38\site-packages\colorama\ansitowin32.py", line 196, in write_plain_text self.wrapped.flush() OSError: [WinError 6] 句柄无效。 --- End of logging error ---

dongfanger avatar May 27 '21 02:05 dongfanger

How is configured the logger? What are the parameters and sinks passed to logger.add()?

You can specify colorize=False and I guess it will fix your issue. As it is, it's hard for me to guess where it came from.

Delgan avatar May 27 '21 06:05 Delgan

Thanks for the reply, I didn't have a configuration, my test code is like this:

from loguru import logger


def test():
    logger.info("hello")

After I ran the command pytest main_test.py --alluredir=allure and allure serve allure, the HTML report showed a error: image

dongfanger avatar May 27 '21 08:05 dongfanger

Thanks for the details. It seems that colarama and by extension loguru detects stderr as supporting the Windows API for colors, while it is not the case. It seems to be a false positive.

In what environment do you run your script? Is it in a Cloud service or on your personal computer? Are you using any specific IDE? Is there anything special about the Windows version you're using? Any information could be useful to know why the detection fails.

Also, can you please tell me whether or not the following code also generates an exception?

from colorama import init
init(autoreset=True)
print('\033[31m' + 'some red text')

As a workaround for now, you can remove() the default sink and re-add() it with colorize=False.

import sys

logger.remove()
logger.add(sys.stderr, colorize=False)

Delgan avatar May 27 '21 18:05 Delgan

The colorama test code is ok, and there is no error after I ran the same command. The remove() function worked, the error disappeared, amazing! My environment:

  • Personal computer
  • PyCharm
  • Windows 10

dongfanger avatar May 28 '21 00:05 dongfanger

Thanks for the details. I would like to fix that, but unfortunately I'm unable to reproduce the error.

I no longer think this is an false positive of Windows API detection in Loguru. The issue seems related to these ones:

  • https://github.com/pytest-dev/pytest/issues/8257
  • Issue30555

I don't want to abuse your time, but do you see any error if you simply execute the following script without pytest nor allure?

from loguru import logger
logger.info("hello")

Maybe @sunyb3 do you have any idea?

Delgan avatar May 28 '21 09:05 Delgan

Thanks for the reply. There is no error if I simply execute the script without pytest nor allure. Thanks again for the related issues.

dongfanger avatar May 31 '21 02:05 dongfanger