~mvo/ubuntu-sso-client/strawman-lp711413

631.2.1 by natalia.bidart at canonical
Added encoding declaration on every file. Fixed error passing from GUI in GTK
1
# -*- coding: utf-8 -*-
2
#
536.1.1 by natalia.bidart at canonical
Making rename of ubuntu.sso to ubuntu_sso to avoid package name clashing.
3
# ubuntu_sso.logger - logging miscellany
2 by Rodney Dawes
Import the code
4
#
5
# Author: Stuart Langridge <stuart.langridge@canonical.com>
611.2.1 by natalia.bidart at canonical
Removed all the old auth mechanism (LP: #616121).
6
# Author: Natalia B. Bidart <natalia.bidart@canonical.com>
2 by Rodney Dawes
Import the code
7
#
8
# Copyright 2009 Canonical Ltd.
9
#
10
# This program is free software: you can redistribute it and/or modify it
11
# under the terms of the GNU General Public License version 3, as published
12
# by the Free Software Foundation.
13
#
14
# This program is distributed in the hope that it will be useful, but
15
# WITHOUT ANY WARRANTY; without even the implied warranties of
16
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
17
# PURPOSE.  See the GNU General Public License for more details.
18
#
19
# You should have received a copy of the GNU General Public License along
20
# with this program.  If not, see <http://www.gnu.org/licenses/>.
554.1.7 by natalia.bidart at canonical
Hoepfully the backend is now binded for real.
21
2 by Rodney Dawes
Import the code
22
"""Miscellaneous logging functions."""
554.1.7 by natalia.bidart at canonical
Hoepfully the backend is now binded for real.
23
557.1.2 by natalia.bidart at canonical
Handled UserRegistrationError.
24
import logging
2 by Rodney Dawes
Import the code
25
import os
557.1.2 by natalia.bidart at canonical
Handled UserRegistrationError.
26
import sys
27
2 by Rodney Dawes
Import the code
28
import xdg.BaseDirectory
29
239.3.1 by Rodney Dawes
Rotate the oauth log and cap at 1MB
30
from logging.handlers import RotatingFileHandler
31
630.2.1 by Natalia B. Bidart
Started refactor for expandible Dbus APIs.
32
611.2.9 by natalia.bidart at canonical
Cleanup of variable names as per lint output.
33
LOGFOLDER = os.path.join(xdg.BaseDirectory.xdg_cache_home, 'sso')
2 by Rodney Dawes
Import the code
34
# create log folder if it doesn't exists
35
if not os.path.exists(LOGFOLDER):
36
    os.makedirs(LOGFOLDER)
37
611.2.4 by natalia.bidart at canonical
Improved logging.
38
if os.environ.get('DEBUG'):
39
    LOG_LEVEL = logging.DEBUG
40
else:
41
    # Only log this level and above
42
    LOG_LEVEL = logging.INFO
239.3.1 by Rodney Dawes
Rotate the oauth log and cap at 1MB
43
630.2.1 by Natalia B. Bidart
Started refactor for expandible Dbus APIs.
44
MAIN_HANDLER = RotatingFileHandler(os.path.join(LOGFOLDER, 'sso-client.log'),
45
                                   maxBytes=1048576,
46
                                   backupCount=5)
47
MAIN_HANDLER.setLevel(LOG_LEVEL)
48
FMT = "%(asctime)s:%(msecs)s - %(name)s - %(levelname)s - %(message)s"
49
MAIN_HANDLER.setFormatter(logging.Formatter(fmt=FMT))
50
554.1.3 by natalia.bidart at canonical
More pep8 fixes.
51
611.2.1 by natalia.bidart at canonical
Removed all the old auth mechanism (LP: #616121).
52
def setup_logging(log_domain):
630.2.1 by Natalia B. Bidart
Started refactor for expandible Dbus APIs.
53
    """Create basic logger to set filename."""
239.3.1 by Rodney Dawes
Rotate the oauth log and cap at 1MB
54
    logger = logging.getLogger(log_domain)
55
    logger.propagate = False
569.1.1 by natalia.bidart at canonical
Trying to fix glade data file.
56
    logger.setLevel(LOG_LEVEL)
630.2.1 by Natalia B. Bidart
Started refactor for expandible Dbus APIs.
57
    logger.addHandler(MAIN_HANDLER)
557.1.2 by natalia.bidart at canonical
Handled UserRegistrationError.
58
    if os.environ.get('DEBUG'):
59
        debug_handler = logging.StreamHandler(sys.stderr)
667.1.1 by natalia.bidart at canonical
The service should shutdown when unused (LP: #701606).
60
        debug_handler.setFormatter(logging.Formatter(fmt=FMT))
557.1.2 by natalia.bidart at canonical
Handled UserRegistrationError.
61
        logger.addHandler(debug_handler)
62
239.3.1 by Rodney Dawes
Rotate the oauth log and cap at 1MB
63
    return logger