Skip to content

logging full warning message #30

Closed
@KarinaMarks

Description

Hi @Delgan ,

Thanks for the great package! I'm swapping from the core logging package to loguru in a python package I am developing. In general the swap has been really easy - but I'm having a problem with warnings/error messages.

I have made a minimal example of the conversion I am trying to make and the log outputs. From the logging package:

import logging

def make_logging_warnings():
    logging.basicConfig(filename="output.log", 
            format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
            level=logging.INFO)
    logger = logging.getLogger()

    logger.info("Let's start logging")
    try:
        raise UserWarning("I'm warning you!")
    except UserWarning:
        logger.warning('Warning encountered in function:', exc_info=True)

make_logging_warnings()

Which returns the following log:

2019-01-09 12:37:55,666 root         INFO     Let's start logging
2019-01-09 12:37:55,666 root         WARNING  Warning encountered in function:
Traceback (most recent call last):
  File "<ipython-input-1-59e413187e43>", line 12, in make_logging_warnings
    raise UserWarning("I'm warning you!")
UserWarning: I'm warning you!

Converting this into loguru the warning traceback doesn't seem to work:

from loguru import logger

def make_loguru_warnings():
    logger.add("output.log", level='INFO')

    logger.info("Let's start logging")
    try:
        raise UserWarning("I'm warning you!")
    except UserWarning:
        logger.warning('Warning encountered in function:', exc_info=True)

make_loguru_warnings()

I get the output:

2019-01-09 12:40:17.751 | INFO     | __main__:make_loguru_warnings:6 - Let's start logging
2019-01-09 12:40:17.754 | WARNING  | __main__:make_loguru_warnings:10 - Warning encountered in function:

See how I'm missing the traceback here?

I have a similar issue with errors, but they're giving too much information back in loguru - I think this is to do with better_exceptions, can this be turned off?

To make the above code work for errors I have just changed:

  • UserWarning("I'm warning you!") to AssertionError("You're not assertive!")
  • logger.warning("Warning encountered in function: ", exc_info=True) to logger.exception("Error encountered in function: ")

I get the following logs for both packages:

From logging

2019-01-09 12:44:06,843 root         INFO     Let's start logging
2019-01-09 12:44:06,843 root         ERROR    Error encountered in function:
Traceback (most recent call last):
  File "<ipython-input-2-759d24f4d830>", line 9, in make_logging_errors
    raise AssertionError("You're not assertive")
AssertionError: You're not assertive

From loguru

2019-01-09 12:45:36.377 | INFO     | __main__:make_loguru_errors:6 - Let's start logging
2019-01-09 12:45:36.379 | ERROR    | __main__:make_loguru_errors:10 - Error encountered in function:
Traceback (most recent call last):

  File "C:\Users\kmarks\AppData\Local\Continuum\miniconda3\envs\cm\Scripts\ipython-script.py", line 10, in <module>
    sys.exit(start_ipython())
    |   |    -> <function start_ipython at 0x00000181A3133598>
    |   -> <built-in function exit>
    -> <module 'sys' (built-in)>

  File "C:\Users\kmarks\AppData\Local\Continuum\miniconda3\envs\cm\lib\site-packages\IPython\__init__.py", line 125, in start_ipython
    return launch_new_instance(argv=argv, **kwargs)
           |                   |    |       -> {}
           |                   |    -> None
           |                   -> None
           -> <bound method Application.launch_instance of <class 

            .... + loads more lines .....

  File "<ipython-input-3-e3410db626c5>", line 12, in <module>
    make_loguru_errors()
    -> <function make_loguru_errors at 0x00000181A3DEA0D0>
> File "<ipython-input-3-e3410db626c5>", line 8, in make_loguru_errors
    raise AssertionError("You're not assertive")
          -> <class 'AssertionError'>

AssertionError: You're not assertive

I see information on using the backtrace=True/False argument, but that still gives much more information on the error than I am looking for. I am guessing the error problem is to do with better_exceptions? I'm not sure about the warning one. I would really appreciate some help with this.

Thanks!

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentationenhancementImprovement to an already existing featurequestionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions