root/branch/ggpolo/umitCore/Logging.py @ 1254

Revision 1254, 2.5 kB (checked in by ggpolo, 6 years ago)

Set 5 minutes refresh as default to TLGraph. Added file_log to Logging, so Scheduler uses this to write log messages to scheduler.log

Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4# Copyright (C) 2005 Insecure.Com LLC.
5#
6# Author: Adriano Monteiro Marques <py.adriano@gmail.com>
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22import os
23
24from logging import Logger, StreamHandler, FileHandler, Formatter
25from umitCore.UmitOptionParser import option_parser
26
27LOGLEVEL = option_parser.get_verbose()
28
29class Log(Logger, object):
30    def __init__(self, name, level=0, file_output=None):
31        Logger.__init__(self, name, level)
32        self.formatter = self.format
33
34        if file_output:
35            handler = FileHandler(file_output)
36        else:
37            handler = StreamHandler()
38
39        handler.setFormatter(self.formatter)
40       
41        self.addHandler(handler)
42       
43    def get_formatter(self):
44        return self.__formatter
45
46    def set_formatter(self, fmt):
47        self.__formatter = Formatter(fmt)
48
49
50    format = "%(levelname)s - %(asctime)s - %(message)s"
51   
52    formatter = property(get_formatter, set_formatter, doc="")
53    __formatter = Formatter(format)
54
55
56# Import this!
57log = Log("Umit", LOGLEVEL)
58
59# or this
60def file_log(file_output):
61    """
62    Returns an Log instance that writes to file_output.
63    Sets LOGLEVEL to 50, so every message is written.
64    """
65    if os.path.isfile(file_output):
66        return Log("Umit", 0, file_output)
67    else:
68        try:
69            open(file_output, 'a')
70            return Log("Umit", 0, file_output)
71        except IOError:
72            raise Exception("Bad file output '%s' especified for saving \
73log." % file_output)
74
75
76if __name__ == '__main__':
77    log.debug("Debug Message")
78    log.info("Info Message")
79    log.warning("Warning Message")
80    log.error("Error Message")
81    log.critical("Critical Message")
82
83    log = file_log("myoutput.log")
84    log.debug("Debug Message")
85    log.info("Info Message")
86    log.warning("Warning Message")
87    log.error("Error Message")
88    log.critical("Critical Message")
89
Note: See TracBrowser for help on using the browser.