| 1 | #!/usr/bin/env python |
|---|
| 2 | # -*- coding: utf-8 -*- |
|---|
| 3 | # |
|---|
| 4 | # Copyright (C) 2005-2006 Insecure.Com LLC. |
|---|
| 5 | # Copyright (C) 2007-2008 Adriano Monteiro Marques |
|---|
| 6 | # |
|---|
| 7 | # Author: Adriano Monteiro Marques <adriano@umitproject.org> |
|---|
| 8 | # |
|---|
| 9 | # This program is free software; you can redistribute it and/or modify |
|---|
| 10 | # it under the terms of the GNU General Public License as published by |
|---|
| 11 | # the Free Software Foundation; either version 2 of the License, or |
|---|
| 12 | # (at your option) any later version. |
|---|
| 13 | # |
|---|
| 14 | # This program is distributed in the hope that it will be useful, |
|---|
| 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 17 | # GNU General Public License for more details. |
|---|
| 18 | # |
|---|
| 19 | # You should have received a copy of the GNU General Public License |
|---|
| 20 | # along with this program; if not, write to the Free Software |
|---|
| 21 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | from logging import Logger, StreamHandler, Formatter, FileHandler |
|---|
| 25 | from umitCore.UmitOptionParser import option_parser |
|---|
| 26 | |
|---|
| 27 | LOGLEVEL = option_parser.get_verbose() |
|---|
| 28 | |
|---|
| 29 | from umitCore.FirstSettings import GeneralSettingsConf |
|---|
| 30 | gs = GeneralSettingsConf() |
|---|
| 31 | |
|---|
| 32 | class Log(Logger, object): |
|---|
| 33 | def __init__(self, name, level=0): |
|---|
| 34 | Logger.__init__(self, name, level) |
|---|
| 35 | self.formatter = self.format |
|---|
| 36 | if gs.log == "File": |
|---|
| 37 | handler = FileHandler(gs.log_file) |
|---|
| 38 | else: |
|---|
| 39 | handler = StreamHandler() |
|---|
| 40 | handler.setFormatter(self.formatter) |
|---|
| 41 | |
|---|
| 42 | self.addHandler(handler) |
|---|
| 43 | |
|---|
| 44 | def get_formatter(self): |
|---|
| 45 | return self.__formatter |
|---|
| 46 | |
|---|
| 47 | def set_formatter(self, fmt): |
|---|
| 48 | self.__formatter = Formatter(fmt) |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | format = "%(levelname)s - %(asctime)s - %(message)s" |
|---|
| 52 | |
|---|
| 53 | formatter = property(get_formatter, set_formatter, doc="") |
|---|
| 54 | __formatter = Formatter(format) |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | # Import this! |
|---|
| 58 | log = Log("Umit", LOGLEVEL) |
|---|
| 59 | gs = None |
|---|
| 60 | |
|---|
| 61 | if __name__ == '__main__': |
|---|
| 62 | log.debug("Debug Message") |
|---|
| 63 | log.info("Info Message") |
|---|
| 64 | log.warning("Warning Message") |
|---|
| 65 | log.error("Error Message") |
|---|
| 66 | log.critical("Critical Message") |
|---|