Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use file and stdout log outputs #384

Merged
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
66 changes: 45 additions & 21 deletions backend/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,44 @@

CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))

def setup_logs(log_dir: str) -> logging.Logger:
"""Setups environment for logs

Args:
log_dir (str): folder for log storage
logging.Logger: logger object
"""
formatter = GelfFormatter()
logger = logging.getLogger("Basegun")
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(formatter)
logger.addHandler(handler)
return logger
def setup_logs(log_dir: str):
os.makedirs(log_dir, exist_ok=True)

logging_config = {
"version": 1,
'disable_existing_loggers': False,
"formatters": {
'standard': {
'()': lambda: GelfFormatter()
}
},
"handlers": {
'default': {
'class': 'logging.StreamHandler',
'formatter': 'standard',
'level': "INFO",
'stream': 'ext://sys.stdout'
},
'file': {
'class': 'logging.handlers.TimedRotatingFileHandler',
'when': 'midnight',
'utc': True,
'backupCount': 5,
'level': "INFO",
'filename': f'{log_dir}/log.json',
'formatter': 'standard',
},
},
"loggers": {
"": {
'handlers': ['default', 'file'],
'level': "DEBUG"
}
}
}

logging.config.dictConfig(logging_config)


def get_device(user_agent) -> str:
Expand Down Expand Up @@ -107,7 +131,7 @@ def upload_image(content: bytes, image_key: str):
"bg_upload_time": time.time() - start,
"bg_image_url": image_key,
}
logger.info("Upload successful", extra=extras_logging)
logging.info("Upload successful", extra=extras_logging)


####################
Expand Down Expand Up @@ -146,7 +170,7 @@ async def add_owasp_middleware(request: Request, call_next):

# Logs
PATH_LOGS = os.environ.get("PATH_LOGS", "/tmp/logs")
logger = setup_logs(PATH_LOGS)
setup_logs(PATH_LOGS)

# Load model
app.model = load_model_inference("./model.pt")
Expand All @@ -165,7 +189,7 @@ async def add_owasp_middleware(request: Request, call_next):
APP_VERSION = versions["app"]
MODEL_VERSION = versions["model"]
else:
logger.warn("File versions.json not found")
logging.warn("File versions.json not found")
APP_VERSION = "-1"
MODEL_VERSION = "-1"

Expand Down Expand Up @@ -227,7 +251,7 @@ async def imageupload(
else:
extras_logging["bg_confidence_level"] = "high"

logger.info("Identification request", extra=extras_logging)
logging.info("Identification request", extra=extras_logging)

return {
"path": img_key,
Expand All @@ -238,7 +262,7 @@ async def imageupload(

except Exception as e:
extras_logging["bg_error_type"] = e.__class__.__name__
logger.exception(e, extra=extras_logging)
logging.exception(e, extra=extras_logging)
raise HTTPException(status_code=500, detail=str(e))


Expand All @@ -253,7 +277,7 @@ async def log_feedback(request: Request, user_id: Union[str, None] = Cookie(None
for key in ["image_url", "label", "confidence", "confidence_level"]:
extras_logging["bg_" + key] = res[key]

logger.info("Identification feedback", extra=extras_logging)
logging.info("Identification feedback", extra=extras_logging)
return


Expand All @@ -277,7 +301,7 @@ async def log_tutorial_feedback(
]:
extras_logging["bg_" + key] = res[key]

logger.info("Tutorial feedback", extra=extras_logging)
logging.info("Tutorial feedback", extra=extras_logging)
return


Expand All @@ -301,7 +325,7 @@ async def log_identification_dummy(
]:
extras_logging["bg_" + key] = res[key]

logger.info("Identification dummy", extra=extras_logging)
logging.info("Identification dummy", extra=extras_logging)
return


Expand Down
1 change: 0 additions & 1 deletion backend/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ def test_upload(self):
if os.environ["WORKSPACE"] == "dev":
create_bucket()
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "revolver.jpg")
geoloc = "12.666,7.666"

with open(path, "rb") as f:
r = client.post(
Expand Down
Loading