~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/canonical/launchpad/scripts/logger.py

  • Committer: Steve Kowalik
  • Date: 2011-08-07 04:05:52 UTC
  • mto: This revision was merged to the branch mainline in revision 13626.
  • Revision ID: stevenk@ubuntu.com-20110807040552-mwnxo0flmhvl35e8
Correct the notification based on review comments, and remove request{,ed}
from the function names, switching to create{,d}.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 
6
6
"""Logging setup for scripts.
7
7
 
8
 
Don't import from this module. Import it from lp.services.scripts.
 
8
Don't import from this module. Import it from canonical.launchpad.scripts.
 
9
 
 
10
Parts of this may be moved into canonical.launchpad somewhere if it is
 
11
to be used for non-script stuff.
9
12
"""
10
13
 
11
14
__metaclass__ = type
31
34
 
32
35
from contextlib import contextmanager
33
36
from cStringIO import StringIO
34
 
from datetime import timedelta
 
37
from datetime import (
 
38
    timedelta,
 
39
    )
35
40
import hashlib
36
41
import logging
37
42
from logging.handlers import WatchedFileHandler
45
50
from zope.component import getUtility
46
51
from zope.exceptions.log import Formatter
47
52
 
48
 
from lp.services.config import config
49
 
from lp.services.librarian.interfaces.client import (
 
53
from canonical.config import config
 
54
from canonical.launchpad.webapp.errorlog import (
 
55
    globalErrorUtility,
 
56
    ScriptRequest,
 
57
    )
 
58
from canonical.librarian.interfaces import (
50
59
    ILibrarianClient,
51
60
    UploadFailed,
52
61
    )
55
64
    compress_hash,
56
65
    utc_now,
57
66
    )
58
 
from lp.services.webapp.errorlog import (
59
 
    globalErrorUtility,
60
 
    ScriptRequest,
61
 
    )
62
67
 
63
68
# Reexport our custom loglevels for old callsites. These callsites
64
69
# should be importing the symbols from lp.services.log.loglevels
85
90
    def emit(self, record):
86
91
        """Emit a record as an OOPS."""
87
92
        try:
88
 
            info = record.exc_info
89
 
            if info is None:
90
 
                info = sys.exc_info()
91
93
            msg = record.getMessage()
 
94
            # Warnings and less are informational OOPS reports.
 
95
            informational = (record.levelno <= logging.WARN)
92
96
            with globalErrorUtility.oopsMessage(msg):
93
 
                globalErrorUtility.raising(info, self.request)
 
97
                globalErrorUtility._raising(
 
98
                    sys.exc_info(), self.request, informational=informational)
94
99
        except Exception:
95
100
            self.handleError(record)
96
101
 
265
270
    True
266
271
 
267
272
    Cleanup:
268
 
    >>> from lp.testing import reset_logging
 
273
    >>> from canonical.testing import reset_logging
269
274
    >>> reset_logging()
270
275
 
271
276
    As part of the options parsing, the 'log' global variable is updated.
346
351
 
347
352
    Cleanup:
348
353
 
349
 
    >>> from lp.testing import reset_logging
 
354
    >>> from canonical.testing import reset_logging
350
355
    >>> reset_logging()
351
356
    """
352
357
    if options is None:
432
437
class _LogWrapper:
433
438
    """Changes the logger instance.
434
439
 
435
 
    Other modules will do 'from lp.services.scripts import log'.
 
440
    Other modules will do 'from canonical.launchpad.scripts import log'.
436
441
    This wrapper allows us to change the logger instance these other modules
437
442
    use, by replacing the _log attribute. This is done each call to logger()
438
443
    """