1

Is there any way in python logging module to send info & errors to stdout and debug to file. Some commands in my script produce a long output which I don't want to send to stdout.

I am using the following logging function which writes logs to file and stdout

def mylog(release_num, logdir='/tmp'):
    applog = logging.getLogger()
    applog.setLevel(logging.DEBUG)
    formatter = logging.Formatter("%(asctime)s %(levelname)s %(message)s", "%b %d %Y %H:%M:%S")

    logfile = "{}/{}.log".format(logdir, release_num)

    if not os.path.exists(logdir):
        os.makedirs(logdir)

    fileHandler = logging.FileHandler(logfile, 'ab')
    fileHandler.setLevel(logging.DEBUG)
    fileHandler.setFormatter(formatter)
    applog.addHandler(fileHandler)

    cformat = logging.Formatter("[%(levelname)8s] : %(message)s")
    consoleHandler = logging.StreamHandler(sys.stdout)
    consoleHandler.setFormatter(cformat)
    log.addHandler(consoleHandler)

    return applog
0

1 Answer 1

2

You need to set the loglevel of consoleHandler to logging.INFO to log messages of level info or higher through the handler:

consoleHandler.setLevel(logging.INFO)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.