Skip to main content

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 details)

Uploaded Source

Built Distribution

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

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

Uploaded Python 3

File details

Details for the file sweetlog-0.1.2.tar.gz.

File metadata

  • Download URL: sweetlog-0.1.2.tar.gz
  • Upload date:
  • Size: 785.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for sweetlog-0.1.2.tar.gz
Algorithm Hash digest
SHA256 fd4f9f747f60541646004a2326a61d6d45c4eb80fb3d36f4c82a3740c6b872c6
MD5 3e3578094b1352a61170e7ab9662a7cb
BLAKE2b-256 9bc5a507d914c7f354052ced78957c3e3e76b1064ee24b290f2072ac33d84bbe

See more details on using hashes here.

File details

Details for the file sweetlog-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: sweetlog-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for sweetlog-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 468c902622986a754e84a8863f654859db1f85baf15d660a15abfe42ce4d5f4b
MD5 e06e23f11fcdbf9439b8ca1f1923b9ac
BLAKE2b-256 e1537044102a314828f4aaca8082d4ae7bb072b6f89a4bd842d11596f1e20aa9

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 files

0.1.1

2 files

0.1.0

2 files

Supported by

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