Project

General

Profile

Description of the logging class

Logging class (LogFileClass.py)

Creating the Logging object [Logging(logFileName)]

At the start you have to create the object of the Logging class. We can do that by executing the following code, logger = LogFileClass.Logging(logFileName), where logFileName is the name of the file where you want to log or store some events/errors/etc... The file will be automatically created if it doesn't exist, if it exists then each event you write to it will be stored in the next line.

Logging an event or error in the log file [logEvent(event)]

Once you have created the logging object you can start writing events, errors or whatever you want to the file. You can log an event by using the logEvent(event) function, where event must be a string of your event (e.g. logEvent('Error number: 344')). The first time the command is used, it writes an header:

------------------STARTED THE LOGGING current_data_and_time ------------------
e.g. ------------------STARTED THE LOGGING 2011-07-07 18:26:57.338129 ------------------

The purpose of the header is that you can easier look up for some events in the log file and that you know when the class was created (i.e. when the script/program was started)

Closing the log file [closeLogging()]

When your program successfully finishes(i.e. without a crash) one should close the log file properly. This can be done using the following command closeLogging(). The logging object will write in the log file that it was closed successfully.

------------------FINISHED THE LOGGING current_data_and_time ------------------

Code example for logging using the LogFileClass

Here you can see an example how to use the class:


import LogFileClass

logger = LogFileClass.Logging('handlerOfSystem.log')
logger.logEvent('') #first time log is used to make the start header 

#imagine here some code and an error happened
error = 'The Star Trek ship failed to start: 432'

logger.logEvent(error)

#closing log the file now
logger.closeLogging()

Code of the LogFileClass

In the repository you can find the code, inside of the LogFileClass.py

How to read the log file?

One can easily read the log file by opening it. The log file will look similar to this:

------------------STARTED THE LOGGING 2011-07-07 18:16:56.893179 ------------------
On: 2011-07-07 18:16:59.374600  Event: Tried to open socket: 1
On: 2011-07-07 18:16:59.374653  Event: Tried to close the socket: 1

------------------STARTED THE LOGGING 2011-07-07 18:24:50.521193 ------------------

We can easily see here, since there is no FINISHED THE LOGGING line that the application crashed or was terminated by the user.