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❤✨.
- Introduction
- Installation
- Features
- Usage
- Set Default Path
- Color Options
- Tips & Tricks
- Contributing
- Code of Conduct
- License
- Support the Project
- Happy Coding
- 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!
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
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
You can create a Logly instance with default settings or customize the configuration:
logly = Logly()
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
)
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)
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 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)
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 logging to a file:
logly.disable_file_logging()
Enable logging to a file:
logly.enable_file_logging()
Set the default file path for log files:
logly.set_default_file_path("logs/new_log.txt")
Set the default maximum file size for log files:
logly.set_default_max_file_size(50) # 50 MB
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.
Level | Color Code |
---|---|
INFO | CYAN |
WARNING | YELLOW |
ERROR | RED |
DEBUG | BLUE |
CRITICAL | BRIGHT RED |
TRACE | BLUE |
DEFAULT | WHITE |
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.
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
[XXXX-XX-XX XX:XX: XX] INFO: msg: hello this is logly
Contributions are welcome! Before contributing, please read our Contributing Guidelines to ensure a smooth and collaborative development process.
Please review our Code of Conduct to understand the standards of behavior we expect from contributors and users of this project.
This project is licensed under the MIT License. See LICENSE for more details.
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.
Support Logly directly on GitHub to help sustain ongoing development.
If you prefer, you can also support the project via Ko-fi.
Thank you for supporting the project! 🙏