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

Write to multiple file objects #768

Open
hadim opened this issue Jul 4, 2019 · 0 comments
Open

Write to multiple file objects #768

hadim opened this issue Jul 4, 2019 · 0 comments

Comments

@hadim
Copy link
Contributor

hadim commented Jul 4, 2019

(Maybe somehow related to #737 and #313)

Consider the following code from #313 :

class TqdmToLogger(io.StringIO):
    """
        Output stream for TQDM which will output to logger module instead of
        the StdOut.
    """
    logger = None
    level = None
    buf = ''
    def __init__(self,logger,level=None):
        super(TqdmToLogger, self).__init__()
        self.logger = logger
        self.level = level or logging.INFO
    def write(self,buf):
        self.buf = buf.strip('\r\n\t ')
    def flush(self):
        self.logger.log(self.level, self.buf)

if __name__ == "__main__":
    logging.basicConfig(format='%(asctime)s [%(levelname)-8s] %(message)s')
    logger = logging.getLogger()
    logger.setLevel(logging.DEBUG)

    tqdm_out = TqdmToLogger(logger,level=logging.INFO)
    for x in tqdm(range(100),file=tqdm_out,mininterval=30,):
        time.sleep(.5)

Would that be possible to write to both sys.stdout (or sys.stderr depending on the context) AND TqdmToLogger?

I tried to write to sys.stdout directly in TqdmToLogger but tqdm seems to do some fancy things when self.fp is sys.stdout or sys.stderr.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant