How to package a logging.config file?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matthew Wilson

    How to package a logging.config file?

    I'm working on a package that uses the standard library logging module
    along with a .cfg file.

    In my code, I use
    logging.config. fileConfig('/home/matt/mypackage/matt.cfg') to load in
    the logging config file.

    However, it seems really obvious to me that this won't work when I share
    this package with others.

    I can't figure out what path to use when I load my .cfg file.

    Any ideas?

    Matt
  • Larry Bates

    #2
    Re: How to package a logging.config file?

    Matthew Wilson wrote:
    I'm working on a package that uses the standard library logging module
    along with a .cfg file.
    >
    In my code, I use
    logging.config. fileConfig('/home/matt/mypackage/matt.cfg') to load in
    the logging config file.
    >
    However, it seems really obvious to me that this won't work when I share
    this package with others.
    >
    I can't figure out what path to use when I load my .cfg file.
    >
    Any ideas?
    >
    Matt
    Normally you put the logging configuration file in the path where you put the
    program that you will be sharing (quite often with no path at all).

    -Larry

    Comment

    • Vinay Sajip

      #3
      Re: How to package a logging.config file?

      On Jul 14, 1:21 am, Matthew Wilson <m...@tplus1.co mwrote:
      I'm working on a package that uses the standard libraryloggingm odule
      along with a .cfg file.
      >
      In my code, I uselogging.conf ig.fileConfig('/home/matt/mypackage/matt.cfg') to load in
      theloggingconfi g file.
      >
      However, it seems really obvious to me that this won't work when I share
      this package with others.
      >
      I can't figure out what path to use when I load my .cfg file.
      >
      Any ideas?
      >
      Matt
      Is your package a library or an application? If it's a library, you
      should avoid configuring logging using a config file - this is because
      logging configuration is process-wide, and if multiple libraries use
      fileConfig to configure their logging, you may get unexpected results.

      If it's an application, then Larry's advice is good.

      Regards,


      Vinay Sajip

      Comment

      • Younger Wang

        #4
        RE: How to package a logging.config file?

        I had the similar question and my solution is:

        default_config = os.path.join(os .getcwd(), "log.config ")

        def get_logger(name , config_file=Non e):
        if config_file:
        logging.config. fileConfig(conf ig_file)
        else:
        logging.basicCo nfig(level=logg ing.INFO,
        format='%(level name)s %(module)s:%(li neno)d:
        %(message)s')

        return logging.getLogg er(name)

        I had to make this helper function because calling logging.getLogg er
        will fail in an exception without configuring logging module. I wish it
        is not like that.

        BR
        Younger Wang
        -----Original Message-----
        From: python-list-bounces+younger .wang=packetfro nt.com@python.o rg
        [mailto:python-list-bounces+younger .wang=packetfro nt.com@python.o rg] On
        Behalf Of Matthew Wilson
        Sent: Monday, July 14, 2008 8:21 AM
        To: python-list@python.org
        Subject: How to package a logging.config file?

        I'm working on a package that uses the standard library logging module
        along with a .cfg file.

        In my code, I use
        logging.config. fileConfig('/home/matt/mypackage/matt.cfg') to load in
        the logging config file.

        However, it seems really obvious to me that this won't work when I share
        this package with others.

        I can't figure out what path to use when I load my .cfg file.

        Any ideas?

        Matt
        --

        Comment

        • Matthew Wilson

          #5
          Re: How to package a logging.config file?

          On Mon 14 Jul 2008 09:25:19 AM EDT, Vinay Sajip wrote:
          Is your package a library or an application? If it's a library, you
          should avoid configuring logging using a config file - this is because
          logging configuration is process-wide, and if multiple libraries use
          fileConfig to configure their logging, you may get unexpected results.
          I thought that the point of using logging.getLogg er('foo.bar.baz ') was
          to allow each module/class/function to choose from the available
          configurations.

          So, I can define a really weird logger and still be a good citizen.

          As long as I don't tweak the root logger, is it safe to use a config
          file?

          Matt

          Comment

          Working...