summaryrefslogtreecommitdiff
path: root/pypers/europython05/Quixote-2.0/logger.py
blob: f631c57a878b9416bfb22b9a2b6a410f28dc8d73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/quixote/logger.py $
$Id: logger.py 25521 2004-11-04 18:16:18Z nascheme $
"""
import sys
import os
import time
import socket
from quixote.sendmail import sendmail

class DefaultLogger:
    """
    This is the default logger object used by the Quixote publisher.  It
    controls access log and error log behavior.  You may provide your own
    object if you wish to have different behavior.

    Instance attributes:

      access_log : file | None
        file to which every access will be logged.  If None then access
        is not logged.
      error_log : file
        file to which application errors (exceptions caught by Quixote,
        as well as anything printed to stderr by application code) will
        be logged.  Set to sys.stderr by default.
      error_email : string | None
        if set then internal server errors will cause messages to be sent to
        this address
    """
    def __init__(self, access_log=None, error_log=None, error_email=None):
        if access_log:
            self.access_log = open(access_log, 'a', 1)
        else:
            self.access_log = None
        if error_log is None:
            self.error_log = sys.stderr
        else:
            self.error_log = open(error_log, 'a', 1)
        self.error_email = error_email
        sys.stdout = self.error_log # print is handy for debugging

    def log(self, msg):
        """
        Write an message to the error log with a time stamp.
        """
        timestamp = time.strftime("%Y-%m-%d %H:%M:%S",
                                  time.localtime(time.time()))
        self.error_log.write("[%s] %s\n" % (timestamp, msg))

    def log_internal_error(self, error_summary, error_msg):
        """(error_summary: str, error_msg: str)

        error_summary is a single line summary of the internal error, suitable
        for an email subject.  error_msg is a multi-line plaintext message
        describing the error in detail.
        """
        self.log("exception caught")
        self.error_log.write(error_msg)
        if self.error_email:
            sendmail('Quixote Traceback (%s)' % error_summary,
                     error_msg, [self.error_email],
                     from_addr=(self.error_email, socket.gethostname()))

    def log_request(self, request, start_time):
        """Log a request in the access_log file.
        """
        if self.access_log is None:
            return
        if request.session:
            user = request.session.user or "-"
        else:
            user = "-"
        now = time.time()
        seconds = now - start_time
        timestamp = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(now))

        request_uri = request.get_path()
        query = request.get_query()
        if query:
            request_uri += "?" + query
        proto = request.get_environ('SERVER_PROTOCOL')
        self.access_log.write('%s %s %s %d "%s %s %s" %s %r %0.2fsec\n' %
                               (request.get_environ('REMOTE_ADDR'),
                                user,
                                timestamp,
                                os.getpid(),
                                request.get_method(),
                                request_uri,
                                proto,
                                request.response.status_code,
                                request.get_environ('HTTP_USER_AGENT', ''),
                                seconds
                               ))