SQLSERVERDAWG
SQLSERVERDAWG

Reputation: 67

Common logging module in Python

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.

Upvotes: 2

Views: 2445

Answers (2)

nathancy
nathancy

Reputation: 46600

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.

Upvotes: 2

rdas
rdas

Reputation: 21275

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.

Upvotes: 1

Related Questions