Skip to main content

logpp

Why logpp?

logpp is a fairly simple module that contains some extensions for Python’s built in logging module. It provides a few facilities that allow you to pass extended information with logging messages.

The three principle components are listed below.

The module also provides the LogppMixin which you can use to provide standardized access to a logger via the LogppMixin.logger method.

Logging a Message

The example below has been expanded to make the components easier to see, but it’s actually a fairly simple one-liner. The msg() function takes a summary str and a detail object (which in the example is just a dictionary).

The function returns a LogppMessage which, when represented in str form is simply the summary.

logging.info(
    msg(
        'The weather is currently sunny with a temperature of 25°C.',
        {
            'conditions': 'sunny',
            'temperature': 25
        }
    )
)

Logging handlers that aren’t aware of the detail information should simply see the logpp message as the summary.

Handling Messages

If you’re using logpp, chances are you want to do something useful or clever with the detail information. To accomplish that you can create your own logging handler. If your custom handler is only interested in logpp messages, you can extend the LogppHandler and override the emit_logpp() method. The base class will perform checks to make sure that only logging messages that are instances of the LogppMessage class are passed to this method.

Putting It All Together

The sample below briefly demonstrates the creation of a custom log handler and should give you an idea of what to expect from such a facility.

import logging
from logpp.logging import msg, LogppMessage, LogppHandler


# Create a custom handler.
class CustomLogppHandler(LogppHandler):

    def emit_logpp(self, msg_: LogppMessage):
        print(f'SUMMARY: {msg_.summary}')
        print(f'DETAILS: {msg_.detail}')


logging.basicConfig(level=logging.INFO)
# Add the custom handler to the logger (just as you would with any handler).
logging.getLogger().addHandler(CustomLogppHandler())

# Log a message to be handled by the custom handler.
logging.info(
    msg(
        'The weather is currently sunny with a temperature of 25°C.',
        {
            'conditions': 'sunny',
            'temperature': 25
        }
    )
)

# Log a message that will be ignored by the custom handler.
logging.info('This message will be ignored by the custom handler.')

Using the LogppMixin

Let’s say you have a class that needs to log its activities. Often you’ll want to use a named logger. This can involve a few lines of boiler plate which can be a bit tedious to produce in every class. By extending the LogppMixin your class gains the logger() function which returns a logger with a name that reflects the name of the class (though you can override that behavior by adding a __loggername__ attribute to the class).

import logging
from logpp.logging import LogppMixin


# Just so we may demonstrate the use of the mixin, here's a base class
# that has nothing to do with logging from which we can inherit.
class SampleBaseClass(object):
    pass


# Now let's create a class that extends the sample base class, but
# which also mixes in the logging facility.
class LoggableClass(SampleBaseClass, LogppMixin):

    def log_something(self):
        self.logger().info('Hello world!')


# Set up basic logging
logging.basicConfig(level=logging.INFO)

# Create a new instance of the mixed-in class...
loggable = LoggableClass()
# ...and ask it to log something.
loggable.log_something()

Resources

Would you like to learn more? Check out the links below!

Authors

  • Pat Daburu - Initial work - github

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Metadata

Release files for logpp 0.0.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for logpp 0.0.8
File Size Uploaded Provenance Actions
logpp-0.0.8.tar.gz 5.5 kB Without provenance Details for logpp-0.0.8.tar.gz

Release files / logpp-0.0.8.tar.gz

Download URL logpp-0.0.8.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
cb938f27cf9203aa7d192d5265889b0a2b29bde83e7ad1a4c2a0d99f81ad064d
BLAKE2b-256 checksum
How to use checksums
1258d1aa1c75ee70b23127e9dc461e1d02f022e707e558e9cfe4de0909c1a8a9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.4

Security for logpp 0.0.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page