source: TI12-security/trunk/python/ndg.security.server/ndg/security/server/Log.py @ 1638

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/python/ndg.security.server/ndg/security/server/Log.py@1785
Revision 1638, 2.5 KB checked in by pjkersha, 13 years ago (diff)

Moved client and server side specific modules to their respective client and server packages.

  • Property svn:executable set to *
Line 
1"""NDG Logging class
2
3NERC Data Grid Project
4
5P J Kershaw 10/05/06
6
7Copyright (C) 2006 CCLRC & NERC
8
9This software may be distributed under the terms of the Q Public License,
10version 1.0 or later.
11"""
12
13reposID = '$Id$'
14
15import logging
16from logging.handlers import *
17
18# Inherit directly from Logger
19_logSuperClass = logging.getLoggerClass()
20
21
22#_____________________________________________________________________________
23class LogError(Exception):
24    """Exception handling for NDG Logging class."""
25   
26    def __init__(self, msg):
27        self.__msg = msg
28         
29    def __str__(self):
30        return self.__msg
31
32
33#_____________________________________________________________________________
34class Log(_logSuperClass):
35    """NDG Logging class"""
36   
37    __msgFmt = '%(asctime)s %(name)s: %(levelname)-8s %(message)s'
38    __dateFmt = '%d %b %Y %H:%M:%S'
39   
40    # Log file size limit and number of backups saved
41    __maxBytes = 1048576
42    __backUpCnt = 10
43   
44    def __init__(self, logName='', logFilePath=None, console=False):
45        """NDG Logging class
46       
47        logName:        provide a log name
48        logFilePath:    if set, write to a log file given by the path
49        console:        Set to True to send output to the stderr"""
50
51        # Inherit from Logger class
52        _logSuperClass.__init__(self, logName, level=logging.DEBUG)
53                           
54       
55        # Set a format for messages
56        formatter = logging.Formatter(fmt=self.__msgFmt, 
57                                      datefmt=self.__dateFmt)
58
59
60        # Handler set to write to INFO messages or higher to the sys.stderr
61        if console:
62            console = logging.StreamHandler()
63            console.setLevel(logging.INFO)
64       
65            # Set the handler to use format set earlier
66            console.setFormatter(formatter)
67   
68            # Add the handler to this log object
69            self.addHandler(console)
70       
71       
72        # Log file set with rotating file handler.  When log size > than
73        # maxBytes, a new log file is started.  Up to backupCount are created
74        # before the first is overwritten
75        if logFilePath:
76            fileLog = RotatingFileHandler(logFilePath, 
77                                          maxBytes=self.__maxBytes, 
78                                          backupCount=self.__backUpCnt)
79            fileLog.setFormatter(formatter)
80           
81            # Nb. log file includes debug messages
82            fileLog.setLevel(logging.DEBUG)
83           
84            self.addHandler(fileLog)
Note: See TracBrowser for help on using the repository browser.