Back to index

salome-kernel  6.5.0
Public Member Functions | Private Attributes
KERNEL_PY.kernel.logger.Logger Class Reference
Inheritance diagram for KERNEL_PY.kernel.logger.Logger:
Inheritance graph
[legend]

List of all members.

Public Member Functions

def __init__
def showDebug
def setLogFile
def setColor
def closeLogFile
def hideDebug
def fatal

Private Attributes

 _baseFormatString
 _baseFormatter
 _stdoutStream
 _stdoutHandler
 _fileHandler

Detailed Description

This class formats and displays log messages in Salome environment. It
inherits :class:`Logger<logging.Logger>` class defined in :mod:`logging`
module from Python library, so all methods from :class:`logging.Logger`
can be used here. The format of the traces is:

LEVEL    [keyword] : Message

where `LEVEL` is the level of the message (`DEBUG`, `INFO`, etc.),
`keyword` is the name of the logger, and `Message` is the message to log.

When creating a new Logger object, the parameter `keyword` defines the
name of the logger, `level` defines the logging level (default is
:const:`logging.DEBUG` if KERNEL module is configured with --enable-debug
option or :const:`logging.WARNING` otherwise), and `color` defines the color
of the log messages for this logger (log messages will appear in color
only when displayed on color-capable ASCII terminals). See module
:mod:`salome.kernel.termcolor` for the color constants.

By default, log messages will be displayed only on standard output. They
can also be recorded in a file (see method :meth:`setLogFile`). For now,
the CORBA-based logging facility can not be used through this class.

A source filename `sourceFileName` can be defined. If this argument is
specified, then the `keyword` is modified to the basename of the `sourceFileName`

Basic usage::

    from salome.kernel.logger import Logger
    log = Logger("Test")
    log.debug("Debug message")
    log.info("Information message")
    log.warning("Warning message")
    log.error("Error message")
    log.critical("Fatal error message")

Definition at line 40 of file logger.py.


Constructor & Destructor Documentation

def KERNEL_PY.kernel.logger.Logger.__init__ (   self,
  keyword = "KEY",
  level = salome.kernel.logconfig.loggingLevel,
  color = None,
  sourceFileName = None 
)

Reimplemented in KERNEL_PY.kernel.logger.ExtLogger.

Definition at line 80 of file logger.py.

00080 
00081                  color = None, sourceFileName=None):
00082 
00083         if sourceFileName is not None:
00084             keyword = os.path.basename(sourceFileName).split('.')[0]
00085         logging.Logger.__init__(self, keyword, level)
00086         self._baseFormatString = "%(levelname)-8s [%(name)s] : %(message)s"
00087         self._baseFormatter = logging.Formatter(self._baseFormatString)
00088         if hasattr(sys.stdout, "flush"):
00089             self._stdoutStream = sys.stdout
00090         else:
00091             self._stdoutStream = _UnFlushableLogStream(sys.stdout)
00092         self._stdoutHandler = logging.StreamHandler(self._stdoutStream)
00093         self._stdoutHandler.setLevel(logging.DEBUG)
00094         self.setColor(color)
00095         self.addHandler(self._stdoutHandler)
00096         self._fileHandler = None


Member Function Documentation

Close the log file.

Definition at line 130 of file logger.py.

00130 
00131     def closeLogFile(self):
00132         """Close the log file."""
00133         if self._fileHandler is not None:
00134             self.removeHandler(self._fileHandler)
00135             self._fileHandler.close()
00136             self._fileHandler = None

Here is the caller graph for this function:

def KERNEL_PY.kernel.logger.Logger.fatal (   self,
  message 
)
Log a message with CRITICAL level. This method only exists for
backward compatibility and is equivalent to ``critical(message)``.

Definition at line 145 of file logger.py.

00145 
00146     def fatal(self, message):
00147         """
00148         Log a message with CRITICAL level. This method only exists for
00149         backward compatibility and is equivalent to ``critical(message)``.
00150         """
00151         self.critical(message)
00152 

Hide DEBUG level messages (equivalent to ``setLevel(logging.INFO)``).

Definition at line 137 of file logger.py.

00137 
00138     def hideDebug(self):
00139         """
00140         Hide DEBUG level messages (equivalent to ``setLevel(logging.INFO)``).
00141         """
00142         self.setLevel(logging.INFO)

Here is the call graph for this function:

def KERNEL_PY.kernel.logger.Logger.setColor (   self,
  color 
)
Set the color of log messages on color-capable terminals. If `color`
is :const:`None`, the default color will be used.

Definition at line 115 of file logger.py.

00115 
00116     def setColor(self, color):
00117         """
00118         Set the color of log messages on color-capable terminals. If `color`
00119         is :const:`None`, the default color will be used.
00120         """
00121         if color is None or not termcolor.canDisplayColor(self._stdoutStream):
00122             stdoutFormatter = self._baseFormatter
00123         else:
00124             format = ("%s%s%s" %
00125                       (termcolor.getControlSequence(color),
00126                        self._baseFormatString,
00127                        termcolor.getControlSequence(termcolor.DEFAULT)))
00128             stdoutFormatter = logging.Formatter(format)
00129         self._stdoutHandler.setFormatter(stdoutFormatter)

def KERNEL_PY.kernel.logger.Logger.setLogFile (   self,
  logFilename 
)
Define a log file to record the log messages (in addition to the
standard output).

Definition at line 104 of file logger.py.

00104 
00105     def setLogFile(self, logFilename):
00106         """
00107         Define a log file to record the log messages (in addition to the
00108         standard output).
00109         """
00110         self.closeLogFile()
00111         self._fileHandler = logging.FileHandler(logFilename, 'w')
00112         self._fileHandler.setLevel(logging.DEBUG)
00113         self._fileHandler.setFormatter(self._baseFormatter)
00114         self.addHandler(self._fileHandler)

Here is the call graph for this function:

Log all messages, including DEBUG level messages (equivalent to
``setLevel(logging.DEBUG)``).

Definition at line 97 of file logger.py.

00097 
00098     def showDebug(self):
00099         """
00100         Log all messages, including DEBUG level messages (equivalent to
00101         ``setLevel(logging.DEBUG)``).
00102         """
00103         self.setLevel(logging.DEBUG)


Member Data Documentation

Definition at line 85 of file logger.py.

Definition at line 86 of file logger.py.

Definition at line 95 of file logger.py.

Definition at line 91 of file logger.py.

Definition at line 88 of file logger.py.


The documentation for this class was generated from the following file: