Skip to main content

A simple Python logger with colors and file output

Project description

Smart Logger 📝

Smart Logger is a highly customizable Python logger with colored console output, file logging, daily rotation, unique directories, and context support. Perfect for projects where structured logging and contextual information are essential.


📦 Installation

pip install smlog

🚀 Quick Start

from logger import Logger

logger = Logger(
    name="MyApp",
    min_level="DEBUG",
    use_colors=True,
    log_to_file=True,
    log_dir="logs",
    rotate_daily=True,
    unique_dir=True,
    console=True
)

logger.debug("This is a debug message")
logger.info("Informational message", user="Nikita")
logger.warning("Warning message")
logger.error("Error occurred", exc=ValueError("Invalid value"))

logger.bind(session_id="12345", user="Nikita")
logger.info("Message with context")
logger.unbind("session_id")  # Remove a single key
⚙️ Features
1. Colored Console Output
- DEBUG  Blue
- INFO  Cyan
- WARNING  Yellow
- ERROR  Red
- DATE  Magenta
- MESSAGE  Cyan
Colors can be disabled using use_colors=False.

2. File Logging
Logs can be saved to a specific directory (log_dir).
Supports daily rotation (rotate_daily=True).
Unique directories can be created with unique_dir=True to avoid overwriting existing logs.

3. Custom Format
Use set_format(fmt) to customize log format:
logger.set_format("[{time}] [{level}] ({name}) {msg} | {context}")
Available fields:
- {time}  current date and time
- {level}  log level
- {name}  logger name
- {msg}  log message
- {context}  optional context
Context is only included if it exists. The | separator is automatically omitted if empty.

4. Context Management
Add context globally with bind:
logger.bind(user="Nikita", session="xyz")
logger.info("Message with context")
logger.unbind("session")  # Remove a key
logger.unbind()  # Clear all context

5. Log Levels
Supported levels: DEBUG, INFO, WARNING, ERROR.
Filter logs by min_level:

    logger = Logger(min_level="WARNING")
    logger.debug("This will not show")  # Skipped
    logger.error("This will show")
    Invalid levels will suggest the closest match:
    logger.log("INF", "Message")  # Did you mean "INFO"?

6. Format Help
Prints available fields and example usage:
logger.show_format_help()
Example output:
    ==========================
    Available parameters: ['time', 'level', 'name', 'msg', 'context']
    Example: [{time}] [{level}] ({name}) {msg}{context}
    [NOTICE] If you want to add 'context', do it as shown in the example!
    ==========================
🛠️ Logger Constructor
| **Parameter**  | **Type** | **Default** | **Description**                                  |
|:--------------:|:--------:|:-----------:|:------------------------------------------------|
| `name`         | `str`    | `"root"`    | 🏷 Logger name                                   |
| `min_level`    | `str`    | `"DEBUG"`   |  Minimum log level                             |
| `use_colors`   | `bool`   | `True`      | 🎨 Enable colored output in console             |
| `log_to_file`  | `bool`   | `False`     | 💾 Save logs to file                             |
| `log_dir`      | `str`    | `"logs"`    | 📂 Directory for log files                       |
| `rotate_daily` | `bool`   | `False`     | 🗓 Create a new file each day                    |
| `unique_dir`   | `bool`   | `False`     |  Create a unique directory if one exists      |
| `console`      | `bool`   | `True`      | 🖥 Print messages to console                     |

📝 Example Usage

logger = Logger(
    name="MyApp",
    min_level="DEBUG",
    log_to_file=True,
    log_dir="my_logs",
    rotate_daily=True,
    unique_dir=True
)

logger.bind(user="Nikita", env="production")
logger.info("App started")
logger.warning("High load warning")
logger.error("Connection error", exc=ConnectionError("Timeout"))
logger.unbind("env")
logger.debug("Debugging info", ip="127.0.0.1")

Example log file output:
    [2026-01-27 15:42:10] [INFO] (MyApp) App started | user=Nikita, env=production
    --------------------
    [2026-01-27 15:42:15] [WARNING] (MyApp) High load warning | user=Nikita, env=production
    --------------------
    [2026-01-27 15:42:20] [ERROR] (MyApp) Connection error | user=Nikita, env=production
    --------------------

✅ Features Summary

Lightweight, clean Python logger Colored and formatted console output Context support for dynamic logging File logging with optional daily rotation Unique directories to avoid overwriting logs Full Python 3.7+ support

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

smlog-1.3.0.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

smlog-1.3.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file smlog-1.3.0.tar.gz.

File metadata

  • Download URL: smlog-1.3.0.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for smlog-1.3.0.tar.gz
Algorithm Hash digest
SHA256 8687a4af4a605de98f7c7dffde468fce1b8bbaed8572dcd072aecc6ca0963775
MD5 e7b2f7348ba4bdb75298440f8b9de0bb
BLAKE2b-256 7730bfd4c8a4f2c853ea78a5244e9fd0593d9cca482af09d7fc41c1fdca7e91e

See more details on using hashes here.

File details

Details for the file smlog-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: smlog-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for smlog-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8ffc306590f08fdf02ae7d5f986f811fdf5958c4d43602a995078cc263838db4
MD5 85cfcfdca969696217c7904dc2e1ae4a
BLAKE2b-256 295a9d32c2b847c9aaf2800ef50025de9b0ea5d5a6c02c97f8293ebf4cd7b0c6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page