Skip to main content

Ultra-lightweight log wrapper that prevents duplicate logs within a specified time window

Project description

notspam

一定時間内に同一ログを出さない超軽量ログラッパー

概要

notspamは、スパムログ防止・モニタリング用の超軽量ログラッパーです。同じログメッセージが一定時間内に繰り返し出力されることを防ぎ、ログの可読性を向上させます。

特徴

  • 超軽量: 標準ライブラリのみ使用、外部依存なし
  • ワンライナー設置: 簡単な設定で即座に使用可能
  • 全ログレベル対応: DEBUG, INFO, WARNING, ERROR, CRITICAL全てに対応
  • スレッドセーフ: マルチスレッド環境でも安全に使用可能
  • メモリ効率: 自動的に古いログ履歴をクリーンアップ
  • カスタマイズ可能: 抑制時間やロガー名を自由に設定

インストール

pip install notspam

使用方法

基本的な使用法

import notspam

# デフォルト設定(60秒間抑制)
logger = notspam.get_logger()

# 最初のログは出力される
logger.info("This is a test message")

# 60秒以内の同じメッセージは抑制される
logger.info("This is a test message")  # 抑制される

# 異なるメッセージは出力される
logger.info("This is a different message")  # 出力される

カスタム設定

import notspam

# 30秒間抑制、カスタムロガー名
logger = notspam.get_logger(suppress_seconds=30, name="my_app")

logger.warning("Warning message")
logger.error("Error message")

複数のロガーを使用

import notspam

# 異なる設定の複数のロガー
api_logger = notspam.create_logger(suppress_seconds=120, name="api")
db_logger = notspam.create_logger(suppress_seconds=60, name="database")

api_logger.info("API request received")
db_logger.error("Database connection failed")

全てのログレベルを使用

import notspam
import logging

logger = notspam.get_logger(level=logging.DEBUG)

logger.debug("Debug message")
logger.info("Info message")
logger.warning("Warning message")
logger.error("Error message")
logger.critical("Critical message")

API リファレンス

get_logger(suppress_seconds=60, name=None, level=logging.INFO)

グローバルロガーインスタンスを取得します。

パラメータ:

  • suppress_seconds (int): 同じログを抑制する秒数(デフォルト: 60)
  • name (str): ロガー名(デフォルト: "notspam")
  • level (int): ログレベル(デフォルト: logging.INFO)

戻り値:

  • NotSpamLogger: ロガーインスタンス

create_logger(suppress_seconds=60, name=None, level=logging.INFO)

新しいロガーインスタンスを作成します。

パラメータ:

  • suppress_seconds (int): 同じログを抑制する秒数(デフォルト: 60)
  • name (str): ロガー名(デフォルト: "notspam")
  • level (int): ログレベル(デフォルト: logging.INFO)

戻り値:

  • NotSpamLogger: 新しいロガーインスタンス

NotSpamLogger クラス

メソッド

  • debug(message, *args, **kwargs): DEBUGレベルのログ
  • info(message, *args, **kwargs): INFOレベルのログ
  • warning(message, *args, **kwargs): WARNINGレベルのログ
  • error(message, *args, **kwargs): ERRORレベルのログ
  • critical(message, *args, **kwargs): CRITICALレベルのログ
  • set_level(level): ログレベルを設定
  • get_suppressed_count(): 現在抑制中のメッセージ数を取得
  • clear_history(): ログ履歴をクリア

使用例

Webアプリケーションでの使用

import notspam
from flask import Flask

app = Flask(__name__)
logger = notspam.get_logger(suppress_seconds=300, name="webapp")

@app.route("/api/data")
def get_data():
    try:
        # データベースアクセス
        data = fetch_data()
        logger.info("Data fetched successfully")
        return data
    except Exception as e:
        # エラーログの重複を防ぐ
        logger.error(f"Failed to fetch data: {e}")
        return {"error": "Internal server error"}, 500

定期実行スクリプトでの使用

import notspam
import schedule
import time

logger = notspam.get_logger(suppress_seconds=60, name="scheduler")

def job():
    try:
        # 何らかの処理
        result = process_data()
        logger.info("Job completed successfully")
    except Exception as e:
        # 1分間同じエラーログを抑制
        logger.error(f"Job failed: {e}")

schedule.every(10).seconds.do(job)

while True:
    schedule.run_pending()
    time.sleep(1)

ライセンス

MIT License

作者

tikipiya

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

notspam-1.0.0.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

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

notspam-1.0.0-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file notspam-1.0.0.tar.gz.

File metadata

  • Download URL: notspam-1.0.0.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for notspam-1.0.0.tar.gz
Algorithm Hash digest
SHA256 d568696aac7846f038c0944b0e73b2142e4cd59e03d0de25ad4dda7f04cc8f23
MD5 b9c688a918d86ab42a21a05d8fdd8b26
BLAKE2b-256 17d3d31429502887d6777d84bff8cb8aec84c4e9b538ebd8fec8e0591d1b8445

See more details on using hashes here.

File details

Details for the file notspam-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: notspam-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for notspam-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c5c76992c137afa43e2012fa83b1f5ad2de69178f4a9784e93bccda99d606661
MD5 4150d4b1c61f49307cc6bb758bfc9e34
BLAKE2b-256 229cce41f4d8fa53941e2c7a628eec34f7e40f9dfb1a5d7b3012d042c853dc44

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