0

I have written a simple python package which has a set of functions that perform simple operations (data manipulation). I am trying to enhance the package and add more functionality for logging, which leads me to this question.

Should I expect the user of the package to pass in a file descriptor or a file handler of the python logging module into the methods of the package, or should the package itself have its own logging module which the methods within the package employ.

I can see benefits (user controls logging and can maintain a flow of function calls based on the same handler) and cons (users logger is not good enough) in both, however what is / are best practices in this case.

user2883071
  • 960
  • 1
  • 20
  • 50

1 Answers1

4

In your module, create a logger object:

import logging
LOGGER = logging.getLogger(__name__)

And then call the appropriate functions on that object:

LOGGER.debug('you probably dont care about this')
LOGGER.info('some info message')
LOGGER.error('whoops, something went wrong')

If the user has configured the logging subsystem correctly, then the messages will automatically go where the user wants them to go (a file, stderr, syslog, etc.)

Community
  • 1
  • 1
John Gordon
  • 29,573
  • 7
  • 33
  • 58
  • 3
    Usually you do `.getLogger(__name__)` to use the current module name instead of `'foo'`. – gilch May 31 '18 at 03:00
  • So the package itself will have its own logger module. And if a destination / handler is not specified by the user of the package (assuming it is not passed into the method being called), it should default to its own internal package logging? Im assuming this would / shoul dbe declared in sort of the instantiation of the package class? – user2883071 May 31 '18 at 11:48