Skip to content

Commit

Permalink
Added all the logging functionality and updated README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammad-fiaz committed Dec 16, 2023
1 parent e72c6ae commit 71886ed
Show file tree
Hide file tree
Showing 7 changed files with 443 additions and 81 deletions.
93 changes: 93 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,99 @@ pip install logly
```
## Usage

```python
# Import Logly
from logly import Logly

# Create a Logly instance
logly = Logly()

# Start logging
logly.start_logging()

# Log messages with different levels and colors
logly.info("Key1", "Value1", color=logly.COLOR.CYAN)
logly.warn("Key2", "Value2", color=logly.COLOR.YELLOW)
logly.error("Key3", "Value3", color=logly.COLOR.RED)
logly.debug("Key4", "Value4", color=logly.COLOR.BLUE)
logly.critical("Key5", "Value5", color=logly.COLOR.CRITICAL)
logly.fatal("Key6", "Value6", color=logly.COLOR.CRITICAL)
logly.trace("Key7", "Value7", color=logly.COLOR.BLUE)
logly.log("Key8", "Value8", color=logly.COLOR.WHITE)

# Stop logging (no messages will be displayed or logged after this point)
logly.stop_logging()

# Log more messages after stopping logging (these won't be displayed or logged)
logly.info("AnotherKey1", "AnotherValue1", color=logly.COLOR.CYAN)
logly.warn("AnotherKey2", "AnotherValue2", color=logly.COLOR.YELLOW)
logly.error("AnotherKey3", "AnotherValue3", color=logly.COLOR.RED)

# Start logging again
logly.start_logging()

# 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)

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

# Display logged messages
print("Logged Messages:")
for message in logly.logged_messages:
print(message)

```
## Explanation:

1. Import the `Logly` class from the `logly` module.
2. Create an instance of `Logly`.
3. Start logging using the `start_logging()` method.
4. Log messages with various levels (info, warn, error, debug, critical, fatal, trace) and colors.
5. Stop logging using the `stop_logging()` method.
6. Log additional messages after stopping logging.
7. Start logging again.
8. Log messages with default settings and custom file path and max file size.
9. Access color constants directly.
10. Display logged messages.

## 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:

| Color Code | Color |
| ---------- | ------ |
| BLACK | Black |
| RED | Red |
| GREEN | Green |
| YELLOW | Yellow |
| BLUE | Blue |
| MAGENTA | Magenta|
| CYAN | Cyan |
| WHITE | White |

For example, you can use `color=logly.COLOR.BLACK` or `color="BLACK"` for the black color.

## Contributing
Contributions are welcome! Before contributing, please read our [Contributing Guidelines](CONTRIBUTING.md) to ensure a smooth and collaborative development process.

Expand Down
47 changes: 47 additions & 0 deletions logly/exception.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class LoglyException(Exception):
"""
Base exception class for Logly. All specific Logly exceptions should inherit from this class.
"""
def __init__(self, message):
"""
Initialize a LoglyException.
Parameters:
- message (str): The error message.
"""
super().__init__(message)


class FilePathNotFoundException(LoglyException):
"""
Exception raised when the specified file path is not found.
"""
pass


class FileCreationError(LoglyException):
"""
Exception raised when there is an error creating or writing to the log file.
"""
pass


class FileAccessError(LoglyException):
"""
Exception raised when there is an error accessing the log file.
"""
pass


class InvalidConfigError(LoglyException):
"""
Exception raised for invalid Logly configuration.
"""
pass


class InvalidLogLevelError(LoglyException):
"""
Exception raised when an invalid log level is provided.
"""
pass
Loading

0 comments on commit 71886ed

Please sign in to comment.