Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions aind-dataverse-service-server/log_config_template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
version: 1
disable_existing_loggers: false
formatters:
default:
"()": aind_dataverse_service_server.CustomJsonFormatter
format: "%(asctime)s %(levelname)s %(module)s %(lineno)s %(message)s"
reserved_attrs:
- args
- asctime
- created
- exc_info
- exc_text
- filename
- funcName
- levelname
- levelno
- lineno
- message
- module
- msecs
- msg
- name
- pathname
- process
- processName
- relativeCreated
- stack_info
- taskName
- thread
- threadName
- color_message
rename_fields:
"asctime": "timestamp"
"levelname": "level"
static_fields:
"environment": "local"
"software_version": ext://aind_dataverse_service_server.__version__
"software_name": "aind-dataverse-service"
handlers:
console:
class: logging.StreamHandler
formatter: default
level: INFO
stream: ext://sys.stdout
loggers:
'':
handlers:
- console
level: INFO
propagate: true
uvicorn.error:
handlers:
- console
level: INFO
propagate: false
uvicorn.access:
handlers:
- console
level: INFO
propagate: false
2 changes: 2 additions & 0 deletions aind-dataverse-service-server/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ dependencies = [
'fastapi[standard]>=0.114.0',
'azure-identity>=1.15.0',
'fastapi-cache2[redis]>=0.2.2',
'python-json-logger',
'PyYAML'
]

[project.optional-dependencies]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,42 @@
"""Package for pulling info from Dataverse"""

import logging.config
import os
from datetime import datetime, timezone
from logging import LogRecord

import yaml
from pythonjsonlogger import json as log_json

__version__ = "0.1.0"


# We want to standardize the timestamp format to UTC and ISO-8601, which
# requires a custom formatter and can't be done through configuration only.
class CustomJsonFormatter(log_json.JsonFormatter):
"""Custom class to format log timestamps as ISO-8601 UTC"""

def formatTime(self, record: LogRecord, datefmt=None) -> str:
"""
Format timestamp as ISO-8601 UTC
Parameters
----------
record : LogRecord
datefmt : str, optional
Default is None

Returns
-------
str

"""
dt = datetime.fromtimestamp(record.created, tz=timezone.utc)
return dt.strftime("%Y-%m-%dT%H:%M:%S.%fZ")


if os.path.isfile(os.getenv("LOGGING_CONFIG_FILE", "log_config.yaml")):
config_path = os.getenv("LOGGING_CONFIG_FILE", "log_config.yaml")
with open(config_path, "rt") as f:
config = yaml.safe_load(f.read())
logging.config.dictConfig(config)
logging.info(f"Found logging file at: {config_path}")
Loading