This module is called with a string message and it is written to a file with a date and timestamp. After a certain number of entries, it creates a backup of the file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | """ logger.py
This module is responsible for controlling the action logging for the
application.
"""
"""
Author: Johan Geldenhuys
"""
##
#Imports
################################################################################
import os, time, threading
#Constants
################################################################################
""" Tuple containing the backup filename extensions"""
OLD_FILE_TUP = ('.1bak','.2bak','.3bak','.4bak')
STR_TIME_FORMAT = '%d/%m/%Y %H:%M:%S'
CR = '\n'
TIMESTAMP = 'TIMESTAMP: '
SPACE = ' '
#Classes
################################################################################
class ActionLogManager (object):
""" Script action log storage manager class
"""
##
#Constructor
############################################################################
def __init__ (self):
""" LogStorageManager constructor
Initialising the LogStorageManager class
:Parameters:
-
:Returns:
- None
"""
##
#Private Members
########################################################################
self._actionLogName = ('ActivityLogFile')
self._logHandle = None
self._maxEntries = 20
self._maxEntryCount = 0
self._logStr = None
self._threadObject = None
self._threadFlag = False
""" check if the log file exists. If yes read the entries and update the
entry counter."""
if os.path.exists(self._actionLogName):
""" The log file exists already """
logFile = open(self._actionLogName, 'r')
""" Read the number of entries from the log file"""
self._maxEntryCount = len(logFile.readlines())
print('No. of entries in Action log file:%d' %\
self._maxEntryCount )
""" Close it"""
logFile.close()
""" Flag which will be usd to determine that the application is shutting down """
self._isRunning = True
print('LogStorageManager Class Initialised..')
##
#Methods
############################################################################
def writeActionLog(self):
""" This method just logs the logStr into log-action-file and increments
the entry-count
:Parameters:
- None
:Returns:
- None
:Raises:
-`FileHandlingException` : Raised when the script encounters an
IOError.
"""
try:
""" Opening the file in append mode and writing to it """
self._logHandle = open(self._actionLogName, 'a+')
self._logHandle.write(self._logStr)
""" Incrementing the entry count """
self._maxEntryCount += 1
print('The action log string written to the file')
self._logHandle.close()
except IOError:
print('ActionLogFile could not be written to.')
############################################################################
def logMessage(self, msg):
""" This method will log the data together with time stamp.
The eventual checking, file transfer and backup will also be done by
this method.
:Parameters:
- `msg` : Contains the text message.
:Returns:
- None.
:Raises:
- `FileHandlingException` : Raised when file operation fails.
"""
""" Getting the Time-Stamp """
timeStr = time.strftime(STR_TIME_FORMAT, time.localtime())
string = msg
self._logStr = TIMESTAMP + timeStr + SPACE + string + CR
print self._logStr
""" Calling the writeActionLog method """
self.writeActionLog()
""" Checking for Maximum entries """
if( self._maxEntryCount >= self._maxEntries):
""" If the maximum entries is reached start the thread"""
try:
if self._threadFlag is not True:
""" Creating a new thread object """
self._threadObject = WriteThread(self)
""" Starting the new thread """
self._threadObject.start()
""" Sleeping so that the thread runs """
time.sleep(1)
else:
print('Log file already resized, Write thread active !!')
except Exception :
printException()
############################################################################
def threadRun (self):
"""run method of the thread
This method will run as a separate thread which will handle the file overwrite function.
:Parameters:
- None
:Returns:
- None
Setting the threadFlag so that any accidental duplicate spawning doesnt occur.
"""
self._threadFlag = True
print('Maximum entry count exceeded. Starting the '\
+ 'Write thread..')
try:
try:
""" Checks whether the files exist """
if(os.path.exists(self._actionLogName)):
""" if 4th bak file exists remove it """
if(os.path.exists(self._actionLogName + OLD_FILE_TUP[3])):
os.remove(self._actionLogName + OLD_FILE_TUP[3])
print('Removing %s file' \
%self._actionLogName + OLD_FILE_TUP[3])
""" Renaming the bak files to accomodate the new file """
for index in range(2, -1, -1):
if(os.path.exists(self._actionLogName \
+ OLD_FILE_TUP[index])):
os.rename(self._actionLogName + OLD_FILE_TUP[index],
self._actionLogName + OLD_FILE_TUP[index + 1])
os.rename(self._actionLogName, self._actionLogName \
+ OLD_FILE_TUP[0])
except IOError:
printException()
""" Making the entry count to 0 """
self._maxEntryCount = 0
finally:
""" Clearing the threadFlag when the thread ends """
self._threadFlag = False
############################################################################
def stop (self):
""" method for stopping the WriteThread
:Parameters:
- None
:Returns:
- None
"""
""" Calling the stop method of the thread """
print('Stopping ActionLogManager ..')
if (self._threadFlag == True):
""" Notify the Write thread that it needs to be stopped """
self._isRunning = False
self._threadObject.join()
################################################################################
class WriteThread (threading.Thread):
""" WriteThread class
This class will overwrite old backup files and rename the other files in
separate threads.
"""
##
#Constructor
############################################################################
def __init__ (self , caller):
""" WriteThread constructor
:Paramters:
- `caller` : Reference to the calling object
:Returns:
- None
"""
""" Calling the constructor of the base class """
threading.Thread.__init__ (self)
self._caller = caller
print('Initialising the WriteThread class ..')
############################################################################
def run (self):
""" run method of the WriteThread class
:Parameters:
- None
:Returns:
- None
"""
print('Inside the run method of the WriteThread')
""" Calling the threadRun method of the LogStorageManager class """
self._caller.threadRun()
################################################################################
|
There's already a standard module... Err... you do know about the standard 'logging' module, don't you...?
What does it do for you?