Project

General

Profile

LogFileClass » History » Version 2

Refik Hadzialic, 07/08/2011 09:28 PM

1 1 Refik Hadzialic
h1. Description of the logging class
2 1 Refik Hadzialic
3 1 Refik Hadzialic
h2. Logging class (LogFileClass.py)
4 1 Refik Hadzialic
5 1 Refik Hadzialic
!http://lab.ks.uni-freiburg.de/attachments/download/246/logging.png!
6 1 Refik Hadzialic
7 1 Refik Hadzialic
h3. Creating the Logging object [Logging(logFileName)]
8 1 Refik Hadzialic
9 1 Refik Hadzialic
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. 
10 1 Refik Hadzialic
11 1 Refik Hadzialic
h3. Logging an event or error in the log file [logEvent(event)] 
12 1 Refik Hadzialic
13 1 Refik Hadzialic
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:
14 1 Refik Hadzialic
> ------------------STARTED THE LOGGING current_data_and_time ------------------ 
15 1 Refik Hadzialic
> e.g. ------------------STARTED THE LOGGING 2011-07-07 18:26:57.338129 ------------------
16 1 Refik Hadzialic
17 1 Refik Hadzialic
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)
18 1 Refik Hadzialic
19 1 Refik Hadzialic
h3. Closing the log file [closeLogging()]
20 1 Refik Hadzialic
21 2 Refik Hadzialic
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.
22 1 Refik Hadzialic
23 1 Refik Hadzialic
> ------------------FINISHED THE LOGGING current_data_and_time ------------------ 
24 1 Refik Hadzialic
25 1 Refik Hadzialic
h3. Code example for logging using the LogFileClass
26 1 Refik Hadzialic
27 1 Refik Hadzialic
Here you can see an example how to use the class:
28 1 Refik Hadzialic
29 1 Refik Hadzialic
<pre>
30 1 Refik Hadzialic
31 1 Refik Hadzialic
import LogFileClass
32 1 Refik Hadzialic
33 1 Refik Hadzialic
logger = LogFileClass.Logging('handlerOfSystem.log')
34 1 Refik Hadzialic
logger.logEvent('') #first time log is used to make the start header 
35 1 Refik Hadzialic
36 1 Refik Hadzialic
#imagine here some code and an error happened
37 1 Refik Hadzialic
error = 'The Star Trek ship failed to start: 432'
38 1 Refik Hadzialic
39 1 Refik Hadzialic
logger.logEvent(error)
40 1 Refik Hadzialic
41 1 Refik Hadzialic
#closing log the file now
42 1 Refik Hadzialic
logger.closeLogging()
43 1 Refik Hadzialic
44 1 Refik Hadzialic
</pre>
45 1 Refik Hadzialic
46 1 Refik Hadzialic
h3. Code of the LogFileClass
47 1 Refik Hadzialic
48 1 Refik Hadzialic
In the repository you can find the code, inside of the LogFileClass.py 
49 1 Refik Hadzialic
50 1 Refik Hadzialic
h3. How to read the log file?
51 1 Refik Hadzialic
52 1 Refik Hadzialic
One can easily read the log file by opening it. The log file will look similar to this:
53 1 Refik Hadzialic
54 1 Refik Hadzialic
<pre>
55 1 Refik Hadzialic
------------------STARTED THE LOGGING 2011-07-07 18:16:56.893179 ------------------
56 1 Refik Hadzialic
On: 2011-07-07 18:16:59.374600  Event: Tried to open socket: 1
57 1 Refik Hadzialic
On: 2011-07-07 18:16:59.374653  Event: Tried to close the socket: 1
58 1 Refik Hadzialic
59 1 Refik Hadzialic
60 1 Refik Hadzialic
------------------STARTED THE LOGGING 2011-07-07 18:24:50.521193 ------------------
61 1 Refik Hadzialic
</pre>
62 1 Refik Hadzialic
63 1 Refik Hadzialic
We can easily see here, since there is no FINISHED THE LOGGING line that the application crashed or was terminated by the user.