Skip to content

Commit

Permalink
added auto feature
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammad-fiaz committed Dec 16, 2023
1 parent 71886ed commit 91c89fd
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 31 deletions.
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,19 @@ Logly is a Python logging package designed to simplify and enhance your logging

- 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.
## Getting Started

## Installation

```bash
pip install logly
```

## Usage

```python
Expand Down Expand Up @@ -67,19 +72,24 @@ logly.error("AnotherKey3", "AnotherValue3", color=logly.COLOR.RED)
# Start logging again
logly.start_logging()

# Set default file path and max file size
logly.set_default_file_path("log.txt") # set default file path is "log.txt" if you want to set the file path where you want to save the log file.
logly.set_default_max_file_size(50) # set default max file size is 50 MB

# Log messages with default settings (using default file path and max file size)
logly.info("DefaultKey1", "DefaultValue1")
logly.warn("DefaultKey2", "DefaultValue2")
logly.error("DefaultKey3", "DefaultValue3", log_to_file=False)

# Log messages with custom file path and max file size
logly.info("CustomKey1", "CustomValue1", file_path="path/c.txt", max_file_size=25)
logly.warn("CustomKey2", "CustomValue2", file_path="path/c.txt", max_file_size=25)
#DEFAULT FILE SIZE IS 100 MB in txt file
# Log messages with custom file path and max file size(optional)
logly.info("CustomKey1", "CustomValue1", file_path="path/c.txt", max_file_size=25) # max_file_size is in MB and create a new file when the file size reaches max_file_size
logly.warn("CustomKey2", "CustomValue2", file_path="path/c.txt", max_file_size=25,auto=True) # auto=True will automatically delete the file data when it reaches max_file_size

# Access color constants directly
logly.info("Accessing color directly", "DirectColorValue", color=logly.COLOR.RED)

# Display logged messages
# Display logged messages (this will display all the messages logged so far)
print("Logged Messages:")
for message in logly.logged_messages:
print(message)
Expand Down
71 changes: 46 additions & 25 deletions logly/logly.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# logly.py

import os
from colorama import Fore, Style, init
from datetime import datetime
import re # Add this line for color code removal
import re

from logly.exception import FilePathNotFoundException, FileAccessError, FileCreationError, LoglyException

Expand Down Expand Up @@ -49,7 +47,7 @@ class COLOR:
YELLOW = Fore.YELLOW
RED = Fore.RED
CRITICAL = f"{Fore.RED}{Style.BRIGHT}"
WHITE = Fore.WHITE # Add this line
WHITE = Fore.WHITE

DEFAULT_MAX_FILE_SIZE_MB = 100 # 100MB

Expand Down Expand Up @@ -133,7 +131,7 @@ def remove_color_codes(self, text):
"""
return re.sub(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])', '', text)

def _log(self, level, key, value, color=None, log_to_file=True, file_path=None, file_name=None, max_file_size=None):
def _log(self, level, key, value, color=None, log_to_file=True, file_path=None, file_name=None, max_file_size=None, auto=True):
"""
Internal method to log a message.
Expand All @@ -146,6 +144,7 @@ def _log(self, level, key, value, color=None, log_to_file=True, file_path=None,
- file_path (str): File path for logging.
- file_name (str): File name for logging.
- max_file_size (int): Maximum file size for logging.
- auto (bool): Whether to auto-delete log file data when the size limit is reached.
"""
color = color or self.COLOR_MAP.get(level, self.COLOR.BLUE)
log_message = f"[{self.get_current_datetime()}] {level}: {color}{key}: {value}{Style.RESET_ALL}"
Expand Down Expand Up @@ -182,12 +181,17 @@ def _log(self, level, key, value, color=None, log_to_file=True, file_path=None,

# Check if the file size limit is reached
if max_file_size and file_exists and os.path.getsize(file_path) >= max_file_size_bytes:
# Find the next available file name with a number appended
file_base, file_ext = os.path.splitext(file_path)
count = 1
while os.path.exists(f"{file_base}_{count}{file_ext}"):
count += 1
file_path = f"{file_base}_{count}{file_ext}"
if auto:
# Auto-delete log file data by truncating the file
with open(file_path, 'w'):
pass
else:
# Find the next available file name with a number appended
file_base, file_ext = os.path.splitext(file_path)
count = 1
while os.path.exists(f"{file_base}_{count}{file_ext}"):
count += 1
file_path = f"{file_base}_{count}{file_ext}"

# Open the file in append mode, creating it if it doesn't exist
with open(file_path, "a" if file_exists else "w") as log_file:
Expand All @@ -200,7 +204,7 @@ def _log(self, level, key, value, color=None, log_to_file=True, file_path=None,
except Exception as e:
raise FileCreationError(f"Error creating or writing to the log file: {e}")

def log_function(self, level, key, value, color=None, log_to_file=True, file_path=None, file_name=None, max_file_size=None):
def log_function(self, level, key, value, color=None, log_to_file=True, file_path=None, file_name=None, max_file_size=None, auto=True):
"""
Log a message with exception handling.
Expand All @@ -213,13 +217,14 @@ def log_function(self, level, key, value, color=None, log_to_file=True, file_pat
- file_path (str): File path for logging.
- file_name (str): File name for logging.
- max_file_size (int): Maximum file size for logging.
- auto (bool): Whether to auto-delete log file data when the size limit is reached.
"""
try:
self._log(level, key, value, color, log_to_file, file_path, file_name, max_file_size)
self._log(level, key, value, color, log_to_file, file_path, file_name, max_file_size, auto)
except LoglyException as e:
print(f"LoglyException: {e}")

def info(self, key, value, color=None, log_to_file=True, file_path=None, file_name=None, max_file_size=None):
def info(self, key, value, color=None, log_to_file=True, file_path=None, file_name=None, max_file_size=None, auto=True):
"""
Log a message with the INFO level.
Expand All @@ -231,10 +236,11 @@ def info(self, key, value, color=None, log_to_file=True, file_path=None, file_na
- file_path (str): File path for logging.
- file_name (str): File name for logging.
- max_file_size (int): Maximum file size for logging.
- auto (bool): Whether to auto-delete log file data when the size limit is reached.
"""
self.log_function("INFO", key, value, color, log_to_file, file_path, file_name, max_file_size)
self.log_function("INFO", key, value, color, log_to_file, file_path, file_name, max_file_size, auto)

def warn(self, key, value, color=None, log_to_file=True, file_path=None, file_name=None, max_file_size=None):
def warn(self, key, value, color=None, log_to_file=True, file_path=None, file_name=None, max_file_size=None, auto=True):
"""
Log a message with the WARNING level.
Expand All @@ -246,10 +252,11 @@ def warn(self, key, value, color=None, log_to_file=True, file_path=None, file_na
- file_path (str): File path for logging.
- file_name (str): File name for logging.
- max_file_size (int): Maximum file size for logging.
- auto (bool): Whether to auto-delete log file data when the size limit is reached.
"""
self.log_function("WARNING", key, value, color, log_to_file, file_path, file_name, max_file_size)
self.log_function("WARNING", key, value, color, log_to_file, file_path, file_name, max_file_size, auto)

def error(self, key, value, color=None, log_to_file=True, file_path=None, file_name=None, max_file_size=None):
def error(self, key, value, color=None, log_to_file=True, file_path=None, file_name=None, max_file_size=None, auto=True):
"""
Log a message with the ERROR level.
Expand All @@ -261,10 +268,11 @@ def error(self, key, value, color=None, log_to_file=True, file_path=None, file_n
- file_path (str): File path for logging.
- file_name (str): File name for logging.
- max_file_size (int): Maximum file size for logging.
- auto (bool): Whether to auto-delete log file data when the size limit is reached.
"""
self.log_function("ERROR", key, value, color, log_to_file, file_path, file_name, max_file_size)
self.log_function("ERROR", key, value, color, log_to_file, file_path, file_name, max_file_size, auto)

def debug(self, key, value, color=None, log_to_file=True, file_path=None, file_name=None, max_file_size=None):
def debug(self, key, value, color=None, log_to_file=True, file_path=None, file_name=None, max_file_size=None, auto=True):
"""
Log a message with the DEBUG level.
Expand All @@ -276,9 +284,11 @@ def debug(self, key, value, color=None, log_to_file=True, file_path=None, file_n
- file_path (str): File path for logging.
- file_name (str): File name for logging.
- max_file_size (int): Maximum file size for logging.
- auto (bool): Whether to auto-delete log file data when the size limit is reached.
"""
self.log_function("DEBUG", key, value, color, log_to_file, file_path, file_name, max_file_size)
def critical(self, key, value, color=None, log_to_file=True, file_path=None, file_name=None, max_file_size=None):
self.log_function("DEBUG", key, value, color, log_to_file, file_path, file_name, max_file_size, auto)

def critical(self, key, value, color=None, log_to_file=True, file_path=None, file_name=None, max_file_size=None, auto=True):
"""
Log a critical message.
Expand All @@ -290,9 +300,11 @@ def critical(self, key, value, color=None, log_to_file=True, file_path=None, fil
- file_path (str, optional): The path to the log file. Defaults to None.
- file_name (str, optional): The name of the log file. Defaults to None.
- max_file_size (int, optional): The maximum size of the log file in megabytes. Defaults to None.
- auto (bool): Whether to auto-delete log file data when the size limit is reached.
"""
self.log_function("CRITICAL", key, value, color, log_to_file, file_path, file_name, max_file_size, auto)

def fatal(self, key, value, color=None, log_to_file=True, file_path=None, file_name=None, max_file_size=None):
def fatal(self, key, value, color=None, log_to_file=True, file_path=None, file_name=None, max_file_size=None, auto=True):
"""
Log a fatal message.
Expand All @@ -304,9 +316,11 @@ def fatal(self, key, value, color=None, log_to_file=True, file_path=None, file_n
- file_path (str, optional): The path to the log file. Defaults to None.
- file_name (str, optional): The name of the log file. Defaults to None.
- max_file_size (int, optional): The maximum size of the log file in megabytes. Defaults to None.
- auto (bool): Whether to auto-delete log file data when the size limit is reached.
"""
self.log_function("FATAL", key, value, color, log_to_file, file_path, file_name, max_file_size, auto)

def trace(self, key, value, color=None, log_to_file=True, file_path=None, file_name=None, max_file_size=None):
def trace(self, key, value, color=None, log_to_file=True, file_path=None, file_name=None, max_file_size=None, auto=True):
"""
Log a trace message.
Expand All @@ -318,9 +332,13 @@ def trace(self, key, value, color=None, log_to_file=True, file_path=None, file_n
- file_path (str, optional): The path to the log file. Defaults to None.
- file_name (str, optional): The name of the log file. Defaults to None.
- max_file_size (int, optional): The maximum size of the log file in megabytes. Defaults to None.
- auto (bool, optional): Whether to automatically delete log file data if it reaches the file size limit
and start storing again from scratch. Defaults to True.
"""
self.log_function("TRACE", key, value, color, log_to_file, file_path, file_name, max_file_size, auto)


def log(self, key, value, color=None, log_to_file=True, file_path=None, file_name=None, max_file_size=None):
def log(self, key, value, color=None, log_to_file=True, file_path=None, file_name=None, max_file_size=None, auto=True):
"""
Log an info message.
Expand All @@ -332,4 +350,7 @@ def log(self, key, value, color=None, log_to_file=True, file_path=None, file_nam
- file_path (str, optional): The path to the log file. Defaults to None.
- file_name (str, optional): The name of the log file. Defaults to None.
- max_file_size (int, optional): The maximum size of the log file in megabytes. Defaults to None.
- auto (bool, optional): Whether to automatically delete log file data if it reaches the file size limit
and start storing again from scratch. Defaults to True.
"""
self.log_function("LOG", key, value, color, log_to_file, file_path, file_name, max_file_size, auto)
14 changes: 14 additions & 0 deletions tests/log.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[2023-12-16 21:07:28] INFO: Key1: Value1
[2023-12-16 21:07:28] WARNING: Key2: Value2
[2023-12-16 21:07:28] ERROR: Key3: Value3
[2023-12-16 21:07:28] DEBUG: Key4: Value4
[2023-12-16 21:07:28] CRITICAL: Key5: Value5
[2023-12-16 21:07:28] FATAL: Key6: Value6
[2023-12-16 21:07:28] TRACE: Key7: Value7
[2023-12-16 21:07:28] LOG: Key8: Value8
[2023-12-16 21:07:28] INFO: AnotherKey1: AnotherValue1
[2023-12-16 21:07:28] WARNING: AnotherKey2: AnotherValue2
[2023-12-16 21:07:28] ERROR: AnotherKey3: AnotherValue3
[2023-12-16 21:07:28] INFO: DefaultKey1: DefaultValue1
[2023-12-16 21:07:28] WARNING: DefaultKey2: DefaultValue2
[2023-12-16 21:07:28] INFO: Accessing color directly: DirectColorValue
2 changes: 2 additions & 0 deletions tests/path/log.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[2023-12-16 21:07:28] INFO: CustomKey1: CustomValue1
[2023-12-16 21:07:28] WARNING: CustomKey2: CustomValue2
2 changes: 1 addition & 1 deletion tests/test_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_logly_integration(logly_instance):

# Log messages with custom file path and max file size
logly_instance.info("CustomKey1", "CustomValue1", file_path="path/log.txt", max_file_size=25)
logly_instance.warn("CustomKey2", "CustomValue2", file_path="path/log.txt", max_file_size=25)
logly_instance.warn("CustomKey2", "CustomValue2", file_path="path/log.txt",auto=True, max_file_size=25)

# Access color constants directly
logly_instance.info("Accessing color directly", "DirectColorValue", color=logly_instance.COLOR.RED)
Expand Down

0 comments on commit 91c89fd

Please sign in to comment.