loguru icon indicating copy to clipboard operation
loguru copied to clipboard

All logs printed on the same line

Open tollis-hunt opened this issue 6 years ago • 8 comments

Hello! I cannot figure out what I am doing wrong. Anything I log is printed without a newline before or after it.

When I run the following:

from loguru import logger as log

log.info("info")
log.debug("debug")
log.error("error")
log.success("success")

I get the following:

2020-09-16 19:16:05.188INFO    __main__<module>9info2020-09-16 19:16:05.189DEBUG   __main__<module>10debug2020-09-16 19:16:05.189ERROR   __main__<module>11error2020-09-16 19:16:05.189SUCCESS __main__<module>12 |  | :: - 
 |  | :: - 
 |  | :: - 
 |  | :: - 
success

I was able to produce this with explicit '\n' at the end of each log, but this is what I expected:

2020-09-16 19:24:30.707INFO    __main__<module>9info
2020-09-16 19:24:30.707DEBUG   __main__<module>10debug
2020-09-16 19:24:30.707ERROR   __main__<module>11error
2020-09-16 19:24:30.707SUCCESS __main__<module>12success
 |  | :: - 
 |  | :: - 
 |  | :: - 
 |  | :: - 

If it helps, I am on python 3.8, PyCharm pro 2020.2, Windows 10. Am I missing some kind of configuration line I am not seeing in the docs? Any advice on this matter would be greatly appreciated.

tollis-hunt avatar Sep 17 '20 01:09 tollis-hunt

Huh, that's weird. :confused:

There is absolutely no reason you should see this. There is not any configuration option that would lead to such output. Curiously, the separators (like |, : and -) are printed on a different new line. These separators are the only one which are not wrapped by any color tag in the loguru default format.

Could this be a problem related to colorama? :thinking:

Delgan avatar Sep 17 '20 06:09 Delgan

So I just tried every version of colorama down to 0.2.0. Of course, it just didn't work when I tried versions 0.3.0 - 0.2.0. But I thought I would give it a try. My current version is 0.4.3. Any suggestions as to troubleshooting I could do?

tollis-hunt avatar Sep 17 '20 15:09 tollis-hunt

Did you try running the script in your terminal instead of using PyCharm?

Delgan avatar Sep 17 '20 16:09 Delgan

Oh my gosh, I can't believe I didn't think of that.

It prints out perfectly fine outside PyCharm. In fact, even the terminal within PyCharm works just fine it looks like the only issue is when I click "Run" or "Debug". Ain't that interesting...

FIX: So I thought, maybe it's a plugin! The very first plugin I disabled was Grep Console. And loguru now works as expected. Interestingly turning all the grep options off did not fix it, but disabling the plugin does.

I am closing this ticket, thanks so much for your timely response! 😃

tollis-hunt avatar Sep 17 '20 19:09 tollis-hunt

No problem, glad you figured out the solution! :smiley:

Delgan avatar Sep 17 '20 19:09 Delgan

Hello @Delgan, I am experiencing a similar issue as soon as I define a format as a callable. I.e. all logs are printed on the same line.

Here is my code:

import sys

from loguru import logger

# Same as loguru's default
DEFAULT_FORMAT = (
    "<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | "
    "<level>{level: <8}</level> | "
    "<cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>"
)


def formatter(record):
    return DEFAULT_FORMAT


logger.remove()
logger.add(sys.stderr, format=formatter)
logger.info("Foo")
logger.warning("Bar")
logger.error("Baz")

Output:

$ python log_fail.py 
2024-02-15 09:54:50.982 | INFO     | __main__:<module>:21 - Foo2024-02-15 09:54:50.982 | WARNING  | __main__:<module>:22 - Bar2024-02-15 09:54:50.983 | ERROR    | __main__:<module>:23 - Baz

If I directly pass format=DEFAULT_FORMAT into logger.add(), it works perfectly:

-logger.add(sys.stderr, format=formatter)
+logger.add(sys.stderr, format=DEFAULT_FORMAT)

Output:

$ python log_fail.py 
2024-02-15 10:00:36.242 | INFO     | __main__:<module>:19 - Foo
2024-02-15 10:00:36.242 | WARNING  | __main__:<module>:20 - Bar
2024-02-15 10:00:36.242 | ERROR    | __main__:<module>:21 - Baz

Any idea on why this happens?

I am running on Python 3.12.1 and loguru 0.7.2

Thanks a lot for your help (and the amazing library)! :slightly_smiling_face:

RafaelWO avatar Feb 15 '24 09:02 RafaelWO

Hey @RafaelWO.

When format is a function, you have complete control over how the logs are formatted. This implies you also become responsible for adding the line-ending. You can add "\n" at the end of your DEFAULT_FORMAT and it should work as expected. :+1:

See documentation:

If fine-grained control is needed, the format can also be a function which takes the record as parameter and return the format template string. However, note that in such a case, you should take care of appending the line ending and exception field to the returned format, while "\n{exception}" is automatically appended for convenience if format is a string.

Note that with your current usage of DEFAULT_FORMAT, the exception won't be printed either.

Delgan avatar Feb 17 '24 08:02 Delgan

Ahh, I see! Apparently, I overlooked that info in the docs. Thanks for clarifying, @Delgan 🙂

RafaelWO avatar Feb 17 '24 11:02 RafaelWO