This is a logging framework.
Project description
jk_logging
Introduction
This python module provides a logging infrastructure. It contains various classes to implement logging and aid in debugging.
Information about this module can be found here:
How to use this module
Basic Architecture
Documentation of Log Objects
In order to use loggers you need to know which classes there are end what kind of methods they offer for use. The next subchapters will provide you with that information.
Common Methods
Every log object will provide the following methods for use:
#
# Perform logging with log level DEBUG.
#
# @param string text The text to write to this logger.
#
def debug(self, text)
#
# Perform logging with log level NOTICE.
#
# @param string text The text to write to this logger.
#
def notice(self, text)
#
# Perform logging with log level INFO.
#
# @param string text The text to write to this logger.
#
def info(self, text)
#
# Perform logging with log level STDOUT.
# This method is intended to be used in conjunction with STDOUT handlers.
#
# @param string text The text to write to this logger.
#
def stdout(self, text)
#
# Perform logging with log level WARNING.
#
# @param string text The text to write to this logger.
#
def warning(self, text)
#
# Perform logging with log level ERROR.
#
# @param string text The text to write to this logger.
#
def error(self, text)
#
# Perform logging with log level STDERR.
# This method is intended to be used in conjunction with STDERR handlers.
#
# @param string text The text to write to this logger.
#
def stderr(self, text)
#
# Perform logging with log level EXCEPTION.
#
# @param Exception exception The exception to write to this logger.
#
def exception(self, exception)
#
# If this logger is buffering log messages, clear all log messages from this buffer.
# If this logger has references to other loggers, such as a <c>FilterLogger</c>
# or a <c>MulticastLogger</c>
#
def clear(self)
Other log objects will provide additional methods.
BufferLogger
Objects of type BufferLogger
will provide the following additional methods:
#
# Return a list of strings that contains the data stored in this logger.
# Standard formatting is used for output.
#
# @return string[] Returns an array of strings ready to be written to the console or a file.
#
def getBufferDataAsStrList(self)
#
# Return a list of tuples that contains the data stored in this logger.
#
# @return tuple[] Returns an array of tuples. Each tuple will contain the following fields:
# * int timeStamp : The time stamp since Epoch in seconds.
# * EnumLogLevel logLevel : The log level of this log entry.
# * string|Exception textOrException : A log message or an execption object.
#
def getBufferDataAsTupleList(self)
#
# Return a single string that contains the data stored in this logger.
# Standard formatting is used for output.
#
# @return string Returns a single string ready to be written to the console or a file.
#
def getBufferDataAsStr(self)
#
# Forward the log data stord in this logger to another logger.
#
# @param AbstractLogger logger Another logger that will receive the log data.
#
def forwardTo(self, logger, bClear = False)
Instantiation Based on Configuration Information Provided
Often it is convenient for applications to provide some detailed way of specifying how data should be logged. For exactly that reason this logging framework provides a function that is capable of creating loggers from some kind of description. Example:
import jk_logging
logger = jk_logging.instantiate({
"type": "MulticastLogger",
"nested": [
{
"type": "ConsoleLogger"
},
{
"type": "FileLogger",
"filePath": "mylogfile-%Y-%m-%d.log",
"rollOver": "day"
}
]
})
(more description comeing soon)
Examples
Here is some example code that demonstrates the use of the various loggers available:
print()
print("-- ConsoleLogger --")
print()
clog = ConsoleLogger.create()
clog.debug("This is a test for DEBUG.")
clog.notice("This is a test for NOTICE.")
clog.info("This is a test for INFO.")
clog.warning("This is a test for WARNING.")
clog.error("This is a test for ERROR.")
print()
print("-- Exception Handling --")
print()
def produceError():
a = 5
b = 0
c = a / b
try:
clog.notice("Now let's try a calculation that will fail ...")
produceError()
except Exception as ee:
clog.error(ee)
print()
print("-- FilterLogger --")
print()
flog = FilterLogger.create(clog, EnumLogLevel.WARNING)
flog.notice("This message will not appear in the log output.")
flog.error("This message will appear in the log output.")
print()
print("-- DetectionLogger --")
print()
dlog = DetectionLogger.create(clog)
dlog.notice("A notice.")
dlog.debug("A debug message.")
dlog.info("An informational message.")
dlog.debug("Another debug message.")
print(dlog.getLogMsgCountsStrMap())
print("Do we have debug messages? Answer: " + str(dlog.hasDebug()))
print("Do we have error messages? Answer: " + str(dlog.hasError()))
print()
print("-- BufferLogger --")
print()
blog = BufferLogger.create()
blog.info("First we write something to a buffer.")
blog.info("And something more.")
blog.notice("And more.")
blog.debug("And even more.")
blog.info("Then we write it to the console by forwarding the data to another logger.")
blog.forwardTo(clog)
print()
print("-- MulticastLogger --")
print()
mlog = MulticastLogger.create(clog, clog)
mlog.info("This message gets written twice.")
print()
print("-- NamedMulticastLogger --")
print()
nmlog = NamedMulticastLogger.create()
nmlog.addLogger("log1", clog)
nmlog.addLogger("log2", clog)
nmlog.info("This message gets written twice.")
nmlog.removeLogger("log1")
nmlog.info("This message gets written once.")
Things to do
Any help in implementing additional log classes and improving on the existing ones is appreciated. Feel free to contact me if you are interested in colaborating.
Contact Information
This is Open Source code. That not only gives you the possibility of freely using this code it also allows you to contribute. Feel free to contact the author(s) of this software listed below, either for comments, collaboration requests, suggestions for improvement or reporting bugs:
- Jürgen Knauth: pubsrc@binary-overflow.de
License
This software is provided under the following license:
- Apache Software License 2.0
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file jk_logging-0.2024.1.3.tar.gz
.
File metadata
- Download URL: jk_logging-0.2024.1.3.tar.gz
- Upload date:
- Size: 34.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.31.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 461feb9207412e15b6f04af22acd53f46e74d4d0d4866c0e5bd5a071e08f5d57 |
|
MD5 | 693f2782aed6777238b721a36286cd61 |
|
BLAKE2b-256 | e27ed2bb38ca0963b27299d4ea58ef5d7b7e1961338dbb638445038c9db63f0d |
File details
Details for the file jk_logging-0.2024.1.3-py3-none-any.whl
.
File metadata
- Download URL: jk_logging-0.2024.1.3-py3-none-any.whl
- Upload date:
- Size: 56.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.31.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 53d51e7d692ee036756a171a2ac503603f055e3bf49c019854017c502bd7d66a |
|
MD5 | 52d4e387756586cd33bf17035f79664d |
|
BLAKE2b-256 | c1722ae75e08e294b8262a25bd412e659a3c683f035dbef20c883ca91cda3ffb |