[bonfiredemokit] Annotation of /virt-cluster/vc/logger.py
Annotation of /virt-cluster/vc/logger.py
Parent Directory
| Revision Log
Revision 1 -
(view)
(download)
(as text)
1 : |
agomez |
1 |
# -*- coding: utf-8 -*-
|
2 : |
|
|
import logging.handlers
|
3 : |
|
|
|
4 : |
|
|
DEFAULT_FORMAT = "%(asctime)s - %(module)s:%(lineno)d - %(levelname)s - %(message)s"
|
5 : |
|
|
|
6 : |
|
|
class NullHandler(logging.Handler):
|
7 : |
|
|
def emit(self, record):
|
8 : |
|
|
pass
|
9 : |
|
|
|
10 : |
|
|
def configure(logfile = None, debug = False, console = False):
|
11 : |
|
|
log.setLevel(logging.DEBUG)
|
12 : |
|
|
formatter = logging.Formatter(DEFAULT_FORMAT)
|
13 : |
|
|
if logfile != None:
|
14 : |
|
|
rotate = logging.handlers.RotatingFileHandler(logfile,maxBytes=1048576,backupCount=2)
|
15 : |
|
|
rotate.setFormatter(formatter)
|
16 : |
|
|
rotate.setLevel(logging.INFO)
|
17 : |
|
|
if debug:
|
18 : |
|
|
rotate.setLevel(logging.DEBUG)
|
19 : |
|
|
log.addHandler(rotate)
|
20 : |
|
|
|
21 : |
|
|
if console:
|
22 : |
|
|
cli = logging.StreamHandler()
|
23 : |
|
|
cli.setFormatter(formatter)
|
24 : |
|
|
cli.setLevel(logging.INFO)
|
25 : |
|
|
if debug:
|
26 : |
|
|
cli.setLevel(logging.DEBUG)
|
27 : |
|
|
log.addHandler(cli)
|
28 : |
|
|
|
29 : |
|
|
|
30 : |
|
|
|
31 : |
|
|
log = logging.getLogger()
|
32 : |
|
|
log.addHandler(NullHandler())
|
|