A better logging option.

Back in January 2013 i wrote: Create random id's and easily writing logs. This expands on the logging aspect, which I found problematic over time.

As time goes on we improve our knowledge in our subject of choice. Lately I have been spending a lot of time in Python, improving techniques, making things easier to use..etc.

Way back when, I created an article about logging to a file. It seemed like a good idea at the time, until you deal with hundreds of log entries, then it can become hdd intensive to constantly open and close a file.

So I picked up using Python's logging library, and I wrote a mini class that initalizes everything, takes care of log folders..etc.

A note before continuing. The logging library offers a lot more options that this little class does. For example it offers WARN, CRITICAL and other various logging levels. I've written it strictly for INFO and DEBUG. This class also prints out the associated log data as it's called, that way you don't need to add print's eveywhere to view data.

Usage:

My typical usage is, INFO message before a loop to advise what is about to happen, and then DEBUG displaying whatever data is being handled within the loop.

I store the class below in logsystem.py so my usage below goes based on that.

import logsystem

log = logsystem.logSystem('custom-file-name', 'DEBUG')

log.info('Some type of information message')

log.debug('More detailed information about what is happening')

# Pretend I have connected to MySQL here
log.info('Grabbing Users Data')
cursor.execute("SELECT name, rank FROM users")
for user in cursor.fetchall():
log.debug('Found: {}'.format(user.name))

# Do something with the user data.

logSystem Class:

import os
import logging


class logSystem:

    def __init__(self, fileName='errors', level='INFO', directory='logs/', printAll=True, requestsLogging=False):
        self.fileName = fileName
        self.directory = directory
        self.level = level
        self.printAll = printAll
        self.fullFilePath = '{}{}.log'.format(self.directory, self.fileName)

# Ensure the log directory exists if not os.path.isdir(directory): os.mkdir(directory) # Set and Check debugging levels if self.level == 'DEBUG': loggingLevel = logging.DEBUG elif self.level == 'INFO': loggingLevel = logging.INFO else: loggingLevel = logging.NOTSET logging.disable(logging.INFO) self.logging = logging.basicConfig( filename=self.fullFilePath, format='%(asctime)s - %(levelname)s - %(message)s', level=loggingLevel)
# If you use the requests library like I do, it will create log entries for EVERY http call you make
# This option shuts that off. if not requestsLogging: # Disable requests logging requests_log = logging.getLogger("requests") requests_log.setLevel(logging.NOTSET) requests_log.propagate = False def info(self, data): print data if self.level != 'OFF': logging.info(data) def debug(self, data): if self.printAll: print data if self.level == 'DEBUG': logging.debug(data)

Print