8

目前我正在使用 gunicorn 作为守护进程运行我的 sanic(微框架)网络服务,我想将所有日志保存在文件中(访问和错误)

我的配置:

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'

接下来我启动网络服务:

gunicorn --config config.py app:app

Sooo ..我的错误日志有效,但我绝对没有访问日志..

文档中没有关于此问题的提示,有人可以帮助我吗?

谢谢和问候!

4

5 回答 5

19

你能试试:

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

并查看标准输出(控制台输出)上是否记录了任何内容?

于 2018-04-07T12:16:29.740 回答
0

我认为您可以直接从 sanic 框架中直接获取。

检查链接

如何将默认的 sanic 日志目录更改为自定义目录?

于 2018-04-09T15:16:31.887 回答
0

使用 supervisord 启动带有日志参数的 gunicorn 服务。

[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

这对我来说非常有效,所以我可以tail -f log/api_stderr.log

于 2018-06-01T08:20:10.623 回答
0

您应该将日志配置传递给 Sanic 应用程序,并将文件而不是 sys.stdout 设置为访问日志处理程序的流。

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

您可以从 sanic 文件夹中的 log.py 复制默认配置

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"
        },
    }
)

即使您使用 gunicorn 运行应用程序,这也将起作用。

于 2018-04-09T20:03:19.670 回答
0

如果您正在使用logging.config.fileConfig('someFile.conf'),请确保您使用disable_existing_loggers=False参数,因此它将是:

logging.config.fileConfig('someFile.conf', disable_existing_loggers=False)

此处的文档:https ://docs.python.org/3.7/library/logging.config.html#logging.config.fileConfig

否则访问日志将被禁止。

让现有记录器保持活动状态可能会导致出现其他日志,例如来自requestsurllib3库。不用担心,如果您不需要它们,只需在下一行禁用:

logging.getLogger("urllib3").setLevel(logging.WARNING)
于 2019-12-18T10:48:58.220 回答