question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

_sub_tag() could not find tag

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
Delgancommented, 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.

0reactions
Delgancommented, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ElementTree from python doesn't find a child tag
I'm trying to access an element from a XML file from Python with ElementTree , and it doesn't find it. XML code: <item>...
Read more >
<sup> and <sub> tag not working · Issue #339 - GitHub
So basically, it seems like the only option is to wrap the rendered <Text> component inside a <View> component for subscript/superscript cases, ...
Read more >
Language tags in HTML and XML - W3C
For HTML and XML language markup, the case should not matter. Using the subtag registry. As mentioned above, you used to find subtags...
Read more >
ffmpeg: "Could not find tag for codec none" - audio - Super User
I had a similar problem with a .mov file, and solved it using this info ...
Read more >
How the Resource Management System matches language tags
The tags match on the language, script, and region subtags, ... Description: The comparison starts with "pt-PT" but does not find an exact ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found