| 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 | |
|---|
| 22 | import os |
|---|
| 23 | |
|---|
| 24 | from logging import Logger, StreamHandler, FileHandler, Formatter |
|---|
| 25 | from umitCore.UmitOptionParser import option_parser |
|---|
| 26 | |
|---|
| 27 | LOGLEVEL = option_parser.get_verbose() |
|---|
| 28 | |
|---|
| 29 | class 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! |
|---|
| 57 | log = Log("Umit", LOGLEVEL) |
|---|
| 58 | |
|---|
| 59 | # or this |
|---|
| 60 | def 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 \ |
|---|
| 73 | log." % file_output) |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | if __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 | |
|---|