Code Monkey home page Code Monkey logo

sweetlog's Introduction

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.

sweetlog's People

Contributors

dariush-bahrami avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.