benoitc / gunicorn

gunicorn 'Green Unicorn' is a WSGI HTTP Server for UNIX, fast clients and sleepy applications.
http://www.gunicorn.org
Other
9.86k stars 1.75k forks source link

No logs in the docker container #3292

Open manas007 opened 2 months ago

manas007 commented 2 months ago

I am trying to run a simple app and i can't seem to get any logs when my container is up. I am unable to determine the issue and I have read multiple open/close threads about it.

Here is my dockerfile:

ARG PYTHON_VERSION=3.11.3
# application in prod or dev
ARG APPLICATION_ENVIRONMENT='prod' 
FROM python:${PYTHON_VERSION}-slim as base

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV PYTHONPATH=/src

WORKDIR /src

ARG UID=10001
RUN adduser \
    --disabled-password \
    --gecos "" \
    --home "/nonexistent" \
    --shell "/sbin/nologin" \
    --no-create-home \
    --uid "${UID}" \
    appuser

RUN --mount=type=cache,target=/root/.cache/pip \
    --mount=type=bind,source=requirements.txt,target=requirements.txt \
    python -m pip install -r requirements.txt

USER appuser

COPY . .

EXPOSE 5000

CMD gunicorn 'src.app:server' --bind=0.0.0.0:5000 -e APP_ENV=${APPLICATION_ENVIRONMENT} --access-logfile -

here is my bare minimum app.py, and the src.utils.setting is where i am reading env variables from .env file and doing some logging, based on the APPLICATION_ENVIRONMENT .

import logging
from flask import Flask
import dash
from dash import html, dcc, callback, Output, Input, State, no_update
from dash.exceptions import PreventUpdate
import plotly.graph_objects as go
import plotly.express as px

from src.utils.setting import APP_HOST, APP_PORT, APP_DEBUG, DEV_TOOLS_PROPS_CHECK
import dash_bootstrap_components as dbc
from src.components import navbar
from src.service.ede_service import EdeService

server = Flask(__name__)
# bootstrap theme for the page
app = dash.Dash(__name__, server=server, external_stylesheets=[dbc.themes.FLATLY])

app.layout = html.Div(
    [
        dbc.Container([ 
            dbc.Row([
                dbc.Col(children='Start Date', width=1),
                dbc.Col(dbc.Input(id="start_date", type="date"), width=3),
                dbc.Col(children='End Date', width=1),
                dbc.Col(dbc.Input(id="end_date", type="date"), width=3),
                ])
        ]),
    ]
)

if __name__ == "__main__":
    logging.info("Starting the Dash Application")
    try:
        app.run(
        host=APP_HOST,
        port=APP_PORT,
        debug=APP_DEBUG,
        dev_tools_props_check=DEV_TOOLS_PROPS_CHECK
        )
    except Exception as e:
        logging.error(f'failed to start the server : {e}', exc_info=True)

in the container logs, i can only see [2024-09-09 21:49:28 +0000] [7] [INFO] Starting gunicorn 22.0.0 [2024-09-09 21:49:28 +0000] [7] [INFO] Listening at: http://0.0.0.0:5000 (7) [2024-09-09 21:49:28 +0000] [7] [INFO] Using worker: sync [2024-09-09 21:49:28 +0000] [8] [INFO] Booting worker with pid: 8

however the logs in the module from src.utils.setting import APP_HOST, APP_PORT, APP_DEBUG, DEV_TOOLS_PROPS_CHECK do not show up at all.

i am very confused as to what to do to accomplish stdout logging

dzhygun commented 1 month ago

Hi, @manas007!

I believe, that is neither gunicorn nor docker issue. That is a pure python.

References:

  1. https://docs.python.org/3/howto/logging-cookbook.html
  2. https://docs.python.org/3/library/logging.html

First of all, learn how logging module works - when you initialize a Logger instance, or initialize application you need to set a configuration.

To direct your logs to stdout try following:

` import logging

logger = logging.getLogger(name) logger.setLevel(logging.INFO) logger.info("your log info...") `

However, since you use Flask, you should configure logging through the flask application config: https://flask.palletsprojects.com/en/latest/logging/