Gunicorn - No access logs

偶尔善良 提交于 2019-12-11 00:07:13

问题


currently i am running my sanic(microframework) webservice with gunicorn as a daemon and i would like to save all logs in files(access and error)

My config:

reload = True
daemon = True
bind = '0.0.0.0:6666'
worker_class = 'sanic.worker.GunicornWorker'
loglevel = 'debug'
accesslog = 'access.log'
access_log_format = '%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"'
errorlog = 'error.log'

Next i start the webservice:

gunicorn --config config.py app:app

Sooo.. my errorlog works, but i get absolutely no accesslogs..

There is no hint in the documentation about this issue, could anybody help me?

Thanks and Greetings!


回答1:


could you try :

gunicorn --config config.py app:app --access-logfile '-'

And see if anything is logged on stdout(console output)?




回答2:


I think you could simply from the sanic framework directly.

check the link

How to change the default sanic log directory to a custom directory?




回答3:


You should pass a log config to the Sanic App and set the a file instead of sys.stdout as stream for the access log handler.

app = Sanic('test', log_config=CUSTOM_LOGIN_CONFIG)

You can copy the default config from log.py in the sanic folder

accesslog_file = open('accesslog_file.log','w')


CUSTOM_LOGIN_CONFIG = dict(
    version=1,
    disable_existing_loggers=False,

    loggers={
        "root": {
            "level": "INFO",
            "handlers": ["console"]
        },
        "sanic.error": {
            "level": "INFO",
            "handlers": ["error_console"],
            "propagate": True,
            "qualname": "sanic.error"
        },

        "sanic.access": {
            "level": "INFO",
            "handlers": ["access_console"],
            "propagate": True,
            "qualname": "sanic.access"
        }
    },
    handlers={
        "console": {
            "class": "logging.StreamHandler",
            "formatter": "generic",
            "stream": sys.stdout
        },
        "error_console": {
            "class": "logging.StreamHandler",
            "formatter": "generic",
            "stream": sys.stdout
        },
        "access_console": {
            "class": "logging.StreamHandler",
            "formatter": "access",
            "stream": accesslog_file
        },
    },
    formatters={
        "generic": {
            "format": "%(asctime)s [%(process)d] [%(levelname)s] %(message)s",
            "datefmt": "[%Y-%m-%d %H:%M:%S %z]",
            "class": "logging.Formatter"
        },
        "access": {
            "format": "%(asctime)s - (%(name)s)[%(levelname)s][%(host)s]: " +
                      "%(request)s %(message)s %(status)d %(byte)d",
            "datefmt": "[%Y-%m-%d %H:%M:%S %z]",
            "class": "logging.Formatter"
        },
    }
)

This is gonna work even if you run the the app with gunicorn.




回答4:


use supervisord to start the gunicorn service with logging parameters.

[program:sanic]
directory=/home/ubuntu/api
command=/home/ubuntu/api/venv/bin/gunicorn api:app --bind 0.0.0.0:8000 --worker-class sanic.worker.GunicornWorker -w 2
stderr_logfile = log/api_stderr.log
stdout_logfile = log/api_stdout.log

This works perfectly for me so I can just tail -f log/api_stderr.log



来源:https://stackoverflow.com/questions/49706746/gunicorn-no-access-logs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!