~robru/friends/early-dbus-interface

« back to all changes in this revision

Viewing changes to friends/utils/logging.py

  • Committer: Ken VanDine
  • Date: 2012-10-13 01:27:15 UTC
  • Revision ID: ken.vandine@canonical.com-20121013012715-gxfoi1oo30wdm8dv
initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# friends-service -- send & receive messages from any social network
 
2
# Copyright (C) 2012  Canonical Ltd
 
3
#
 
4
# This program is free software: you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation, version 3 of the License.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 
 
16
"""Logging utilities."""
 
17
 
 
18
import os
 
19
import errno
 
20
import logging
 
21
import logging.handlers
 
22
 
 
23
from gi.repository import GLib
 
24
 
 
25
 
 
26
LOG_FILENAME = os.path.join(
 
27
    os.path.realpath(os.path.abspath(GLib.get_user_cache_dir())),
 
28
    'friends', 'friends.log')
 
29
LOG_FORMAT = '{asctime} - {name:12} {threadName:14}: {levelname:8} - {message}'
 
30
CSL_FORMAT = '{name:12} {threadName:12}: {levelname:8} {message}'
 
31
 
 
32
 
 
33
def initialize(console=False, debug=False, filename=None):
 
34
    """Initialize the Friends service logger.
 
35
 
 
36
    :param console: Add a console logger.
 
37
    :type console: bool
 
38
    :param debug: Set the log level to DEBUG instead of INFO.
 
39
    :type debug: bool
 
40
    :param filename: Alternate file to log messages to.
 
41
    :type filename: string
 
42
    """
 
43
    # Start by ensuring that the directory containing the log file exists.
 
44
    if filename is None:
 
45
        filename = LOG_FILENAME
 
46
    try:
 
47
        os.makedirs(os.path.dirname(filename))
 
48
    except OSError as error:
 
49
        if error.errno != errno.EEXIST:
 
50
            raise
 
51
    log = logging.getLogger('friends.service')
 
52
    if debug:
 
53
        log.setLevel(logging.DEBUG)
 
54
    else:
 
55
        log.setLevel(logging.INFO)
 
56
    # Install a rotating log file handler.  XXX There should be a
 
57
    # configuration file rather than hard-coded values.
 
58
    text_handler = logging.handlers.RotatingFileHandler(
 
59
        filename, maxBytes=20971520, backupCount=5)
 
60
    # Use str.format() style format strings.
 
61
    text_formatter = logging.Formatter(LOG_FORMAT, style='{')
 
62
    text_handler.setFormatter(text_formatter)
 
63
    log.addHandler(text_handler)
 
64
    if console:
 
65
        console_handler = logging.StreamHandler()
 
66
        console_formatter = logging.Formatter(CSL_FORMAT, style='{')
 
67
        console_handler.setFormatter(console_formatter)
 
68
        log.addHandler(console_handler)