~barry/ubuntu-system-image/citrain302

« back to all changes in this revision

Viewing changes to systemimage/logging.py

  • Committer: CI bot
  • Date: 2014-07-23 23:07:29 UTC
  • mfrom: (232.1.6 citrain231)
  • Revision ID: ps-jenkins@lists.canonical.com-20140723230729-efe2ckz8xxqdt5uu
* New upstream release.
  - LP: #1207860 - Support factory resets.  system-image-cli
    --factory-reset and a new D-Bus API method FactoryReset() are added.
  - LP: #1262256 - Data file checksums are passed to
    ubuntu-download-manager where available.
  - LP: #1286542 - Certain duplicate destinations are allowed, if they
    have matching source urls and checksums.
  - LP: #1301995 - When system-image-{cli,dbus} is run as non-root, use
    a fallback location for the log file if the system log file isn't
    writable.
  - LP: #1251291 - system-image-cli --list-channels lists all the
    available channels, including aliases.
  - LP: #1279028 - system-image-cli --no-reboot downloads all files and
    prepares for recovery, but does not actually issue a reboot.
  - LP: #1249347 - system-image-cli --switch <channel> is a convenient
    alias for system-image-cli -b 0 -c <channel>.
  - LP: #1294273 - Added --show-settings, --get, --set, and --del
    options for viewing, changing, and setting all the internal database
    settings.
  - LP: #1271684 - Improve memory usage when verifying file checksums.
    Given by Michael Vogt.
  - LP: #1274131 - In the UpdatePaused signal, return a percentage value
    that's closer to reality than hardcoding it to 0.
  - LP: #1280169 - New D-Bus API method .Information() which is like
    .Info() except that it returns extended information details, as a
    mapping of strings to strings.  These details include a
    last_check_date which is the ISO 8601 timestamp of the last time an
    UpdateAvailableStatus signal was sent.
  - LP: #1339157 - Set the GSM flag in ubuntu-download-manager based on
    the current s-i download setting.
  - LP: #1340882 - The system-image-dbus(8) manpage now describes the
    full D-Bus API.
  - LP: #1273354 - Fix the D-Bus mock service so that the downloading
    flag for UpdateAvailableStatus will correctly return true when
    checking twice under manual downloads.
  - LP: #1342183 - Pay down some tech-debt.
* d/watch, d/upstream/signing-key.asc: Added Barry's GPG signing key so
  that uscan will verify the signature of the download.
* d/control: Updated Build-Depends.
* d/rules:
  - Updated, and add --buildsystem=pybuild.
  - Fix 'nocheck' test short-circuiting.
* d/tests:
  - control: Update dependencies and restrictions.  The smoketest test
    should not include the system-image-dev package, for a more
    realistic simulation of the installed enviroment.
  - dryrun: New schroot-compatible limited test suite.  The existing
    smoketest test requires isolation-container so isn't compatible with
    schroot.
  - smoketest-noreboot: Added full update test, with no reboot.
* New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
    ]
22
22
 
23
23
 
24
 
import os
25
24
import sys
26
25
import stat
27
26
import logging
28
27
 
29
 
from contextlib import contextmanager
 
28
from contextlib import contextmanager, suppress
 
29
from pathlib import Path
30
30
from systemimage.config import config
31
 
from systemimage.helpers import makedirs
 
31
from systemimage.helpers import DEFAULT_DIRMODE
 
32
from xdg.BaseDirectory import xdg_cache_home
32
33
 
33
34
 
34
35
DATE_FMT = '%b %d %H:%M:%S %Y'
67
68
            return super().getMessage()
68
69
 
69
70
 
 
71
def _make_handler(path):
 
72
    # issue21539 - mkdir(..., exist_ok=True)
 
73
    with suppress(FileExistsError):
 
74
        path.parent.mkdir(DEFAULT_DIRMODE, parents=True)
 
75
    path.touch(LOGFILE_PERMISSIONS)
 
76
    # Our handler will output in UTF-8 using {} style logging.
 
77
    return logging.FileHandler(bytes(path), encoding='utf-8')
 
78
 
 
79
 
70
80
def initialize(*, verbosity=0):
71
81
    """Initialize the loggers."""
72
82
    level = {
80
90
    logging.setLogRecordFactory(FormattingLogRecord)
81
91
    # Now configure the application level logger based on the ini file.
82
92
    log = logging.getLogger('systemimage')
83
 
    # Make sure the log directory exists.
84
 
    makedirs(os.path.dirname(config.system.logfile))
85
 
    # touch(1) - but preserve in case file already exists.
86
 
    with open(config.system.logfile, 'a', encoding='utf-8'):
87
 
        pass
88
 
    os.chmod(config.system.logfile, LOGFILE_PERMISSIONS)
89
 
    # Our handler will output in UTF-8 using {} style logging.
90
 
    handler = logging.FileHandler(config.system.logfile, encoding='utf-8')
 
93
    try:
 
94
        handler = _make_handler(Path(config.system.logfile))
 
95
    except PermissionError:
 
96
        handler = _make_handler(
 
97
            Path(xdg_cache_home) / 'system-image' / 'client.log')
91
98
    handler.setLevel(level)
92
99
    formatter = logging.Formatter(style='{', fmt=MSG_FMT, datefmt=DATE_FMT)
93
100
    handler.setFormatter(formatter)