Package x2go :: Module log

[frames] | no frames]

# Source Code for Module x2go.log

      1  # -*- coding: utf-8 -*-
      2
      3  # Copyright (C) 2010-2013 by Mike Gabriel <mike.gabriel@das-netzwerkteam.de>
      4  #
      5  # Python X2Go is free software; you can redistribute it and/or modify
      6  # it under the terms of the GNU Affero General Public License as published by
      7  # the Free Software Foundation; either version 3 of the License, or
      8  # (at your option) any later version.
      9  #
     10  # Python X2Go is distributed in the hope that it will be useful,
     11  # but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13  # GNU Affero General Public License for more details.
     14  #
     15  # You should have received a copy of the GNU Affero General Public License
     16  # along with this program; if not, write to the
     17  # Free Software Foundation, Inc.,
     18  # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
     19
     20  """\
     21  X2GoLogger class - flexible handling of log and debug output.
     22
     23  """
     24  __NAME__ = 'x2gologger-pylib'
     25
     26  # modules
     27  import os
     28  import sys
     29  import types
     30
     31  loglevel_NONE = 0
     32  loglevel_ERROR = 8
     33  loglevel_WARN = 16
     34  loglevel_NOTICE = 32
     35  loglevel_INFO = 64
     36  loglevel_DEBUG = 128
     37  loglevel_DEBUG_SFTPXFER = 1024
     38
     39  loglevel_DEFAULT = loglevel_ERROR | loglevel_WARN | loglevel_NOTICE
     40  """\
     41  Default loglevel of X2GoLogger objects is: NOTICE & WARN & ERROR
     42  """
     43
     44  # Python X2Go modules
     45  import utils
     46
     47 -class X2GoLogger(object):
     48      """\
     49      A simple logger class, that is used by all Python X2Go classes.
     50
     51      """
     52      name = ''
     53      tag = ''
     54      progpid = -1
     55      level = -1
     56      destination = sys.stderr
     57
     58      _loglevel_NAMES = {8: 'error',
     59                     16: 'warn',
     60                     32: 'notice',
     61                     64: 'info',
     62                     128: 'debug',
     63                     1024: 'debug-sftpxfer',
     64                    }
     65
     66 -    def __init__(self, name=sys.argv[0], loglevel=loglevel_DEFAULT, tag=None):
     67          """\
     68          @param name: name of the programme that uses Python X2Go
     69          @type name: C{str}
     70          @param loglevel: log level for Python X2Go
     71          @type loglevel: C{int}
     72          @param tag: additional tag for all log entries
     73          @type tag: C{str}
     74
     75          """
     76          self.name = os.path.basename(name)
     77          self.tag = tag
     78          self.loglevel = loglevel
     79          self.progpid = os.getpid()
     80
     81 -    def message(self, msg, loglevel=loglevel_NONE, tag=None):
     82          """\
     83          Log a message.
     84
     85          @param msg: log message text
     86          @type msg: C{str}
     87          @param loglevel: log level of this message
     88          @type loglevel: C{int}
     89          @param tag: additional tag for this log entry
     90          @type tag: C{str}
     91
     92          """
     93          if tag is None:
     94              tag = self.tag
     95          if loglevel & self.loglevel:
     96
     97              msg = msg.replace('\n', ' ')
     98              msg = msg.encode(utils.get_encoding())
     99
    100              if self.tag is not None:
    101                  self.destination.write('%s[%s] (%s) %s: %s\n' % (self.name, self.progpid, tag, self._loglevel_NAMES[loglevel].upper(), msg))
    102              else:
    103                  self.destination.write('%s[%s] %s: %s\n' % (self.name, self.progpid, self._loglevel_NAMES[loglevel].upper(), msg))
    104      __call__ = message
    105
    106 -    def get_loglevel(self):
    107          """\
    108          Get the current loglevel.
    109
    110          @return: current log level
    111          @rtype: C{int}
    112
    113          """
    114          return self.loglevel
    115
    116 -    def set_loglevel(self, loglevel_name='none'):
    117          """\
    118          Set log level by name.
    119
    120          @param loglevel_name: name of loglevel to be set
    121          @type loglevel_name: C{str}
    122
    123          """
    124          if type(loglevel_name) is types.IntegerType:
    125              self.loglevel = loglevel_name
    126          elif type(loglevel_name) is types.StringType and loglevel_name in self._loglevel_NAMES.values():
    127              _method = getattr(self, 'self.set_loglevel_%s' % loglevel_name)
    128              _method()
    129          else:
    130              self.loglevel = loglevel_DEFAULT
    131
    132 -    def set_loglevel_quiet(self):
    133          """\
    134          Silence logging completely.
    135
    136          """
    137          self.loglevel = 0
    138
    139 -    def set_loglevel_error(self):
    140          """\
    141          Set log level to I{ERROR}.
    142
    143          """
    144          self.loglevel = loglevel_ERROR
    145
    146 -    def set_loglevel_warn(self):
    147          """\
    148          Set log level to I{WARN}.
    149
    150          """
    151          self.loglevel = loglevel_ERROR | loglevel_WARN
    152
    153 -    def set_loglevel_notice(self):
    154          """\
    155          Set log level to I{NOTICE} (default).
    156
    157          """
    158          self.loglevel = loglevel_ERROR | loglevel_WARN | loglevel_NOTICE
    159
    160 -    def set_loglevel_info(self):
    161          """\
    162          Set log level to I{INFO}.
    163
    164          """
    165          self.loglevel = loglevel_ERROR | loglevel_WARN | loglevel_NOTICE | loglevel_INFO
    166
    167 -    def set_loglevel_debug(self):
    168          """\
    169          Set log level to I{DEBUG}.
    170
    171          """
    172          self.loglevel = loglevel_ERROR | loglevel_WARN | loglevel_NOTICE | loglevel_INFO | loglevel_DEBUG
    173
    174 -    def enable_debug_sftpxfer(self):
    175          """\
    176          Additionally, switch on sFTP data transfer debugging
    177
    178          """
    179          self.loglevel = self.loglevel | loglevel_DEBUG_SFTPXFER
    180
    181 -    def disable_debug_sftpxfer(self):
    182          """\
    183          Switch off sFTP data transfer debugging.
    184
    185          """
    186          self.loglevel = self.loglevel ^ loglevel_DEBUG_SFTPXFER
    187
    188  # compat section
    189  X2goLogger = X2GoLogger
    190