Skip to content

Commit

Permalink
logging fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
hubsmoke committed Jun 28, 2024
1 parent f80202a commit 9da956d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 41 deletions.
10 changes: 8 additions & 2 deletions app/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@
from flask import request, abort
from functools import wraps
import os
import logging
from logging_config import setup_logging

VALID_API_KEY = os.environ.get('AM_API_KEY')

setup_logging()
logger = logging.getLogger(__name__)

def validate_api_key(func):
@wraps(func)
def wrapper(*args, **kwargs):
api_key = request.headers.get('X-API-Key')
print(api_key)
print(VALID_API_KEY)
logger.info(f"Received API Key: {api_key}")
logger.info(f"Expected API Key: {VALID_API_KEY}")
if api_key != VALID_API_KEY:
logger.warning('Invalid API key provided')
abort(401, 'Invalid API key')
return func(*args, **kwargs)
return wrapper
46 changes: 46 additions & 0 deletions app/logging_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# logging_config.py
import logging
import json
from logging.config import dictConfig

class JsonFormatter(logging.Formatter):
def format(self, record):
log_record = {
'level': record.levelname,
'time': self.formatTime(record, self.datefmt),
'message': record.getMessage(),
'name': record.name,
'pathname': record.pathname,
'lineno': record.lineno,
}
if record.exc_info:
log_record['exc_info'] = self.formatException(record.exc_info)
return json.dumps(log_record)

def setup_logging():
dictConfig({
'version': 1,
'formatters': {
'json': {
'()': JsonFormatter,
}
},
'handlers': {
'stdout': {
'class': 'logging.StreamHandler',
'formatter': 'json',
'stream': 'ext://sys.stdout',
},
'file': {
'class': 'logging.FileHandler',
'formatter': 'json',
'filename': 'app.log',
}
},
'root': {
'level': 'INFO',
'handlers': ['stdout', 'file']
}
})

setup_logging()
40 changes: 3 additions & 37 deletions app/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,14 @@
# Import your script
from langchain_orcid2 import run as run_langchain
from auth import validate_api_key

import logging
import json
from logging.config import dictConfig
from logging_config import setup_logging

# Read environment variables
cr_mailto = os.getenv('CR_MAILTO')
pyalex_email = os.getenv('PYALEX_EMAIL')

# Configure logging
class JsonFormatter(logging.Formatter):
def format(self, record):
log_record = {
'level': record.levelname,
'time': self.formatTime(record, self.datefmt),
'message': record.getMessage(),
'name': record.name,
'pathname': record.pathname,
'lineno': record.lineno,
}
if record.exc_info:
log_record['exc_info'] = self.formatException(record.exc_info)
return json.dumps(log_record)

dictConfig({
'version': 1,
'formatters': {
'json': {
'()': JsonFormatter,
}
},
'handlers': {
'stdout': {
'class': 'logging.StreamHandler',
'formatter': 'json',
'stream': 'ext://sys.stdout',
}
},
'root': {
'level': 'INFO',
'handlers': ['stdout']
}
})

app = Flask(__name__)

Expand All @@ -64,7 +30,7 @@ def log_request_info():
if len(body) > 1000: # Limit body size for logging
body = body[:1000] + '... [truncated]'

app.logger.info('Request received', extra={
app.logger.info('Request', extra={
'method': request.method,
'url': request.url,
'headers': dict(request.headers),
Expand Down
2 changes: 1 addition & 1 deletion kubernetes/deployment_dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ spec:
name: automating-metadata-dev
command: ['/bin/bash', '-c']
args:
- echo "SOURCING ENV"; source /vault/secrets/config; flask run --host=0.0.0.0 --port=5001
- echo "SOURCING ENV"; source /vault/secrets/config; gunicorn -w 4 -b 0.0.0.0:5001 server:app
ports:
- containerPort: 5001
name: server-api
Expand Down
2 changes: 1 addition & 1 deletion kubernetes/deployment_prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ spec:
name: automating-metadata-prod
command: ['/bin/bash', '-c']
args:
- echo "SOURCING ENV"; source /vault/secrets/config; flask run --host=0.0.0.0 --port=5001
- echo "SOURCING ENV"; source /vault/secrets/config; gunicorn -w 4 -b 0.0.0.0:5001 server:app
ports:
- containerPort: 5001
name: server-api
Expand Down

0 comments on commit 9da956d

Please sign in to comment.