Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[hma] Enable custom logging format (#1582)
- Loading branch information
Showing
4 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
hasher-matcher-actioner/reference_omm_configs/json_log_omm_config.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
|
||
""" | ||
A development version of an omm_config, with every field visible and commented. | ||
This configuration is identical to development_omm_config.py, except that it | ||
enables json log formatting. | ||
""" | ||
|
||
import logging | ||
from logging.config import dictConfig | ||
|
||
from OpenMediaMatch.storage.postgres.impl import DefaultOMMStore | ||
from OpenMediaMatch.utils.fetch_benchmarking import InfiniteRandomExchange | ||
from OpenMediaMatch.utils.formatters import CustomJsonFormatter | ||
from threatexchange.signal_type.pdq.signal import PdqSignal | ||
from threatexchange.signal_type.md5 import VideoMD5Signal | ||
from threatexchange.content_type.photo import PhotoContent | ||
from threatexchange.content_type.video import VideoContent | ||
from threatexchange.exchanges.impl.static_sample import StaticSampleSignalExchangeAPI | ||
from threatexchange.exchanges.impl.ncmec_api import NCMECSignalExchangeAPI | ||
from threatexchange.exchanges.impl.stop_ncii_api import StopNCIISignalExchangeAPI | ||
from threatexchange.exchanges.impl.fb_threatexchange_api import ( | ||
FBThreatExchangeSignalExchangeAPI, | ||
) | ||
|
||
# Database configuration | ||
DBUSER = "media_match" | ||
DBPASS = "hunter2" | ||
DBHOST = "db" | ||
DBNAME = "media_match" | ||
DATABASE_URI = f"postgresql+psycopg2://{DBUSER}:{DBPASS}@{DBHOST}/{DBNAME}" | ||
|
||
# Role configuration | ||
PRODUCTION = False | ||
ROLE_HASHER = True | ||
ROLE_MATCHER = True | ||
ROLE_CURATOR = True | ||
# APScheduler (background threads for development) | ||
TASK_FETCHER = True | ||
TASK_INDEXER = True | ||
TASK_INDEX_CACHE = True | ||
|
||
# Core functionality configuration | ||
STORAGE_IFACE_INSTANCE = DefaultOMMStore( | ||
signal_types=[PdqSignal, VideoMD5Signal], | ||
content_types=[PhotoContent, VideoContent], | ||
exchange_types=[ | ||
StaticSampleSignalExchangeAPI, | ||
InfiniteRandomExchange, # type: ignore | ||
FBThreatExchangeSignalExchangeAPI, # type: ignore | ||
NCMECSignalExchangeAPI, # type: ignore | ||
StopNCIISignalExchangeAPI, | ||
], | ||
) | ||
|
||
FLASK_LOGGING_CONFIG = dictConfig( | ||
{ | ||
"version": 1, | ||
"formatters": { | ||
"default": { | ||
"format": "[%(asctime)s] %(levelname)s in %(module)s: %(message)s", | ||
}, | ||
"json": {"()": "OpenMediaMatch.utils.formatters.CustomJsonFormatter"}, | ||
}, | ||
"handlers": { | ||
"wsgi": { | ||
"class": "logging.StreamHandler", | ||
"stream": "ext://flask.logging.wsgi_errors_stream", | ||
"formatter": "json", | ||
} | ||
}, | ||
"root": {"level": "INFO", "handlers": ["wsgi"]}, | ||
} | ||
) | ||
|
||
# Debugging stuff | ||
# SQLALCHEMY_ENGINE_LOG_LEVEL = logging.INFO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
hasher-matcher-actioner/src/OpenMediaMatch/utils/formatters.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from pythonjsonlogger import jsonlogger | ||
|
||
|
||
class CustomJsonFormatter(jsonlogger.JsonFormatter): | ||
""" | ||
Outputs logs in a JSON format. | ||
""" | ||
|
||
def add_fields(self, log_record, record, message_dict): | ||
log_record["level"] = record.__dict__.get("levelname") | ||
log_record["timestamp"] = self.formatTime(record) | ||
super(CustomJsonFormatter, self).add_fields(log_record, record, message_dict) |