Skip to main content

A small logger for notion.so

Project description

Notion Logger

The NotionLogger package provides a custom logging handler that sends log entries to a specified Notion database. This tool is useful for centralizing logs from different applications and tracking errors, warnings, and other log messages in Notion.

Features

  • 📄 Log to Notion: Directly send log messages to a Notion database.
  • ⚙️ Custom Log Types: Support for various log types including INFO, DEBUG, WARNING, and ERROR.
  • 🌐 Global Exception Handling: Automatically logs uncaught exceptions to Notion.

Installation

pip install requests

Usage

Basic Setup

First, import the necessary modules and set up your logger:

import logging
from notion_logger import NotionLogger

# Replace with your own Notion page ID and secret token
PAGE_ID = "your_page_id"
SECRET_TOKEN = "your_secret_token"

notion_handler = NotionLogger(page_id=PAGE_ID, secret_token=SECRET_TOKEN)
logging.getLogger().addHandler(notion_handler)
logging.getLogger().setLevel(logging.DEBUG)

# Activate global exception handler
notion_handler.activate_global_handler()

Logging Messages

Now you can log messages as usual, and they will be sent to your Notion database:

logger = logging.getLogger(__name__)

logger.info("This is an informational message 📄")
logger.debug("This is a debug message 🐞")
logger.warning("This is a warning message ⚠️")
logger.error("This is an error message ❌")

Example

Here's a complete example including the setup and logging messages:

import logging
from notion_logger import NotionLogger

# Replace with your own Notion page ID and secret token
PAGE_ID = "your_page_id"
SECRET_TOKEN = "your_secret_token"

# Initialize the Notion logger
notion_handler = NotionLogger(page_id=PAGE_ID, secret_token=SECRET_TOKEN)
logging.getLogger().addHandler(notion_handler)
logging.getLogger().setLevel(logging.DEBUG)

# Activate global exception handler
notion_handler.activate_global_handler()

# Log some messages
logger = logging.getLogger(__name__)

logger.info("This is an informational message 📄")
logger.debug("This is a debug message 🐞")
logger.warning("This is a warning message ⚠️")
logger.error("This is an error message ❌")

# Example of an unhandled exception
def divide(a, b):
    return a / b

try:
    divide(1, 0)
except ZeroDivisionError:
    logger.exception("An exception occurred")

How It Works

Initialization

The NotionLogger class is initialized with a Notion page ID and a secret token. It sets up the necessary headers and creates a database in Notion to store the logs.

Logging

When a log message is emitted, the emit method formats the message and sends it to Notion using the log_to_notion method. Each log entry includes the file name, line number, log type, and the message content.

Exception Handling

The activate_global_handler method sets a global exception handler that catches uncaught exceptions and logs them to Notion.

Requirements

  • Python 3.6+
  • requests library

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

License

This project is licensed under the MIT License. See the LICENSE file for details.


Feel free to reach out with any questions or feedback! Happy logging 📋✨

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

notion_logger-0.0.9.tar.gz (16.4 kB view details)

Uploaded Source

Built Distribution

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

notion_logger-0.0.9-py3-none-any.whl (16.7 kB view details)

Uploaded Python 3

File details

Details for the file notion_logger-0.0.9.tar.gz.

File metadata

  • Download URL: notion_logger-0.0.9.tar.gz
  • Upload date:
  • Size: 16.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for notion_logger-0.0.9.tar.gz
Algorithm Hash digest
SHA256 7d721c0d9005b5b823257cd888e888017608fd75bcf35dee959530eb3fc5a019
MD5 caacc5dd7c5ee7296c560e341927ab12
BLAKE2b-256 c583562abb1a98b8a5d2468e1fdbf725189e8f9b1edfd475694c56bff0509c5e

See more details on using hashes here.

File details

Details for the file notion_logger-0.0.9-py3-none-any.whl.

File metadata

  • Download URL: notion_logger-0.0.9-py3-none-any.whl
  • Upload date:
  • Size: 16.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for notion_logger-0.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 d3234e475694cefcc9a338a6ccd82d3c80bccf30f1173e2231c2e968693b876b
MD5 3e5d5f85247f56ee8bf5f75d3a5f44f6
BLAKE2b-256 b6cd63bfbb1e8a97fb5929e4f1d6de43871f0f92b2115bf7188265503cf1e2c6

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