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
Metadata
Release files for notspam 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| notspam-1.0.0.tar.gz | 11.5 kB | Without provenance | Details for notspam-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| notspam-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for notspam-1.0.0-py3-none-any.whl |
Total release size: 19.7 kB
Release files / notspam-1.0.0.tar.gz
| Download URL | notspam-1.0.0.tar.gz |
|---|---|
| Size | 11.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d568696aac7846f038c0944b0e73b2142e4cd59e03d0de25ad4dda7f04cc8f23
|
|
BLAKE2b-256 checksum How to use checksums |
17d3d31429502887d6777d84bff8cb8aec84c4e9b538ebd8fec8e0591d1b8445
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.3
|
Release files / notspam-1.0.0-py3-none-any.whl
| Download URL | notspam-1.0.0-py3-none-any.whl |
|---|---|
| Size | 8.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c5c76992c137afa43e2012fa83b1f5ad2de69178f4a9784e93bccda99d606661
|
|
BLAKE2b-256 checksum How to use checksums |
229cce41f4d8fa53941e2c7a628eec34f7e40f9dfb1a5d7b3012d042c853dc44
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.3
|
Security for notspam 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.