SweetLog
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.
Metadata
Release files for sweetlog 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| sweetlog-0.1.2.tar.gz | 785.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| sweetlog-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 790.8 kB
Release files / sweetlog-0.1.2.tar.gz
| Download URL | sweetlog-0.1.2.tar.gz |
|---|---|
| Size | 785.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fd4f9f747f60541646004a2326a61d6d45c4eb80fb3d36f4c82a3740c6b872c6
|
|
BLAKE2b-256 checksum How to use checksums |
9bc5a507d914c7f354052ced78957c3e3e76b1064ee24b290f2072ac33d84bbe
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.5
|
Release files / sweetlog-0.1.2-py3-none-any.whl
| Download URL | sweetlog-0.1.2-py3-none-any.whl |
|---|---|
| Size | 5.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
468c902622986a754e84a8863f654859db1f85baf15d660a15abfe42ce4d5f4b
|
|
BLAKE2b-256 checksum How to use checksums |
e1537044102a314828f4aaca8082d4ae7bb072b6f89a4bd842d11596f1e20aa9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.5
|