Commit 24942cb7 authored by Fred Drake's avatar Fred Drake

- zLOG.severity(): Convenience function that converts a value to a

  severity level.  This allows the common names to be used for
  severities, not just integers.  This can make configuration data
  less obscure.

- Refactor initialize():
  get_environment_info() pulls information from the environment
  variables, and this gets passed to initialize_log(), which does
  nothing to determine the source of the configuration data.

- New method initialize_with_config():  Load information not provided
  by the environment from a ZConfig section.  This allows the
  environment to override a config file, and avoids having the
  application deal with the specific information needed to configure
  logging.
parent 4d01f5f7
...@@ -8,12 +8,13 @@ ...@@ -8,12 +8,13 @@
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE # FOR A PARTICULAR PURPOSE.
# #
############################################################################## ##############################################################################
__version__='$Revision: 1.19 $'[11:-2] __version__='$Revision: 1.20 $'[11:-2]
import os, sys, time import os, sys, time
import zLOG
try: try:
import textwrap import textwrap
...@@ -53,30 +54,55 @@ class stupid_log_write: ...@@ -53,30 +54,55 @@ class stupid_log_write:
self.initialize() self.initialize()
def initialize(self): def initialize(self):
global _log_level path, severity = self.get_environment_info()
eget = os.environ.get self.initialize_log(path, severity)
# EVENT_LOG_FILE is the preferred envvar, but we accept def initialize_with_config(self, config):
# STUPID_LOG_FILE also """Initialize logging with information from ZConfig."""
path = eget('EVENT_LOG_FILE') path, severity = self.get_environment_info()
if config is not None:
loginfo = config.getSection("log")
if loginfo is not None:
if path is None: if path is None:
path = eget('STUPID_LOG_FILE') path = loginfo.get("path")
if severity is None:
severity = loginfo.get("level")
self.initialize_log(path, severity)
def initialize_log(self, path, severity):
global _log_level
if path is None: if path is None:
_set_log_dest(None) _set_log_dest(None)
else: elif path:
if path:
_set_log_dest(open(path, 'a')) _set_log_dest(open(path, 'a'))
else: else:
_set_log_dest(sys.stderr) _set_log_dest(sys.stderr)
# EVENT_LOG_SEVERITY is the preferred envvar, but we accept
# STUPID_LOG_SEVERITY also
severity = eget('EVENT_LOG_SEVERITY') or eget('STUPID_LOG_SEVERITY')
if severity: if severity:
_log_level = int(severity) _log_level = zLOG.severity(severity)
else: else:
_log_level = 0 # INFO _log_level = 0 # INFO
def get_environment_info(self):
eget = os.environ.get
# EVENT_LOG_FILE is the preferred envvar, but we accept
# STUPID_LOG_FILE also
path = eget('EVENT_LOG_FILE')
if path is None:
path = eget('STUPID_LOG_FILE')
# EVENT_LOG_SEVERITY is the preferred envvar, but we accept
# STUPID_LOG_SEVERITY also
severity = eget('EVENT_LOG_SEVERITY')
if severity is None:
severity = eget('STUPID_LOG_SEVERITY')
return path, severity
def log(self, subsystem, severity, summary, detail, error): def log(self, subsystem, severity, summary, detail, error):
if _log_dest is None or severity < _log_level: if _log_dest is None or severity < _log_level:
return return
...@@ -112,3 +138,4 @@ class stupid_log_write: ...@@ -112,3 +138,4 @@ class stupid_log_write:
_log = stupid_log_write() _log = stupid_log_write()
log_write = _log.log log_write = _log.log
initialize = _log.initialize initialize = _log.initialize
initialize_with_config = _log.initialize_with_config
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE # FOR A PARTICULAR PURPOSE.
# #
############################################################################## ##############################################################################
...@@ -86,10 +86,10 @@ There is a default event logging facility that: ...@@ -86,10 +86,10 @@ There is a default event logging facility that:
can be overridden with the environment variable EVENT_LOG_SEVERITY can be overridden with the environment variable EVENT_LOG_SEVERITY
""" """
__version__='$Revision: 1.10 $'[11:-2] __version__='$Revision: 1.11 $'[11:-2]
from MinimalLogger import log_write, log_time, severity_string, \ from MinimalLogger import log_write, log_time, severity_string, \
_set_log_dest, initialize _set_log_dest, initialize, initialize_with_config
from traceback import format_exception from traceback import format_exception
# Standard severities # Standard severities
...@@ -102,6 +102,30 @@ WARNING = 100 ...@@ -102,6 +102,30 @@ WARNING = 100
ERROR = 200 ERROR = 200
PANIC = 300 PANIC = 300
_severity_names = {"trace": -300,
"debug": -200,
"blather": -100,
"info": 0,
"problem": 100,
"warning": 100,
"error": 200,
"panic": 300,
}
def severity(value):
"""Return the severity level associated with a value.
The value may be an integer, the repr of an integer, or the name
of a well-known severity value (case-insensitive).
"""
try:
return int(value)
except ValueError:
try:
return _severity_names[value.lower()]
except KeyError:
raise ValueError("unknown severity value: " + repr(value))
def LOG(subsystem, severity, summary, detail='', error=None, reraise=None): def LOG(subsystem, severity, summary, detail='', error=None, reraise=None):
"""Log some information """Log some information
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE # FOR A PARTICULAR PURPOSE.
# #
############################################################################## ##############################################################################
...@@ -16,6 +16,10 @@ import os ...@@ -16,6 +16,10 @@ import os
import sys import sys
import tempfile import tempfile
import unittest import unittest
from cStringIO import StringIO
import ZConfig
import zLOG import zLOG
severity_string = { severity_string = {
...@@ -60,7 +64,7 @@ class StupidLogTest(unittest.TestCase): ...@@ -60,7 +64,7 @@ class StupidLogTest(unittest.TestCase):
def setLog(self, severity=0): def setLog(self, severity=0):
os.environ['%s_LOG_FILE' % self.prefix] = self.path os.environ['%s_LOG_FILE' % self.prefix] = self.path
if severity: if severity is not None:
os.environ['%s_LOG_SEVERITY' % self.prefix] = str(severity) os.environ['%s_LOG_SEVERITY' % self.prefix] = str(severity)
self._severity = severity self._severity = severity
zLOG.MinimalLogger._log.initialize() zLOG.MinimalLogger._log.initialize()
...@@ -107,13 +111,19 @@ class StupidLogTest(unittest.TestCase): ...@@ -107,13 +111,19 @@ class StupidLogTest(unittest.TestCase):
def getLogFile(self): def getLogFile(self):
return open(self.path, 'rb') return open(self.path, 'rb')
def checkBasics(self): def checkBasics(self, severity=None):
self.setLog() self.setLog(severity=severity)
zLOG.LOG("basic", zLOG.INFO, "summary") zLOG.LOG("basic", zLOG.INFO, "summary")
f = self.getLogFile() f = self.getLogFile()
self.verifyEntry(f, subsys="basic", summary="summary") self.verifyEntry(f, subsys="basic", summary="summary")
def checkBasicsNumericSeverity(self):
self.checkBasics(severity=0)
def checkBasicsNamedSeverity(self):
self.checkBasics(severity='info')
def checkDetail(self): def checkDetail(self):
self.setLog() self.setLog()
zLOG.LOG("basic", zLOG.INFO, "xxx", "this is a detail") zLOG.LOG("basic", zLOG.INFO, "xxx", "this is a detail")
...@@ -140,9 +150,24 @@ class EventLogTest(StupidLogTest): ...@@ -140,9 +150,24 @@ class EventLogTest(StupidLogTest):
""" Test alternate envvars EVENT_LOG_FILE and EVENT_LOG_SEVERITY """ """ Test alternate envvars EVENT_LOG_FILE and EVENT_LOG_SEVERITY """
prefix = 'EVENT' prefix = 'EVENT'
class ConfigLogTest(StupidLogTest):
""" Test using a ZConfig section to control logging. """
def setLog(self, severity=None):
self._severity = severity
text = "<Log>\n path %s \n" % self.path
if severity is not None:
text += " level %s \n" % severity
text += "</Log>"
sio = StringIO(text)
conf = ZConfig.loadfile(sio)
zLOG.MinimalLogger._log.initialize_with_config(conf)
def test_suite(): def test_suite():
suite = unittest.makeSuite(StupidLogTest, 'check') suite = unittest.makeSuite(StupidLogTest, 'check')
suite.addTest(unittest.makeSuite(EventLogTest, 'check')) suite.addTest(unittest.makeSuite(EventLogTest, 'check'))
suite.addTest(unittest.makeSuite(ConfigLogTest, 'check'))
return suite return suite
if __name__ == "__main__": if __name__ == "__main__":
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment