source: TI03-DataExtractor/trunk/pydxs/LogManager.py @ 794

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI03-DataExtractor/trunk/pydxs/LogManager.py@794
Revision 794, 2.7 KB checked in by astephen, 13 years ago (diff)

Unstable but latest version with multi-variable support and split hooks
for CDML and CSML.

Line 
1#   Copyright (C) 2004 CCLRC & NERC( Natural Environment Research Council ).
2#   This software may be distributed under the terms of the
3#   Q Public License, version 1.0 or later. http://ndg.nerc.ac.uk/public_docs/QPublic_license.txt
4
5"""
6LogManager.py
7=============
8
9Holds the LogManager class which records successes and failures
10in log files.
11
12Note the syntax of logs is as follows:
13
14errors:
15-------
16
17<DATE> <TIME>:<sessionID>:<userID>:<python_traceback>
18
19requests:
20---------
21
22<DATE> <TIME>:<sessionID>:<userID>:<datasetGroups>:<datasets>:<variables>:\
23              <outputFormat>:<numberOfOutputFiles>:<totalVolumeOfFiles>
24
25"""
26
27# Import library modules
28import os
29import sys
30import time
31import logging
32
33# Import package modules
34from serverConfig import *
35
36class LogManager:
37    """
38    Class to control logging of errors and successful requests.
39    """
40   
41    def __init__(self, request):
42        """
43        Sets instance attributes and logs.
44        """ 
45        self.requestObj=request
46        self.errorLog=logging.Logger("errorLog")
47        errorLogFile=logging.FileHandler(ERROR_LOG)
48        self.errorLog.addHandler(errorLogFile)
49       
50        self.requestLog=logging.Logger("completionLog")
51        requestLogFile=logging.FileHandler(REQUEST_LOG)
52        self.requestLog.addHandler(requestLogFile)
53       
54
55    def logError(self, error):
56        """
57        Logs an error string to the error log.
58        """
59        dt=self._getDateTime()
60        r=self.requestObj
61        errorString="%s:%s:%s:%s" % (dt, r["sessionID"], r["username"], error)
62        self.errorLog.log(errorString)
63   
64
65    def logCompletedRequest(self, outputFilePaths):
66        """
67        Logs a completed request. In the format:
68        <DATE> <TIME>:<sessionID>:<userID>:<datasetGroups>:<datasets>:<variables>:\
69              <outputFormat>:<numberOfOutputFiles>:<totalVolumeOfFiles>
70        """
71        dt=self._getDateTime()
72        r=self.requestObj
73        ndatasets=r["numberOfDatasets"]
74        logString="%s:%s:%s" % (dt, r["sessionID"], r["username"])
75       
76        for i in ("datasetGroup", "dataset", "variable"):
77            tempString=""
78            for n in range(1, ndatasets+1):
79                if tempString=="":
80                    comma=""
81                else:
82                    comma=","
83                tempString=tempString+comma+r["%s_%s" % (i,n)]
84            logString=logString+":"+tempString
85           
86           
87        numberOfFiles=len(outputFilePaths)
88        try:
89            fileVolumes=0
90            for path in outputFilePaths:
91                fileVolumes=fileVolumes+os.stat(path)[6]
92        except:
93            fileVolumes="NOT CAPTURED YET!"   
94        logString="%s:%s:%s:%s" % (logString, r["outputFormat"], numberOfFiles, fileVolumes)                         
95        self.requestLog.log(5, logString)       
96       
97
98    def _getDateTime(self):
99        """
100        Returns a date-time string.
101        """
102        return time.strftime("%Y-%m-%d %H:%M", time.localtime(time.time()))
Note: See TracBrowser for help on using the repository browser.