2

I am new to python and just trying to learn and find better ways to write code. I want to create a custom class for logging and use package logging inside it. I want the function in this class to be reusable and do my logging from other scripts rather than writing custom code in each and every scripts. Is there a good link you guys can share? Is this the right way to handle logging? I just want to avoid writing the same code in every script if I can reuse it from one module. I would highly appreciate any reply.

1
  • 1
    Why not just use the builtin logger? A google search for "Python logging" should be all you need. Commented Apr 5, 2019 at 1:41

2 Answers 2

2

You can build a custom class that utilizes the built in python logging library. There isn't really any right way to handle logging as the library allows you to use 5 standard levels indicating the severity of events (DEBUG, INFO, WARNING, ERROR, and CRITICAL). The way you use these levels are application specific. Here's another good explanation of the package.

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

Comments

1

It's indeed a good idea to keep all your logging configuration (formatters, level, handlers) in one place.

  • create a class wrapping a custom logger with your configuration

  • expose methods for logging with different levels

  • import this class wherever you want

  • create an instance of this class to log where you want

To make sure all you custom logging objects have the same config, you should make logging class own the configuration.

I don't think there's any links I can share for the whole thing but you can find links for the individual details I mentioned easily enough.

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.