Logging to file and not to console

Johannes Findeisen mailman at hanez.org
Sun Oct 27 10:32:47 EDT 2013


On Sun, 27 Oct 2013 11:09:08 +0100
Peter Otten wrote:

> Johannes Findeisen wrote:
> 
> > Hi all,
> > 
> > I am going crazy with logging. I have an application which sets up
> > logging after parsing the args in the main() funktion. It needs to be
> > setup after parsing the args because I can set the loglevel via
> > commandline flags.
> > 
> > I have tried many variants on how to do that but every time with an
> > weird result.
> > 
> > What I want is logging in from all libs and really understand that doing
> > this should be enough there:
> > 
> > from logging import getLogger
> > logger = getLogger(__name__)
> > 
> > But, I need to setup the logger in the main() function to log only to a
> > file and not to console because my application has an own shell
> > interface which should not be spammed with log messages - never a
> > message should show up there.
> > 
> > I think it should be only some few lines of code but I can't figure
> > that out. The logger should be configured to have a max file size and
> > rotate logfiles.
> > 
> > Can someone help me with this?
> 
> import logging
> import logging.handlers
> 
> logger = logging.getLogger(__name__)
> 
> def levelnames():
>     try:
>         names = logging._nameToLevel
>     except AttributeError:
>         names = (name for name in logging._levelNames if isinstance(name, 
> str))
> 
> def main():
>     import argparse
>     parser = argparse.ArgumentParser()
>     parser.add_argument("--logging-file", default="tmp.log")
>     parser.add_argument("--logging-level", choices=levelnames(), 
> default="INFO")
>     args = parser.parse_args()
> 
>     root = logging.getLogger()
> 
>     formatter = logging.Formatter(logging.BASIC_FORMAT)
>     handler = logging.handlers.RotatingFileHandler(args.logging_file, 
> maxBytes=100, backupCount=3)
>     handler.setFormatter(formatter)
> 
>     root.addHandler(handler)
>     root.setLevel(args.logging_level)
> 
>     logger.info("your info")
>     logger.warn("your warning")
>     logger.critical("your critical message")
> 
> if __name__ == "__main__":
>     main()
> 
> Does this do what you want?
> (maxBytes is probably a bit low)

Yes! That was exactly what I want! Thank you very much! Now it behaves
well. I know that maxBytes is slow but that will be configurable in the
future. I just needed some clean way to get debug messages even from
libs like APScheduler. When running my application in production mode
logging will be completely disabled so it is Ok this way.

I knew there must be an easy way because in Python most things are well
designed so logging is an every day task and it should be implemented
well. And, it definitely is.

I only did "logger = logging.getLogger(__name__)" on top of my file
where the main() function resists. Afterwards I played around with that
object in main() and tried to configure it. As I can see you are
requesting the logger object twice as "root" and the configures it.
You put that problem across to me.

You saved my day... :)

Happy hacking!
Johannes



More information about the Python-list mailing list