1

I wanted to add some logging to colored print statements. The naive approach was to create a function

def log_and_print(msg,level='info',color="", reset=""):
    if level == 'info':
        logging.info(msg)
    elif level == 'debug':
        logging.debug(msg)
    print(color+str(msg)+reset)

It works fine, but discovered that of course the path that is being logged isn't correct anymore. Is there some better solution? I hoped to be able to keep the log_and_print function, since it is used in a lot of places.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
MichaelRazum
  • 789
  • 1
  • 10
  • 26
  • 1
    Look about https://github.com/willmcgugan/rich – azro Aug 04 '21 at 09:16
  • What is the expected path and what is the path you're getting? – Datajack Aug 04 '21 at 09:16
  • @Datajack I believe the source of the log message would be from a different file/module instead of where the function `log_and_print` is called from – shoaib30 Aug 04 '21 at 09:17
  • @shoaib30 exactly. So maybe the suggestion with rich is the right approach. In that case, I would have to rewrite of course all the calls of the log_and_print function. – MichaelRazum Aug 04 '21 at 09:20
  • @MichaelRazum I don't know about logging, but does [this](https://stackoverflow.com/questions/37546770/python-choose-logging-files-directory) or [this](https://stackoverflow.com/questions/50824566/python-logging-provide-log-file-path-from-main-module) help you? The second one seems similar to what you want, I think... – Datajack Aug 04 '21 at 09:28
  • Not entirely sure of what you are trying to achieve with the print statement, but have you tried implementing a custom log handler that will print the way you want and call the default handler for the rest. that way you don't need to call `log_and_print` you can just use the logger – shoaib30 Aug 04 '21 at 09:30
  • @shoaib30 a call would look this way `log_and_print("log_str",color=Fore.CYAN)` with `from colorama import Fore`. Before that it was just print(Fore.CYAN + "log_str"). When print was exchanged with `logging.info(Fore.CYAN + "log_str")` it added ugly artifacts in the log message. – MichaelRazum Aug 04 '21 at 09:33
  • Got it, it would help if you add that to your question. Check [this](https://stackoverflow.com/a/56944256/5236575) out. You can create two handlers and attach formatting and output of one log to the console and the other which you already have. It would work if your colors are tied with the log levels – shoaib30 Aug 04 '21 at 09:42
  • @MichaelRazum another option - https://github.com/Delgan/loguru#pretty-logging-with-colors – shoaib30 Aug 04 '21 at 09:45
  • It does not answer your specific question of maintaining the path when logging from a function, hence I am not adding these as an answer. – shoaib30 Aug 04 '21 at 09:47
  • 1
    @shoaib30 thanks a lot! Guess will try logging-with-colors and shib. The color is not tied to the log level. Another solution would be maybe somehow to read out the path of the parent function and add it as an additional parameter to logging as kwargs. But seems a bit ugly as a workaround. – MichaelRazum Aug 04 '21 at 09:49

0 Answers0