Skip to main content

SweetLog, pythonic logging package

Project description

SweetLog

SweetLog Logo

SweetLog, pythonic logging package


SweetLog is a Python logging package that provides a simple and flexible way to handle logging in your applications. It includes several components that you can use to customize your logging behavior.

Installation

You can install SweetLog using pip:

pip install sweetlog

or just clone the repository:

git clone https://github.com/dariush-bahrami/SweetLog.git

there is no dependencies, so you can just copy the sweetlog folder to your project.

Usage

You can use the Logger class to create a logger and log messages. You can also use the FileLoggingStream class to log messages to a file. Here is an example:

from pathlib import Path
from sweetlog import Logger, FileLoggingStream, LoggingLevel

# Create a FileLoggingStream
file_stream = FileLoggingStream(Path('log.txt'))

# Create a logger with the file stream and level set to DEBUG
logger = Logger([file_stream], level=LoggingLevel.DEBUG)

# Log a debug message
logger.debug('This is a debug message')

This will write the debug message to the log.txt file. If you want to add stdout as a stream, you can do this:

import sys
from pathlib import Path
from sweetlog import Logger, FileLoggingStream, LoggingLevel

# Create a FileLoggingStream
file_stream = FileLoggingStream(Path('log.txt'))

# Create a logger with the file and stdout streams and level set to DEBUG
logger = Logger([file_stream, sys.stdout], level=LoggingLevel.DEBUG)

# Log a debug message
logger.debug('This is a debug message')

This will write the debug message to the log.txt file and to the console.

Logger also has a get_decorator method that returns a decorator that can be used to log function calls. Here is an example:

from pathlib import Path
from sweetlog import Logger, FileLoggingStream, LoggingLevel

# Create a FileLoggingStream
file_stream = FileLoggingStream(Path('log.txt'))

# Create a logger with the file stream and level set to DEBUG
logger = Logger([file_stream], level=LoggingLevel.DEBUG)

# Create a decorator that logs function calls
decorator = logger.get_decorator(
    level=LoggingLevel.DEBUG,
    log_arguments=True,
    log_return=True,
)

@decorator
def example_function(a, b):
    return a + b

example_function(1, 2)

This will write the following to the log.txt file:

Calling example_function(a=1, b=2) -> 3

Components

LoggingLevel

This is an enumeration in the datatypes.py file that defines the different levels of logging. The levels are DEBUG, INFO, WARNING, ERROR, and CRITICAL. Each level has a specific value, and the logger will only log messages with a level that is equal to or higher than its own level.

Writable

This is an interface defined in the datatypes.py file. It represents a writable stream and has a write method that takes a string and writes it to the stream. It also has a flush method that flushes the stream.

Logger

This is a class defined in the logger.py file. It is the main component of the SweetLog package. It takes a list of Writable streams and a LoggingLevel as input. It has methods for logging messages at each level (debug, info, warning, error, critical). It also has a write method that writes a message to all its streams if the level of the message is equal to or higher than its own level.

FileLoggingStream

This is a class defined in the streams.py file. It implements the Writable interface and represents a file as a writable stream. It has a write method that writes a string to the file and a flush method that flushes the file.

Download files

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

Source Distribution

sweetlog-0.1.2.tar.gz (785.5 kB view hashes)

Uploaded Source

Built Distribution

sweetlog-0.1.2-py3-none-any.whl (5.3 kB view hashes)

Uploaded Python 3

Supported by

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