添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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

Hi all,
I'm trying to understand which is the problem in the following line:

--- Logging error in Loguru Handler #2 ---
Record was: {'elapsed': datetime.timedelta(microseconds=151157), 'exception': None, 'extra': {'common_to_all': 'default', 'some_value': 42}, 'file': 'postgres_wrapper.py', 'function': 'scrivi_questo', 'level': 'INFO', 'line': 65, 'message': 'SCRIVI QUESTO', 'module': 'postgres_wrapper', 'name': 'postgres_wrapper', 'process': '12014', 'thread': '140287974258432', 'time': datetime(2019, 12, 4, 8, 29, 49, 157895, tzinfo=datetime.timezone(datetime.timedelta(seconds=3600), 'CET'))}
Traceback (most recent call last):
File "/home/pigio/.pyenv/versions/3.7.2/envs/virtual-env-for-test-3.7.2/lib/python3.7/site-packages/loguru/_handler.py", line 144, in emit
self._writer(str_record)
File "/home/pigio/.pyenv/versions/3.7.2/envs/virtual-env-for-test-3.7.2/lib/python3.7/site-packages/loguru/_logger.py", line 696, in writer
write(m)
ValueError: I/O operation on closed file
--- End of logging error ---

Any suggestion? I don't fully understand the concept behind logger, handler and so on.

After upgrading from loguru-0.3.2 to 0.4.0 the error disappear and now the logs are not sent visible to stdout.

So the problem persist but no error is rised. I come back to 0.3.2

## Set class version ROBOT_LIBRARY_VERSION = '1.0' ## Create clear method given that this class is global ROBOT_LIBRARY_SCOPE = 'GLOBAL' def __init__(self, fn_caller=None): fmt = "<level>{level: <8}</> | {time:HH:mm:ss} | {module: <25} | {function: <25} | {line: <4} | {message}" logger.remove() logger.configure( handlers=[ dict(sink=sys.stderr, colorize=True, backtrace=True, diagnose=True, format=fmt, level="DEBUG") #dict(sink="file.log", enqueue=True, serialize=True), levels=[dict(name="CTM_DBG", no=33, color="<blue>")], extra={"common_to_all": "default"}, patch=lambda record: record["extra"].update(some_value=42)

i'm setting the logger.configure inside an init method of a class. After that I import loguru in other file using "from loguru import logger" and i think that the problem is related to this thing.
Something that regards visibility?

@Delgan file.log is under comment, there is the #.
no multiprocessing, but only the logger configured in the init method of a python class. After that i call loguru in another file, without import that class.
So file1:
Contains LoggerManager and the __ init __ method.
File 2:
I have only "from loguru import logger"
I have not understood how the logger configuration is passed between files at the moment (if it happens because i use the from loguru import logger or for some other reason)

I have not understood how the logger configuration is passed between files at the moment (if it happens because i use the from loguru import logger or for some other reason)

The logger is the same instance across your modules. So, whenever you call .add() or .configure() in one module, this also affects the other modules, as the logger object is the same. If you use a logger while your LoggerManager() is not initialized, it will just print to the default sink (which is sys.stderr ). Maybe you should try to remove enqueue=True for now and see if it fixes the problem.

but how the logger object is passed across them? through the import?
This row is not used: #dict(sink="file.log", enqueue=True, serialize=True),
There is the # at the beginning.

but how the logger object is passed across them? through the import?

Yes. When you from loguru import logger , your module get access to the Logger object and hence all the attached handlers. There is no need to explicitly pass it around, the import should suffice.

This row is not used: #dict(sink="file.log", enqueue=True, serialize=True),
There is the # at the beginning.

Yes, sorry. So, the problem comes from the sys.stderr sink. I think it's closed somewhere by some of your module or library. 😕

It is difficult for me to investigate the problem without seeing code or a reproducible example.

I think that is closed by the init method. Inside init method everything work and i can use loguru without any problem.

Infact:

--------------------------------------------------------------------
CASE 1:
file1:
from loguru import logger
class LoggerManager(object):
    ## Set class version
    ROBOT_LIBRARY_VERSION = '1.0'
    ## Create clear method given that this class is global
    ROBOT_LIBRARY_SCOPE   = 'GLOBAL'
    def __init__(self, fn_caller=None):
         fmt = " xxxx "
        logger.remove()
        logger.configure(
        handlers=[
            dict(sink=sys.stderr, ......)
        logger.info("THIS IS OK")
file2:
from loguru import logger
logger.info("THIS IS NOT OK")
--------------------------------------------------------------------
--------------------------------------------------------------------
CASE 2:
file1:
from loguru import logger
         fmt = " xxxx "
        logger.remove()
        logger.configure(
        handlers=[
            dict(sink=sys.stderr, ......)
class LoggerManager(object):
    ## Set class version
    ROBOT_LIBRARY_VERSION = '1.0'
    ## Create clear method given that this class is global
    ROBOT_LIBRARY_SCOPE   = 'GLOBAL'
    def __init__(self, fn_caller=None):
        logger.info("THIS IS OK")
file2:
from loguru import logger
logger.info("THIS IS OK")
--------------------------------------------------------------------
CASE 3:
file1:
from loguru import logger
class LoggerManager(object):
    ## Set class version
    ROBOT_LIBRARY_VERSION = '1.0'
    ## Create clear method given that this class is global
    ROBOT_LIBRARY_SCOPE   = 'GLOBAL'
    def __init__(self, fn_caller=None):
        logger.info("THIS IS OK")
file2:
from loguru import logger
logger.info("THIS IS OK")
          

@pie-r Thanks for the code samples. Unfortunately, I'm still unable to reproduce the error.

How do file1 and file2 interfere with each others? How do you instantiate your LoggerManager()?

Loguru does not close() the sys.stderr stream. Never. I think this might be a side-effect of another of your functions or libraries. I can only try to guess at this point.