A Simple Logger Class : Class Constructor « Class « Python






A Simple Logger Class

A Simple Logger Class
import time

class Logger:
    def __init__(self, filename):
        self.filename = filename
    def __call__(self, string):
        file = open(self.filename, 'a')
        file.write('[' + time.asctime() + '] ')
        file.write(string + '\n')
        file.close()

log = Logger('logfile.txt')


log('Starting program')

log('Trying to divide 1 by 0')

print 1 / 0

log('The division succeeded')

log('Ending program')

           
       








Related examples in the same category

1.Empty Class constructor
2.Constructors DemoConstructors Demo
3.A class may define a special method named __init__()A class may define a special method named __init__()
4.A very simple class with a constructor.A very simple class with a constructor.