plugins/python/regress: add a test and example of using the python logger

This commit is contained in:
Robert Manner
2020-02-27 11:25:02 +01:00
committed by Todd C. Miller
parent c039a99c10
commit 6c9515496d
5 changed files with 27 additions and 5 deletions

View File

@@ -1,5 +1,7 @@
import sudo
import logging
class DebugDemoPlugin(sudo.Plugin):
"""
@@ -61,6 +63,15 @@ class DebugDemoPlugin(sudo.Plugin):
sudo.debug(sudo.DEBUG.INFO, "My demo purpose plugin shows "
"this INFO level debug message")
# You can also use python log system, because sudo sets its log handler
# on the root logger.
# Note that the level of python logging is separate than the one set in
# sudo.conf. If using the python logger, each will have effect.
logger = logging.getLogger()
logger.setLevel(logging.INFO)
logger.error("Python log system shows this ERROR level debug message")
logger.info("Python log system shows this INFO level debug message")
# If you raise the level to info or below, the call of the debug
# will also be logged.
# An example output you will see in the debug log file:

View File

@@ -1,4 +1,6 @@
sudo.debug was called with arguments: (<DEBUG.ERROR: 2>, 'My demo purpose plugin shows this ERROR level debug message')
sudo.debug was called with arguments: (<DEBUG.INFO: 6>, 'My demo purpose plugin shows this INFO level debug message')
LogHandler.emit was called with arguments: (<sudo.LogHandler <stderr> (NOTSET)>, <LogRecord: root, 40, SRC_DIR/example_debugging.py, 72, "Python log system shows this ERROR level debug message">)
LogHandler.emit was called with arguments: (<sudo.LogHandler <stderr> (NOTSET)>, <LogRecord: root, 20, SRC_DIR/example_debugging.py, 73, "Python log system shows this INFO level debug message">)
sudo.options_as_dict was called with arguments: (('ModulePath=SRC_DIR/example_debugging.py', 'ClassName=DebugDemoPlugin'),)
sudo.options_as_dict returned result: {'ModulePath': 'SRC_DIR/example_debugging.py', 'ClassName': 'DebugDemoPlugin'}

View File

@@ -1,7 +1,11 @@
__init__ @ SRC_DIR/example_debugging.py:56 calls C function:
__init__ @ SRC_DIR/example_debugging.py:58 calls C function:
sudo.debug was called with arguments: (<DEBUG.ERROR: 2>, 'My demo purpose plugin shows this ERROR level debug message')
__init__ @ SRC_DIR/example_debugging.py:61 calls C function:
__init__ @ SRC_DIR/example_debugging.py:63 calls C function:
sudo.debug was called with arguments: (<DEBUG.INFO: 6>, 'My demo purpose plugin shows this INFO level debug message')
__init__ @ SRC_DIR/example_debugging.py:74 calls C function:
handle @ /usr/lib/python3.7/logging/__init__.py:894 calls C function:
LogHandler.emit was called with arguments: (<sudo.LogHandler <stderr> (NOTSET)>, <LogRecord: root, 40, SRC_DIR/example_debugging.py, 72, "Python log system shows this ERROR level debug message">)
handle @ /usr/lib/python3.7/logging/__init__.py:894 calls C function:
LogHandler.emit was called with arguments: (<sudo.LogHandler <stderr> (NOTSET)>, <LogRecord: root, 20, SRC_DIR/example_debugging.py, 73, "Python log system shows this INFO level debug message">)
__init__ @ SRC_DIR/example_debugging.py:85 calls C function:
sudo.options_as_dict was called with arguments: (('ModulePath=SRC_DIR/example_debugging.py', 'ClassName=DebugDemoPlugin'),)
sudo.options_as_dict returned result: {'ModulePath': 'SRC_DIR/example_debugging.py', 'ClassName': 'DebugDemoPlugin'}

View File

@@ -1 +1,2 @@
My demo purpose plugin shows this ERROR level debug message
Python log system shows this ERROR level debug message

View File

@@ -1,4 +1,8 @@
__init__ @ SRC_DIR/example_debugging.py:56 debugs:
__init__ @ SRC_DIR/example_debugging.py:58 debugs:
My demo purpose plugin shows this ERROR level debug message
__init__ @ SRC_DIR/example_debugging.py:61 debugs:
__init__ @ SRC_DIR/example_debugging.py:63 debugs:
My demo purpose plugin shows this INFO level debug message
handle @ /usr/lib/python3.7/logging/__init__.py:894 debugs:
Python log system shows this ERROR level debug message
handle @ /usr/lib/python3.7/logging/__init__.py:894 debugs:
Python log system shows this INFO level debug message