0

Suppose I'm working on a python project which needs to use a third-party module such as tweepy https://github.com/tweepy/tweepy. I would like to save the logs output from this third-party module to a local file so I can check it regularly. It happens that root logger is used inside tweepy so the logs are output to console. In this case, no filehandler is yet added to the root logger, so how could I save the logs to a local file?

One way I think of is to use python run.py > ~/project/logs/test.log to redirect all logs to the local file test.log, but is this the best way to do it? I can already see a downside: it will save all logs not only the ones from the third-party module.

Rafael
  • 1,761
  • 1
  • 14
  • 21
  • You can add your own log handler to Tweepy's logger. :) – h4z3 Mar 24 '21 at 16:40
  • oh right, so Logger is actually a singleton pattern, so I only need to add a Filehandler once, and all the logs will be output to the same place. Is that what you mean ? – Rafael Mar 24 '21 at 16:56
  • 1
    No, import this one logger and add a desired handler to it. You can have other loggers with other (or same) handlers. – h4z3 Mar 24 '21 at 22:47
  • I found solution here https://stackoverflow.com/questions/15727420/using-logging-in-multiple-modules – Rafael Mar 25 '21 at 12:54

0 Answers0