~ubuntu-branches/ubuntu/intrepid/prewikka/intrepid

« back to all changes in this revision

Viewing changes to prewikka/Log.py

  • Committer: Bazaar Package Importer
  • Author(s): Pierre Chifflier
  • Date: 2007-04-11 14:41:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070411144109-2hh7zx3amwd27b4l
Tags: upstream-0.9.10
ImportĀ upstreamĀ versionĀ 0.9.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2004,2005 PreludeIDS Technologies. All Rights Reserved.
 
2
# Author: Yoann Vandoorselaere <yoann.v@prelude-ids.com>
 
3
#
 
4
# This file is part of the Prewikka program.
 
5
#
 
6
# This program is free software; you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation; either version 2, or (at your option)
 
9
# any later version.
 
10
#
 
11
# This program is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with this program; see the file COPYING.  If not, write to
 
18
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 
19
 
 
20
 
 
21
import logging, logging.handlers, sys
 
22
 
 
23
DEBUG = logging.DEBUG
 
24
INFO = logging.INFO
 
25
ERROR = logging.ERROR
 
26
WARNING = logging.WARNING
 
27
CRITICAL = logging.CRITICAL
 
28
 
 
29
class Log:
 
30
    def __init__(self, conf):        
 
31
        self._logger = None
 
32
        
 
33
        for logconf in conf.logs:
 
34
            logtype = logconf.keys()[0]
 
35
 
 
36
            config = { }
 
37
            for key in logconf[logtype].keys():
 
38
                config[key] = logconf[logtype].getOptionValue(key)
 
39
 
 
40
            self._logger = logging.getLogger()
 
41
            self._logger.setLevel(logging.NOTSET)
 
42
            self._logger.addHandler(self._getHandler(config, logtype))
 
43
 
 
44
        
 
45
    def _getHandler(self, config, logtype='syslog'):
 
46
        logtype = logtype.lower()
 
47
        level = config.get("level", "")
 
48
 
 
49
        if logtype == 'file':
 
50
            hdlr = logging.FileHandler(config["file"])
 
51
 
 
52
        elif logtype == 'nteventlog':
 
53
            hdlr = logging.handlers.NTEventLogHandler(logid, logtype='Application')
 
54
 
 
55
        elif logtype in ['syslog', 'unix']:
 
56
            hdlr = logging.handlers.SysLogHandler('/dev/log')
 
57
 
 
58
        elif logtype in ['smtp']:
 
59
            hdlr = logging.handlers.SMTPHandler(config["host"], config["from"], config["to"].split(", "), config["subject"]) 
 
60
 
 
61
        elif logtype in ['stderr']:
 
62
            hdlr = logging.StreamHandler(sys.stderr)
 
63
 
 
64
        else:
 
65
            raise _("Unknown logtype specified: '%s'") % logtype
 
66
 
 
67
        format = 'Prewikka %(levelname)s: %(message)s'
 
68
        if logtype in ['file', 'stderr']:
 
69
            format = '%(asctime)s ' + format 
 
70
 
 
71
        datefmt = ''
 
72
        if logtype == 'stderr':
 
73
            datefmt = '%X'        
 
74
 
 
75
        level = level.upper()
 
76
        if level in ['DEBUG', 'ALL']:
 
77
            hdlr.setLevel(logging.DEBUG)
 
78
        elif level == 'INFO':
 
79
            hdlr.setLevel(logging.INFO)
 
80
        elif level == 'ERROR':
 
81
            hdlr.setLevel(logging.ERROR)
 
82
        elif level == 'CRITICAL':
 
83
            hdlr.setLevel(logging.CRITICAL)
 
84
        else:
 
85
            hdlr.setLevel(logging.WARNING)
 
86
        
 
87
        formatter = logging.Formatter(format, datefmt)
 
88
        hdlr.setFormatter(formatter)
 
89
 
 
90
        return hdlr
 
91
 
 
92
 
 
93
    def _getLog(self, request, login, details):
 
94
        message = "["
 
95
        
 
96
        addr = request.getClientAddr()
 
97
        message += "%s" % (addr)
 
98
 
 
99
        port = request.getClientPort()
 
100
        if port:
 
101
            message += ":%d" % port
 
102
 
 
103
        if login:
 
104
            message += " %s@" % (login)
 
105
        else:
 
106
            message += " "
 
107
            
 
108
        message += "%s]" % (request.getView())
 
109
 
 
110
        if details:
 
111
            message += " " + details
 
112
 
 
113
        return message
 
114
        
 
115
    def debug(self, message, request=None, user=None):
 
116
        if self._logger:
 
117
            self._logger.debug(self._getLog(request, user, message))
 
118
 
 
119
    def info(self, message, request=None, user=None):
 
120
        if self._logger:
 
121
            self._logger.info(self._getLog(request, user, message))
 
122
    
 
123
    def warning(self, message, request=None, user=None):
 
124
        if self._logger:
 
125
            self._logger.warning(self._getLog(request, user, message))
 
126
 
 
127
    def error(self, message, request=None, user=None):
 
128
        if self._logger:
 
129
            self._logger.error(self._getLog(request, user, message))
 
130
 
 
131
    def critical(self, message, request=None, user=None):
 
132
        if self._logger:
 
133
            self._logger.critical(self._getLog(request, user, message))
 
134
 
 
135
    def log(self, priority, message, request=None, user=None):
 
136
        return { DEBUG: self.debug,
 
137
                 INFO: self.info,
 
138
                 WARNING: self.warning,
 
139
                 ERROR: self.error,
 
140
                 CRITICAL: self.critical }[priority](message, request, user)
 
141