Skip to main content

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:

License

This software is provided under the following license:

  • Apache Software License 2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jk_logging-0.2025.5.17.1.tar.gz (36.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jk_logging-0.2025.5.17.1-py3-none-any.whl (58.1 kB view details)

Uploaded Python 3

File details

Details for the file jk_logging-0.2025.5.17.1.tar.gz.

File metadata

  • Download URL: jk_logging-0.2025.5.17.1.tar.gz
  • Upload date:
  • Size: 36.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for jk_logging-0.2025.5.17.1.tar.gz
Algorithm Hash digest
SHA256 0975b9a9b7a37380fde8fe2a149252a9b0a04d810e49911349566d41fa729b55
MD5 f65a15df51a3020f2df4b7ec67c4f420
BLAKE2b-256 51ef4e038c3b5c2710db242f760d8aa748bf146229b3192eb3a58a8a1fedd3ad

See more details on using hashes here.

File details

Details for the file jk_logging-0.2025.5.17.1-py3-none-any.whl.

File metadata

File hashes

Hashes for jk_logging-0.2025.5.17.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c8d0c55679c368f5b3c131019004989fa0c7d21a3fc2f89f80a8730563c5b8e9
MD5 5306758ad01a575a5cddc3f8f363a4ab
BLAKE2b-256 46ee4be61a4a620860205f5bf4f0964c90f0846afb72f970ba80cc0013d4c699

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page