~statik/magicicada-gui/include-license

« back to all changes in this revision

Viewing changes to magicicada/logger.py

  • Committer: natalia.bidart at canonical
  • Author(s): facundo
  • Date: 2010-06-05 15:57:34 UTC
  • mfrom: (38.1.1 log-to-file)
  • Revision ID: natalia.bidart@canonical.com-20100605155734-01brtuptrxj2ngyq
Logs are not directed to files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# logger.py
 
2
#
 
3
# Author: Facundo Batista <facundo@taniquetil.com.ar>
 
4
#
 
5
# Copyright 2010 Chicharreros
 
6
#
 
7
# This program is free software: you can redistribute it and/or modify it
 
8
# under the terms of the GNU General Public License version 3, as published
 
9
# by the Free Software Foundation.
 
10
#
 
11
# This program is distributed in the hope that it will be useful, but
 
12
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
13
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
14
# PURPOSE.  See the GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License along
 
17
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 
 
19
"""Logging set up."""
 
20
 
 
21
 
 
22
import logging
 
23
import os
 
24
 
 
25
from logging.handlers import RotatingFileHandler
 
26
 
 
27
import xdg.BaseDirectory
 
28
 
 
29
 
 
30
class CustomRotatingFH(RotatingFileHandler):
 
31
    """Rotating handler that starts a new file for every run."""
 
32
 
 
33
    def __init__(self, *args, **kwargs):
 
34
        RotatingFileHandler.__init__(self, *args, **kwargs)
 
35
        self.doRollover()
 
36
 
 
37
 
 
38
def set_up():
 
39
    """Set up the logging."""
 
40
 
 
41
    # choose the folder to store the logs
 
42
    cache = xdg.BaseDirectory.xdg_cache_home
 
43
    logfolder = os.path.join(cache, 'magicicada')
 
44
    if not os.path.exists(logfolder):
 
45
        os.makedirs(logfolder)
 
46
    logfile = os.path.join(logfolder, 'magicicada.log')
 
47
 
 
48
    logger = logging.getLogger('magicicada')
 
49
    handler = CustomRotatingFH(logfile, maxBytes=1e6, backupCount=10)
 
50
    logger.addHandler(handler)
 
51
    formatter = logging.Formatter("%(asctime)s %(name)-23s"
 
52
                                  "%(levelname)-8s %(message)s",
 
53
                                  '%Y-%m-%d %H:%M:%S')
 
54
    handler.setFormatter(formatter)
 
55
    logger.setLevel(logging.DEBUG)
 
56