source: TI04-geosplat/trunk/pygss/LogManager.py @ 798

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI04-geosplat/trunk/pygss/LogManager.py@6809
Revision 798, 2.1 KB checked in by astephen, 14 years ago (diff)

Latest working version with install method.
Can accept more than one file but doesn't combine variables yet.

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>:<inputFiles>:<variables>:\
23              <fileFormat>:<graphicalOutputType>
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, sessionObject):
42        """
43        Sets instance attributes and logs.
44        """ 
45        self.bag=sessionObject
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.bag
61        errorString="%s:%s:%s:%s" % (dt, r["sessionID"], r["username"], error)
62        self.errorLog.log(errorString)
63   
64
65    def logCompletedRequest(self, outputFilePath):
66        """
67        Logs a completed request. In the format:
68        <DATE> <TIME>:<sessionID>:<userID>:<inputFiles>:<variables>:\
69              <fileFormat>:<graphicalOutputType>
70        """
71        dt=self._getDateTime()
72        r=self.bag
73
74        logString="%s:%s:%s" % (dt, r["sessionID"], r["username"])
75        logString="%s:%s" % (logString, r["fileFormat"])                         
76        self.requestLog.log(5, logString)       
77       
78
79    def _getDateTime(self):
80        """
81        Returns a date-time string.
82        """
83        return time.strftime("%Y-%m-%d %H:%M", time.localtime(time.time()))
Note: See TracBrowser for help on using the repository browser.