~facundo/encuentro/trunk

« back to all changes in this revision

Viewing changes to server/srv_logger.py

  • Committer: Facundo Batista
  • Date: 2013-06-01 18:55:04 UTC
  • mto: This revision was merged to the branch mainline in revision 178.
  • Revision ID: facundo@taniquetil.com.ar-20130601185504-i3ygczru97g9b45m
Better logging.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf8 -*-
 
2
 
 
3
# Copyright 2013 Facundo Batista
 
4
#
 
5
# This program is free software: you can redistribute it and/or modify it
 
6
# under the terms of the GNU General Public License version 3, as published
 
7
# by the Free Software Foundation.
 
8
#
 
9
# This program is distributed in the hope that it will be useful, but
 
10
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
11
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
12
# PURPOSE.  See the GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License along
 
15
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
#
 
17
# For further info, check  https://launchpad.net/encuentro
 
18
 
 
19
"""Set up the logs."""
 
20
 
 
21
import logging
 
22
import os
 
23
import time
 
24
 
 
25
LOG_DIR = "logs"
 
26
 
 
27
 
 
28
def setup_log(shy):
 
29
    """Log setup."""
 
30
    _rootlogger = logging.getLogger("")
 
31
    _rootlogger.setLevel(logging.DEBUG)
 
32
    formatter = logging.Formatter('%(asctime)s %(levelname)7s '
 
33
                                  '%(name)s: %(message)s')
 
34
 
 
35
    # stdout
 
36
    _handler = logging.StreamHandler()
 
37
    level = logging.WARNING if shy else logging.DEBUG
 
38
    _handler.setLevel(level)
 
39
    _rootlogger.addHandler(_handler)
 
40
    _handler.setFormatter(formatter)
 
41
 
 
42
    # file
 
43
    if not os.path.exists(LOG_DIR):
 
44
        os.makedirs(LOG_DIR)
 
45
    fname = time.strftime(os.path.join(LOG_DIR, "encserver-%Y%m.log"))
 
46
    _handler = logging.FileHandler(fname)
 
47
    _handler.setLevel(logging.DEBUG)
 
48
    _rootlogger.addHandler(_handler)
 
49
    _handler.setFormatter(formatter)