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.

3
  • Logging should be there as a separate utility. So that, you can simply initiate the logging from any other files/functions and maintain the log file. Commented May 31, 2018 at 2:08
  • Yes.. but would this added functionality be part of the package.. or should it be sent in by the user who is using the package? Commented May 31, 2018 at 2:24
  • I actually follow the way when we build this utility in in own package. Commented May 31, 2018 at 3:35

1 Answer 1

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.)

Sign up to request clarification or add additional context in comments.

2 Comments

Usually you do .getLogger(__name__) to use the current module name instead of 'foo'.
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?

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.