Skip to content

Commit

Permalink
Enable custom logging format
Browse files Browse the repository at this point in the history
  • Loading branch information
jagraff committed Apr 25, 2024
1 parent 22b77a4 commit de826c8
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions hasher-matcher-actioner/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dependencies = [
"threatexchange",
"flask_apscheduler",
"python-dateutil",
"python-json-logger",
]

[project.optional-dependencies]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.

"""
A development version of an omm_config, with every field visible and commented.
This is the configuration that is used by default for the developer instance
which runs in the dev container by default. Every config field is present
to make it easier to copy the file as a template for others.
"""

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
4 changes: 4 additions & 0 deletions hasher-matcher-actioner/src/OpenMediaMatch/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ def create_app() -> flask.Flask:
SQLALCHEMY_TRACK_MODIFICATIONS=False,
)

logging_config = app.config.get("FLASK_LOGGING_CONFIG")
if logging_config:
logging.config.dictConfig(logging_config)

running_migrations = os.getenv("MIGRATION_COMMAND") == "1"

engine_logging = app.config.get("SQLALCHEMY_ENGINE_LOG_LEVEL")
Expand Down
11 changes: 11 additions & 0 deletions hasher-matcher-actioner/src/OpenMediaMatch/utils/formatters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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)

0 comments on commit de826c8

Please sign in to comment.