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

  • Easy-to-use logging for Python applications.
  • Customizable log levels and formatting.
  • Customizable log colors.
  • Log to file and/or console.
  • Log to file with automatic file rotation.
  • Log to file with automatic file size management.
  • Log to file with automatic file deletion.
  • Log to file with automatic deletion and rewriting of the file when it reaches max_file_size.
  • 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:

# Import Logly
from logly import Logly

# Create a Logly instance
logly = Logly()

# Start logging to store the logs in a text file
logly.start_logging()  # Make sure to include this or else the logs will only display without being saved to a file

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 (messages will be displayed but not logged to file after this point)
logly.stop_logging()

# Log more messages after stopping logging (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)

# Example of retrieving and printing a logged message
get_message = logly.info("New feature deployed", "Version 2.0 live now", color=logly.COLOR.RED)
print(get_message)  # You can use the returned message in your application (optional)

# Log message with custom color and no timestamp
logly.info("Custom log without time", "This log message does not include a timestamp", color=logly.COLOR.RED, show_time=False)

# Start logging again
logly.start_logging()

# Set the default file path and max file size
logly.set_default_file_path("application_logs.txt")  # Set the default file path
logly.set_default_max_file_size(50)  # Set default max file size to 50 MB

# Log messages with default settings (using default file path and max file size)
logly.info("Default logging example", "Logging to default file path and size")
logly.warn("File logging size test", "Max file size set to 50 MB", log_to_file=False)

# Log messages with custom file path and max file size (optional)
logly.info("Logging with custom file path", "This will create a new log file", file_path="logs/custom_log.txt", max_file_size=25)
logly.warn("Auto file size management", "Log file will be auto-deleted when size exceeds limit", file_path="logs/auto_delete_log.txt", max_file_size=25, auto=True)

# Access color constants directly for logging
logly.info("Using color constants directly", "This message is in red", color=logly.COLOR.RED)

# Disable color for logging
logly.color_enabled = False
logly.info("Logging without color", "This log message is displayed without color", color=logly.COLOR.RED)

# Enable color for specific log message
logly.info("Enable color for this message", "This message will have color", color=logly.COLOR.RED, color_enabled=True)

# Disable color for specific log message
logly.info("Disable color for this message", "This message will be displayed without color", color=logly.COLOR.RED, color_enabled=False)

# Display logged messages (this will display all the messages logged so far)
print("Logged Messages:")
for message in logly.logged_messages:
    print(message)

for more information, check the repository

Set the Default Path

If you encounter an error related to the default file path, you can use the following code snippet to set the default path:

import os
from logly import Logly

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")
logly.set_default_file_path(logger)

This will set the default file path, and you can customize it according to your requirements.

if you want to set the default path for the log file, you can use the following code snippet

from logly import Logly
logly = Logly()
logly.set_default_file_path("log.txt")

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

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