~ubuntu-branches/ubuntu/lucid/ubuntuone-client/lucid-proposed

« back to all changes in this revision

Viewing changes to ubuntuone/syncdaemon/logger.py

  • Committer: Bazaar Package Importer
  • Author(s): Rodney Dawes
  • Date: 2009-09-28 18:15:00 UTC
  • mfrom: (1.1.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20090928181500-jz7i72znd1nwap4r
Tags: 1.0.0-0ubuntu1
* New upstream release.
  - Limit log messages, file size, and rotate more often (LP: #435137)
  - Rotate oauth-login.log and cap the file size (LP: #445514)
  - Remove pycurl usage in favor of urllib and better errors (LP: #411029)
  - Save the syncdaemon settings for bw limiting (LP: #418882)
  - Fix NoSuchDatabase when pairing desktopcouch (LP: #438411)
  - Fix IOError when launching preferences window (LP: #441039)
  - Update icons to remove Ubuntu logo, better names (LP: #434886)
  - Use ngettext for the possibly singular notification (LP: #449269)
  - Only create Places bookmark when first authorizing (LP: #397749)
  - Don't always recreate the bookmark (LP: #401211)
  - Handle showing emblems better (LP: #440839)
  - Remove the System->Preferences menu item (LP: #443342)
  - Remove NotOnlineError usage (LP: #404550)
  - Set a default share name in the Nautilus sharing UI (LP: #369488)
  - Simplify some state handling (LP: #420354)

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
import re
26
26
import weakref
27
27
import zlib
 
28
import functools
28
29
import xdg.BaseDirectory
29
30
 
30
31
from itertools import imap
33
34
# extra levels
34
35
# be more verbose than logging.DEBUG(10)
35
36
TRACE = 5
36
 
# info that we always want to log (logging.ERROR+1)
 
37
# info that we almost always want to log (logging.ERROR - 1)
37
38
NOTE = logging.ERROR - 1
38
39
 
39
40
# map names to the extra levels
60
61
 
61
62
 
62
63
class DayRotatingFileHandler(TimedRotatingFileHandler):
63
 
    """ A TimedRotatingFileHandler configured for Day rotation but that uses
64
 
    the suffix and extMatch of Hourly rotation, in order to allow seconds based
65
 
    rotation on each startup.
 
64
    """A mix of TimedRotatingFileHandler and RotatingFileHandler configured for
 
65
    daily rotation but that uses the suffix and extMatch of Hourly rotation, in
 
66
    order to allow seconds based rotation on each startup.
 
67
    The log file is also rotated when the specified size is reached.
66
68
    """
67
69
 
68
70
    def __init__(self, *args, **kwargs):
69
 
        """ create the instance and override the suffix and extMatch. """
 
71
        """ create the instance and override the suffix and extMatch.
 
72
        Also accepts a maxBytes keyword arg to rotate the file when it reachs
 
73
        maxBytes.
 
74
        """
70
75
        kwargs['when'] = 'D'
71
76
        kwargs['backupCount'] = LOGBACKUP
72
77
        # check if we are in 2.5, only for PQM
73
78
        if sys.version_info[:2] >= (2, 6):
74
79
            kwargs['delay'] = 1
 
80
        if 'maxBytes' in kwargs:
 
81
            self.maxBytes = kwargs.pop('maxBytes')
 
82
        else:
 
83
            self.maxBytes = 0
75
84
        TimedRotatingFileHandler.__init__(self, *args, **kwargs)
76
85
        # override suffix
77
86
        self.suffix = "%Y-%m-%d_%H-%M-%S"
78
87
        self.extMatch = re.compile(r"^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}$")
79
88
 
 
89
    def shouldRollover(self, record):
 
90
        """
 
91
        Determine if rollover should occur.
 
92
 
 
93
        Basically, see if TimedRotatingFileHandler.shouldRollover and if it's
 
94
        False see if the supplied record would cause the file to exceed
 
95
        the size limit we have.
 
96
 
 
97
        The size based rotation are from logging.handlers.RotatingFileHandler
 
98
        """
 
99
        if TimedRotatingFileHandler.shouldRollover(self, record):
 
100
            return 1
 
101
        else:
 
102
            # check the size
 
103
            if self.stream is None:                 # delay was set...
 
104
                self.stream = self._open()
 
105
            if self.maxBytes > 0:                   # are we rolling over?
 
106
                msg = "%s\n" % self.format(record)
 
107
                self.stream.seek(0, 2)  #due to non-posix-compliant Windows feature
 
108
                if self.stream.tell() + len(msg) >= self.maxBytes:
 
109
                    return 1
 
110
            return 0
 
111
 
80
112
 
81
113
# pylint: disable-msg=C0103
82
114
class mklog(object):
285
317
# a constant to change the default DEBUG level value
286
318
_DEBUG_LOG_LEVEL = logging.DEBUG
287
319
 
 
320
 
 
321
# partial config of the handler to rotate when the file size is 1MB
 
322
CustomRotatingFileHandler = functools.partial(DayRotatingFileHandler,
 
323
                                              maxBytes=1048576)
 
324
 
288
325
# root logger
289
326
root_logger = logging.getLogger("ubuntuone.SyncDaemon")
290
327
root_logger.propagate = False
291
328
root_logger.setLevel(_DEBUG_LOG_LEVEL)
292
 
root_handler = DayRotatingFileHandler(filename=LOGFILENAME)
 
329
root_handler = CustomRotatingFileHandler(filename=LOGFILENAME)
293
330
root_handler.addFilter(logging.Filter("ubuntuone.SyncDaemon"))
294
331
root_handler.setFormatter(basic_formatter)
295
332
root_handler.setLevel(_DEBUG_LOG_LEVEL)
296
333
root_logger.addHandler(root_handler)
297
334
# exception logs
298
 
exception_handler = DayRotatingFileHandler(filename=EXLOGFILENAME)
 
335
exception_handler = CustomRotatingFileHandler(filename=EXLOGFILENAME)
299
336
exception_handler.setFormatter(basic_formatter)
300
337
exception_handler.setLevel(logging.ERROR)
301
338
# add the exception handler to the root logger
310
347
twisted_logger = logging.getLogger('twisted')
311
348
twisted_logger.propagate = False
312
349
twisted_logger.setLevel(logging.ERROR)
313
 
twisted_handler = DayRotatingFileHandler(filename=LOGFILENAME)
 
350
twisted_handler = CustomRotatingFileHandler(filename=LOGFILENAME)
314
351
twisted_handler.addFilter(logging.Filter("twisted"))
315
352
twisted_handler.setFormatter(basic_formatter)
316
353
twisted_handler.setLevel(logging.ERROR)
343
380
        logfile = os.path.join(LOGFOLDER, 'syncdaemon-debug.log')
344
381
        root_handler.baseFilename = os.path.abspath(logfile)
345
382
        twisted_handler.baseFilename = os.path.abspath(logfile)
 
383
        # don't cap the file size
 
384
        root_handler.maxBytes = 0
 
385
        twisted_handler.maxBytes = 0
346
386
    for name in ['ubuntuone.SyncDaemon', 'twisted']:
347
387
        logger = logging.getLogger(name)
348
388
        logger.setLevel(_DEBUG_LOG_LEVEL)