Skip to content

Logly is a Ready to Go! Python package designed to simplify and enhance the logging process. With Logly, you can log dynamically and rapidly, providing a seamless experience for developers. It comes with customization options, making it easy to tailor your logs according to specific needs.

License

Notifications You must be signed in to change notification settings

muhammad-fiaz/logly

Repository files navigation

Sample Image

Logly

Run Tests PyPI Version Python Versions License: MIT Downloads Last Commit GitHub Issues GitHub Stars GitHub Forks Maintainer Sponsor on GitHub License: MIT Stability Follow me on GitHub

Join My Discord Sponsor muhammad-fiaz

ko-fi

Tired of writing custom logging code for your Python applications?

Logly is a ready to go logging utility that provides an easy way to log messages with different levels, colors, and many custom options. It is designed to be flexible, allowing you to customize the log messages based on your application's needs. Logly supports logging to both the console and a file, and it comes with built-in color-coded log levels for better visibility.

if you like this project, make sure to star 🌟 it in the repository and if you want to contribute make sure to fork this repository❤✨.

Table of Contents

  1. Introduction
  2. Installation
  3. Features
  4. Usage
  5. Set Default Path
  6. Color Options
  7. Tips & Tricks
  8. Contributing
  9. Code of Conduct
  10. License
  11. Support the Project
  12. Happy Coding

Features

  • Support for multiple log levels: INFO, WARNING, ERROR, DEBUG, CRITICAL, FATAL, TRACE, and custom LOG levels.
  • Colored output for console logs.
  • Customizable log message formats.
  • Logging to files with automatic file rollover.
  • Control over console display and file logging.
  • Easy configuration and usage.
  • Open Source: Logly is an open-source project, and we welcome contributions from the community.
  • Community Support: Join a community of developers using Logly for their logging needs.
  • many more features!

Getting Started

Installation

To install the stable version of logly, use:

pip install logly

If you want to install the latest development version directly from GitHub, you can run:

pip install git+https://github.com/muhammad-fiaz/logly.git

Usage

Once installed, you can use logly in your Python project for logging. Here is a basic example of how to set it up and use it:

from logly import Logly, LoglyConfig

Create a Logly Instance

You can create a Logly instance with default settings or customize the configuration:

Default Settings

logly = Logly()

Custom Configuration

    logly.Config(
        show_time=True,
        color_enabled=True,
        logging_enabled=True,
        log_to_file_enabled=True,
        default_file_path="logs/app.log",
        default_max_file_size=100,
        custom_format="{timestamp} - {level}: {message}",
        display=True
    )

Start Logging

Enable logging to store logs in a text file and optionally display them in the console:

# Start logging and display logs in the console
logly.start_logging()

# Start logging but do not display logs in the console
logly.start_logging(display=False)

Stop Logging

Disable logging to a file and optionally stop displaying logs in the console:

# Stop logging to file but continue displaying logs in the console
logly.stop_logging()

# Stop logging to file and stop displaying logs in the console
logly.stop_logging(display=False)

Log Messages

Log messages with different levels and colors:

logly.info("Application started successfully.")
logly.info("User logged in", color=Logly.COLOR.GREEN)  # with custom color

logly.info("Database connection established", "Connection details: host=db.local, port=5432", color=Logly.COLOR.CYAN)
logly.warn("API rate limit exceeded", "API request count exceeded 1000", color=Logly.COLOR.YELLOW)
logly.error("Database connection failed", "Unable to reach database at db.local", color=Logly.COLOR.RED)
logly.debug("User request details", "User requested resource /api/data", color=Logly.COLOR.BLUE)
logly.critical("Critical system failure", "Disk space usage exceeded 95%", color=Logly.COLOR.CRITICAL)
logly.fatal("Application crashed", "Unhandled exception in user module", color=Logly.COLOR.CRITICAL)
logly.trace("Debug trace", "Trace info: function call stack", color=Logly.COLOR.BLUE)
logly.log("System status", "All systems operational", color=Logly.COLOR.WHITE)

Custom Log Format

You can customize the format of the log messages:

config = LoglyConfig(custom_format="{timestamp} - {level} - {message}")
logly = Logly(config=config)
logly.start_logging()

logly.info("Custom format log message.")

Disable File Logging

Disable logging to a file:

logly.disable_file_logging()

Enable File Logging

Enable logging to a file:

logly.enable_file_logging()

Set Default File Path

Set the default file path for log files:

logly.set_default_file_path("logs/new_log.txt")

Set Default Maximum File Size

Set the default maximum file size for log files:

logly.set_default_max_file_size(50)  # 50 MB

Example

from logly import Logly, LoglyConfig

# Create a Logly instance with custom configuration
config = LoglyConfig(display=True, log_to_file_enabled=True, default_file_path="logs/app.log")
logly = Logly(config=config)

# Start logging to store the logs in a text file and display in console
logly.start_logging()

logly.info("Application started successfully.")
logly.info("User logged in", color=Logly.COLOR.GREEN)  # with custom color

# Log messages with different levels and colors
logly.info("Database connection established", "Connection details: host=db.local, port=5432", color=Logly.COLOR.CYAN)
logly.warn("API rate limit exceeded", "API request count exceeded 1000", color=Logly.COLOR.YELLOW)
logly.error("Database connection failed", "Unable to reach database at db.local", color=Logly.COLOR.RED)
logly.debug("User request details", "User requested resource /api/data", color=Logly.COLOR.BLUE)
logly.critical("Critical system failure", "Disk space usage exceeded 95%", color=Logly.COLOR.CRITICAL)
logly.fatal("Application crashed", "Unhandled exception in user module", color=Logly.COLOR.CRITICAL)
logly.trace("Debug trace", "Trace info: function call stack", color=Logly.COLOR.BLUE)
logly.log("System status", "All systems operational", color=Logly.COLOR.WHITE)

# Stop logging to file (messages will be displayed but not logged to file after this point)
logly.stop_logging()

# Log more messages after stopping logging to file (messages will be displayed but not logged in file)
logly.info("User session ended", "User logged out", color=Logly.COLOR.CYAN)
logly.warn("Low disk space", "Disk space is below 10%", color=Logly.COLOR.YELLOW)
logly.error("File not found", "Unable to find /config/settings.json", color=Logly.COLOR.RED)

if you faced an error like FileNotFoundError: [Errno 2] No such file or directory: 'log.txt' you can use the following code snippet to set the default path

import os
from logly import Logly

logly = Logly() # initialize the logly
logly.start_logging() # make sure to include this or else the log will only display without storing it

logly.set_default_max_file_size(50) # optional
logger = os.path.join(os.path.dirname(os.path.abspath(__file__)), "log.txt") # This will ensure the path location to create the log.txt on current directory
logly.set_default_file_path(logger)

for more information, check the repository.

Color Options:

Default Color Options:

Level Color Code
INFO CYAN
WARNING YELLOW
ERROR RED
DEBUG BLUE
CRITICAL BRIGHT RED
TRACE BLUE
DEFAULT WHITE

Custom Color Options:

You can use any of the following color codes for custom coloring:

NAME Color Code
CYAN CYAN
YELLOW YELLOW
RED RED
BLUE BLUE
BRIGHT RED CRITICAL
WHITE WHITE
GREEN Fore.GREEN

For example, you can use color=logly.COLOR.RED for the red color.

Tips & Tricks

If you want to use logly in your project files without creating a new object in each Python file or class, you can create a file named logly.py. In this file, initialize logly and configure the defaults. Now, you can easily import and use it throughout your project:

logly.py

# logly.py in your root or custom path
# Import Logly

from logly import Logly
import os
logly = Logly()
logly.start_logging()

# Set the default file path and maximum file size
logly.set_default_max_file_size(50)
logger = os.path.join(os.path.dirname(os.path.abspath(__file__)), "log.txt") # This will ensure the path location to create the log.txt 
logly.set_default_file_path(logger)

# Start logging again
logly.start_logging()

you can now use the logly by

main.py

from logly import logly # make sure to import it some IDE may automatically import it on top

logly.info("msg","hello this is logly", color=logly.COLOR.RED) # with custom color of red

output

[XXXX-XX-XX XX:XX: XX] INFO: msg: hello this is logly

Contributing

Contributions are welcome! Before contributing, please read our Contributing Guidelines to ensure a smooth and collaborative development process.

Code of Conduct

Please review our Code of Conduct to understand the standards of behavior we expect from contributors and users of this project.

License

This project is licensed under the MIT License. See LICENSE for more details.

Support the Project

Your support helps improve Logly and enables us to continue adding more features and improvements. If you'd like to contribute and support the development of this project, consider becoming a sponsor on GitHub or Ko-fi.

Become a Sponsor on GitHub

Support Logly directly on GitHub to help sustain ongoing development.

Sponsor muhammad-fiaz

Support via Ko-fi

If you prefer, you can also support the project via Ko-fi.

Support on Ko-fi

Thank you for supporting the project! 🙏

Happy Coding

About

Logly is a Ready to Go! Python package designed to simplify and enhance the logging process. With Logly, you can log dynamically and rapidly, providing a seamless experience for developers. It comes with customization options, making it easy to tailor your logs according to specific needs.

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages