Help! my python logging library has a bug , I cant solve this

Bug description:

This is the test scripts:

from FuXLogger import LogManager, LogLevel, LogFormatter, StreamHandler , FileHandler my_log_fmt = LogFormatter("{time} | {levelName:<7} | {module}:{function} | {file}:{line} | {message}") # 我习惯这样的格式 # 然后创建一个StreamHandler, 输出到控制台 console_handler = StreamHandler("console", LogLevel.ON, my_log_fmt, colorize=True, enableXMLRender=True) file_handler = FileHandler("file", LogLevel.ON, my_log_fmt, filename="test.log") # 这样就可以了 logger = LogManager.getLogger("test",LogLevel.ON, my_log_fmt, enqueue=True) # 启用enqueue, 不阻塞主线程 logger.addHandler(console_handler) # 添加到日志记录器 logger.addHandler(file_handler) # 添加到日志记录器 logger.trace("trace message") logger.debug("debug message") logger.info("info message") logger.warning("warning message") logger.error("error message") logger.fatal("fatal message")

OutPut:

2024-10-05 11:15:41 | TRACE | __main__:<module> | test.py:13 | trace message 2024-10-05 11:15:41 | DEBUG | __main__:<module> | test.py:14 | debug message 2024-10-05 11:15:41 | INFO | __main__:<module> | test.py:15 | info message

for more information, look to:

FuXLogger/core/logger.py

CPython versions tested on:

3.12

Operating systems tested on:

Windows

infos:

I implemented a logging library in python, called FuXLogger, but it leaked output, and after debugging, I found that it might be a queue problem?

You have pasted your code a one very long line.

Please copy and paste you code with the new lines and spaces preserved so that we can see what you have written.

1 Like

MY FuXLogger logging library bugs

ALL BUGS

2024-10-5 (bug1. log Incomplete with unknow reasons)

one day , I want make sure my logging-library FuXLogger is working normally, so I write a simple code to test it.


from FuXLogger import LogManager, LogLevel, LogFormatter, StreamHandler , FileHandler

my_log_fmt = LogFormatter("{time} | {levelName:<7} | {module}:{function} | {file}:{line} | {message}") # 我习惯这样的格式

# 然后创建一个StreamHandler, 输出到控制台

console_handler = StreamHandler("console", LogLevel.ON, my_log_fmt, colorize=True, enableXMLRender=True)

file_handler = FileHandler("file", LogLevel.ON, my_log_fmt, filename="test.log") # 这样就可以了

logger = LogManager.getLogger("test",LogLevel.ON, my_log_fmt, enqueue=True) # 启用enqueue, 不阻塞主线程

logger.addHandler(console_handler) # 添加到日志记录器

logger.addHandler(file_handler) # 添加到日志记录器

logger.trace("trace message")

logger.debug("debug message")

logger.info("info message")

logger.warning("warning message")

logger.error("error message")

logger.fatal("fatal message")

this is output:


2024-10-05 23:27:49 | TRACE | __main__:<module> | test.py:13 | trace message

2024-10-05 23:27:49 | DEBUG | __main__:<module> | test.py:14 | debug message

and output next:


2024-10-05 23:28:22 | TRACE | __main__:<module> | test.py:13 | trace message

2024-10-05 23:28:22 | DEBUG | __main__:<module> | test.py:14 | debug message

2024-10-05 23:28:22 | INFO | __main__:<module> | test.py:15 | info message

my logging-library not logged level higher than INFO(some times not logged level higher than DEBUG) messages.

This is so excepted and I don’t know why it happened.

so I test my class Logger method ‘{level}’ is working normally. I test it one by one.


logger.trace("trace message")


logger.debug("debug message")


logger.info("info message")


logger.warning("warning message")


logger.error("error message")


logger.fatal("fatal message")

the output all of them:


2024-10-05 23:30:22 | TRACE | __main__:<module> | test.py:13 | trace message


2024-10-05 23:30:22 | DEBUG | __main__:<module> | test.py:14 | debug message


2024-10-05 23:30:22 | INFO | __main__:<module> | test.py:15 | info message


2024-10-05 23:30:22 | WARNING | __main__:<module> | test.py:16 | warning message


2024-10-05 23:30:22 | ERROR | __main__:<module> | test.py:17 | error message


2024-10-05 23:30:22 | FATAL | __main__:<module> | test.py:18 | fatal message

all of them logged normally, so I think the bug is not in my templated __log method.

The amazing thing is that when I don’t add the FileHandler in to logger, the logger is working fine!


from FuXLogger import LogManager, LogLevel, LogFormatter, StreamHandler , FileHandler

my_log_fmt = LogFormatter("{time} | {levelName:<7} | {module}:{function} | {file}:{line} | {message}") # 我习惯这样的格式

# 然后创建一个StreamHandler, 输出到控制台

console_handler = StreamHandler("console", LogLevel.ON, my_log_fmt, colorize=True, enableXMLRender=True)

logger = LogManager.getLogger("test",LogLevel.ON, my_log_fmt, enqueue=True) # 启用enqueue, 不阻塞主线程

logger.addHandler(console_handler) # 添加到日志记录器

logger.trace("trace message")

logger.debug("debug message")

logger.info("info message")

logger.warning("warning message")

logger.error("error message")

logger.fatal("fatal message")


2024-10-05 23:32:22 | TRACE | __main__:<module> | test.py:13 | trace message

2024-10-05 23:32:22 | DEBUG | __main__:<module> | test.py:14 | debug message

2024-10-05 23:32:22 | INFO | __main__:<module> | test.py:15 | info message

2024-10-05 23:32:22 | WARNING | __main__:<module> | test.py:16 | warning message

2024-10-05 23:32:22 | ERROR | __main__:<module> | test.py:17 | error message

2024-10-05 23:32:22 | FATAL | __main__:<module> | test.py:18 | fatal message

In short, when both FileHandler and StreamHandler are added, and the enqueue option is enabled at the same time, the log will Incomplete with unknow reasons.

but I write a loop code to test it:


for i in range(100):

logger.trace("trace message")

logger.debug("debug message")

logger.info("info message")

logger.warning("warning message")

logger.error("error message")

logger.fatal("fatal message")

The log working normally when the loop is running but the log Incomplete on loop start and end. I don’t know why.

Where is the code of FuXLogger?

Also you have pasted more code that has lost its spaces indenting.

1 Like