Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

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

_sub_tag() could not find tag #177

Closed
yoonghm opened this issue Nov 23, 2019 · 10 comments
Closed

_sub_tag() could not find tag #177

yoonghm opened this issue Nov 23, 2019 · 10 comments
Labels
enhancement Improvement to an already existing feature

Comments

@yoonghm
Copy link

yoonghm commented Nov 23, 2019

I have a simple program using a file as sink:

from loguru import logger
import os

logger.remove()  # Remove the default logger

logfile = r'logfile.log'
loglevel = os.getenv('LOGLEVEL', 'INFO')
loglevel = loglevel.replace('"', '')

fmt='{time:YYYY:MM:DD HH:MM:SS} | {level: <8} | {name}:{function}:{line} - {message}'

logger.add(
    logfile,
    format=fmt,
    level=loglevel
)
logger.trace   ("TRACE    priority message")
logger.debug   ("DEBUG    priority message")
logger.info    ("INFO     priority message")
logger.success ("SUCCESS  priority message")
logger.warning ("WARNING  priority message")
logger.error   ("ERROR    priority message")
logger.critical("CRITICAL priority message")

It shows the following errors:

--- Logging error in Loguru Handler #1 ---
Record was: {'elapsed': datetime.timedelta(microseconds=14967), 'exception': None, 'extra': {}, 'file': 'script.py', 'function': '<module>', 'level': 'TRACE', 'line': 17, 'message': 'TRACE    priority message', 'module': 'script', 'name': '__main__', 'process': '6208', 'thread': '10716', 'time': datetime(2019, 11, 24, 1, 32, 19, 778917, tzinfo=datetime.timezone(datetime.timedelta(seconds=28800), 'Malay Peninsula Standard Time'))}
Traceback (most recent call last):
  File "C:\home\Anaconda3\lib\site-packages\loguru\_handler.py", line 89, in emit
    precomputed_format = self._decolorize_format(dynamic_format)
  File "C:\home\Anaconda3\lib\site-packages\loguru\_handler.py", line 209, in _decolorize_format
    return AnsiMarkup(custom_markups=markups, strip=True).feed(format_, strict=True)
  File "C:\home\Anaconda3\lib\site-packages\loguru\_ansimarkup.py", line 174, in feed
    text = self._regex_tag.sub(self._sub_tag, text)
  File "C:\home\Anaconda3\lib\site-packages\loguru\_ansimarkup.py", line 248, in _sub_tag
    "make sure you did not misspelled it" % markup
ValueError: Tag "<module>" does not corespond to any known ansi directive, make sure you did not misspelled it
--- End of logging error ---
--- Logging error in Loguru Handler #1 ---
Record was: {'elapsed': datetime.timedelta(microseconds=17965), 'exception': None, 'extra': {}, 'file': 'script.py', 'function': '<module>', 'level': 'DEBUG', 'line': 18, 'message': 'DEBUG    priority message', 'module': 'script', 'name': '__main__', 'process': '6208', 'thread': '10716', 'time': datetime(2019, 11, 24, 1, 32, 19, 781915, tzinfo=datetime.timezone(datetime.timedelta(seconds=28800), 'Malay Peninsula Standard Time'))}
Traceback (most recent call last):
  File "C:\home\Anaconda3\lib\site-packages\loguru\_handler.py", line 89, in emit
    precomputed_format = self._decolorize_format(dynamic_format)
  File "C:\home\Anaconda3\lib\site-packages\loguru\_handler.py", line 209, in _decolorize_format
    return AnsiMarkup(custom_markups=markups, strip=True).feed(format_, strict=True)
  File "C:\home\Anaconda3\lib\site-packages\loguru\_ansimarkup.py", line 174, in feed
    text = self._regex_tag.sub(self._sub_tag, text)
  File "C:\home\Anaconda3\lib\site-packages\loguru\_ansimarkup.py", line 248, in _sub_tag
    "make sure you did not misspelled it" % markup
ValueError: Tag "<module>" does not corespond to any known ansi directive, make sure you did not misspelled it
--- End of logging error ---
--- Logging error in Loguru Handler #1 ---
Record was: {'elapsed': datetime.timedelta(microseconds=28922), 'exception': None, 'extra': {}, 'file': 'script.py', 'function': '<module>', 'level': 'INFO', 'line': 19, 'message': 'INFO     priority message', 'module': 'script', 'name': '__main__', 'process': '6208', 'thread': '10716', 'time': datetime(2019, 11, 24, 1, 32, 19, 792872, tzinfo=datetime.timezone(datetime.timedelta(seconds=28800), 'Malay Peninsula Standard Time'))}
Traceback (most recent call last):
  File "C:\home\Anaconda3\lib\site-packages\loguru\_handler.py", line 89, in emit
    precomputed_format = self._decolorize_format(dynamic_format)
  File "C:\home\Anaconda3\lib\site-packages\loguru\_handler.py", line 209, in _decolorize_format
    return AnsiMarkup(custom_markups=markups, strip=True).feed(format_, strict=True)
  File "C:\home\Anaconda3\lib\site-packages\loguru\_ansimarkup.py", line 174, in feed
    text = self._regex_tag.sub(self._sub_tag, text)
  File "C:\home\Anaconda3\lib\site-packages\loguru\_ansimarkup.py", line 248, in _sub_tag
    "make sure you did not misspelled it" % markup
ValueError: Tag "<module>" does not corespond to any known ansi directive, make sure you did not misspelled it
--- End of logging error ---
--- Logging error in Loguru Handler #1 ---
Record was: {'elapsed': datetime.timedelta(microseconds=33908), 'exception': None, 'extra': {}, 'file': 'script.py', 'function': '<module>', 'level': 'SUCCESS', 'line': 20, 'message': 'SUCCESS  priority message', 'module': 'script', 'name': '__main__', 'process': '6208', 'thread': '10716', 'time': datetime(2019, 11, 24, 1, 32, 19, 797858, tzinfo=datetime.timezone(datetime.timedelta(seconds=28800), 'Malay Peninsula Standard Time'))}
Traceback (most recent call last):
  File "C:\home\Anaconda3\lib\site-packages\loguru\_handler.py", line 89, in emit
    precomputed_format = self._decolorize_format(dynamic_format)
  File "C:\home\Anaconda3\lib\site-packages\loguru\_handler.py", line 209, in _decolorize_format
    return AnsiMarkup(custom_markups=markups, strip=True).feed(format_, strict=True)
  File "C:\home\Anaconda3\lib\site-packages\loguru\_ansimarkup.py", line 174, in feed
    text = self._regex_tag.sub(self._sub_tag, text)
  File "C:\home\Anaconda3\lib\site-packages\loguru\_ansimarkup.py", line 248, in _sub_tag
    "make sure you did not misspelled it" % markup
ValueError: Tag "<module>" does not corespond to any known ansi directive, make sure you did not misspelled it
--- End of logging error ---
--- Logging error in Loguru Handler #1 ---
Record was: {'elapsed': datetime.timedelta(microseconds=40889), 'exception': None, 'extra': {}, 'file': 'script.py', 'function': '<module>', 'level': 'WARNING', 'line': 21, 'message': 'WARNING  priority message', 'module': 'script', 'name': '__main__', 'process': '6208', 'thread': '10716', 'time': datetime(2019, 11, 24, 1, 32, 19, 804839, tzinfo=datetime.timezone(datetime.timedelta(seconds=28800), 'Malay Peninsula Standard Time'))}
Traceback (most recent call last):
  File "C:\home\Anaconda3\lib\site-packages\loguru\_handler.py", line 89, in emit
    precomputed_format = self._decolorize_format(dynamic_format)
  File "C:\home\Anaconda3\lib\site-packages\loguru\_handler.py", line 209, in _decolorize_format
    return AnsiMarkup(custom_markups=markups, strip=True).feed(format_, strict=True)
  File "C:\home\Anaconda3\lib\site-packages\loguru\_ansimarkup.py", line 174, in feed
    text = self._regex_tag.sub(self._sub_tag, text)
  File "C:\home\Anaconda3\lib\site-packages\loguru\_ansimarkup.py", line 248, in _sub_tag
    "make sure you did not misspelled it" % markup
ValueError: Tag "<module>" does not corespond to any known ansi directive, make sure you did not misspelled it
--- End of logging error ---
--- Logging error in Loguru Handler #1 ---
Record was: {'elapsed': datetime.timedelta(microseconds=45878), 'exception': None, 'extra': {}, 'file': 'script.py', 'function': '<module>', 'level': 'ERROR', 'line': 22, 'message': 'ERROR    priority message', 'module': 'script', 'name': '__main__', 'process': '6208', 'thread': '10716', 'time': datetime(2019, 11, 24, 1, 32, 19, 809828, tzinfo=datetime.timezone(datetime.timedelta(seconds=28800), 'Malay Peninsula Standard Time'))}
Traceback (most recent call last):
  File "C:\home\Anaconda3\lib\site-packages\loguru\_handler.py", line 89, in emit
    precomputed_format = self._decolorize_format(dynamic_format)
  File "C:\home\Anaconda3\lib\site-packages\loguru\_handler.py", line 209, in _decolorize_format
    return AnsiMarkup(custom_markups=markups, strip=True).feed(format_, strict=True)
  File "C:\home\Anaconda3\lib\site-packages\loguru\_ansimarkup.py", line 174, in feed
    text = self._regex_tag.sub(self._sub_tag, text)
  File "C:\home\Anaconda3\lib\site-packages\loguru\_ansimarkup.py", line 248, in _sub_tag
    "make sure you did not misspelled it" % markup
ValueError: Tag "<module>" does not corespond to any known ansi directive, make sure you did not misspelled it
--- End of logging error ---
--- Logging error in Loguru Handler #1 ---
Record was: {'elapsed': datetime.timedelta(microseconds=53855), 'exception': None, 'extra': {}, 'file': 'script.py', 'function': '<module>', 'level': 'CRITICAL', 'line': 23, 'message': 'CRITICAL priority message', 'module': 'script', 'name': '__main__', 'process': '6208', 'thread': '10716', 'time': datetime(2019, 11, 24, 1, 32, 19, 817805, tzinfo=datetime.timezone(datetime.timedelta(seconds=28800), 'Malay Peninsula Standard Time'))}
Traceback (most recent call last):
  File "C:\home\Anaconda3\lib\site-packages\loguru\_handler.py", line 89, in emit
    precomputed_format = self._decolorize_format(dynamic_format)
  File "C:\home\Anaconda3\lib\site-packages\loguru\_handler.py", line 209, in _decolorize_format
    return AnsiMarkup(custom_markups=markups, strip=True).feed(format_, strict=True)
  File "C:\home\Anaconda3\lib\site-packages\loguru\_ansimarkup.py", line 174, in feed
    text = self._regex_tag.sub(self._sub_tag, text)
  File "C:\home\Anaconda3\lib\site-packages\loguru\_ansimarkup.py", line 248, in _sub_tag
    "make sure you did not misspelled it" % markup
ValueError: Tag "<module>" does not corespond to any known ansi directive, make sure you did not misspelled it
--- End of logging error ---

However, if I remove the format=fmt line, the code works perfectly.

@Delgan
Copy link
Owner

Delgan commented Nov 23, 2019

It works fine on my computer... 😕

Is it this exact code snippet which is triggering the error?

It looks like Loguru tries to reformat a message already formatted, which causes error as the initial formatting transformed "{name}" in "<module>" which fails to be parsed during the second formatting.

@yoonghm
Copy link
Author

yoonghm commented Nov 23, 2019

I could fix it now, I need to set colorize=False.

@yoonghm
Copy link
Author

yoonghm commented Nov 23, 2019

I need to remove ' by

loglevel = loglevel.replace('"', '')

If I do not, the loglevel will have additional quotes.

@Delgan
Copy link
Owner

Delgan commented Nov 23, 2019

I could fix it now, I need to set colorize=False.

Yep, but it should not have failed in the first place. 😅

If you have more information on the code that generates the error, it might be useful to help resolve this.

@yoonghm
Copy link
Author

yoonghm commented Nov 23, 2019

I am using version 0.3.2.

@Delgan
Copy link
Owner

Delgan commented Nov 23, 2019

Looking at the line 89 which appears on the error traceback:

precomputed_format = self._decolorize_format(dynamic_format)

It's called only if self._is_formatter_dynamic == True which is set if the format argument is a callable(). Your code snippet shows that fmt is a str, so the error message looks inconsistent with the code snippet. 😕

@Delgan
Copy link
Owner

Delgan commented Nov 23, 2019

Got it! That happens if format=format (the built-in). That's "funny", I never realized it was possible. It's very prone to error. I will see if I can improve that.

@Delgan Delgan added the enhancement Improvement to an already existing feature label Nov 23, 2019
@yoonghm
Copy link
Author

yoonghm commented Nov 24, 2019

Sorry, I do not get your point. However, today is a new day and I have restarted my Windows 10. The problem disappeared when I removed or commented colorize=False. Here is the whole code

from loguru import logger
import os

logger.remove()  # Remove the default logger

logfile = r'logfile.log'
loglevel = os.getenv('LOGLEVEL', 'INFO')
loglevel = loglevel.replace('"', '')

fmt='{time:YYYY:MM:DD HH:MM:SS} | {level: <8} | {name}:{function}:{line} - {message}'

logger.add(
    logfile,
    format=fmt,
    level=loglevel
)
logger.trace   ("TRACE    priority message")
logger.debug   ("DEBUG    priority message")
logger.info    ("INFO     priority message")
logger.success ("SUCCESS  priority message")
logger.warning ("WARNING  priority message")
logger.error   ("ERROR    priority message")
logger.critical("CRITICAL priority message")

This is very strange to me too.

However, I still need to have loglevel.replace(...) if i set LOGLEVEL="TRACE" from a command prompt and run the script.

set LOGLEVEL="TRACE"

Of course, if I set the environment variable in following way, the problem will not appear:

set LOGLEVEL=TRACE

@Delgan
Copy link
Owner

Delgan commented Nov 24, 2019

Sorry, I do not get your point. However, today is a new day and I have restarted my Windows 10. The problem disappeared when I removed or commented colorize=False.

I think there was some confusion when you tried to debug this problem, this code snippet actually always worked fine. However, I think that at one time or another, you had something very slightly different that would generate the error, just a small typo:

logger.add(
    logfile,
    format=format,
    level=loglevel
)

Note the format=format. The logger accepts a function to be passed to the format argument. In such case, Loguru will first call this function so that it dynamically generates the logs format instead of using a static str (like your fmt variable). This dynamically generated format is then used to create the log message, just as the static one would have been.

Here, the format function passed to the format argument is actually the built-in format(). For this reason, Loguru will internally do something like fmt = format(record), which results in:

"{'elapsed': datetime.timedelta(microseconds=10002), 'exception': None, 'extra': {}, 'file': (name='a.py', path='a.py'), 'function': '<module>', 'level': (name='INFO', no=20, icon='ℹ️'), 'line': 20, 'message': 'INFO     priority message', 'module': 'a', 'name': '__main__', 'process': (id=100026, name='MainProcess'), 'thread': (id=140006286919296, name='MainThread'), 'time': datetime(2019, 11, 24, 9, 8, 26, 483395, tzinfo=datetime.timezone(datetime.timedelta(seconds=3600), 'CET'))}"

Later, this will raise an error, because fmt is expected to look like "{time} - {message}" so that Loguru can call message = fmt.format_map(record).

That's why I propose to add a check inside Loguru, to ensure that the format argument is different from the format() built-in. This is a mistake that can easily happen.

However, I still need to have loglevel.replace(...) if i set LOGLEVEL="TRACE" from a command prompt and run the script.

Yes, as you taught me, the set command should be used without quotes. I prefer not to automatically strip() the ", I don't think Loguru should try to fix incorrect usage of OS commands.

@Delgan
Copy link
Owner

Delgan commented Dec 1, 2019

Check for builtins format() and filter() incorrectly passed as arguments has been added to  0.4.0. The new error message is more explicit.

@Delgan Delgan closed this as completed Dec 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improvement to an already existing feature
Projects
None yet
Development

No branches or pull requests

2 participants