【问题标题】:multiple processors logging to same rotate file多个处理器记录到同一个旋转文件
【发布时间】:2015-12-10 10:26:55
【问题描述】:

我的 nginx+uwsgi+django 站点出现问题。 我知道 django+uwsgi 没什么特别的,应该是日志模块本身的东西。

在我的站点中,我使用 RotatingFileHandler 记录特殊条目,但是,当 uwsgi 与多个工作处理器一起运行时,今天我发现, 多个日志文件同时更改。比如这里是文件sn-p:

[root@speed logs]# ls -lth
total 18M
-rw-rw-rw- 1 root root  2.1M Sep 14 19:44 backend.log.7
-rw-rw-rw- 1 root root  1.3M Sep 14 19:43 backend.log.6
-rw-rw-rw- 1 root root  738K Sep 14 19:43 backend.log.3
-rw-rw-rw- 1 root root  554K Sep 14 19:43 backend.log.1
-rw-rw-rw- 1 root root 1013K Sep 14 19:42 backend.log.4
-rw-rw-rw- 1 root root  837K Sep 14 19:41 backend.log.5
-rw-rw-rw- 1 root root  650K Sep 14 19:40 backend.log.2
-rw-rw-rw- 1 root root  656K Sep 14 19:40 backend.log
-rw-r--r-- 1 root root   10M Sep 13 10:11 backend.log.8
-rw-r--r-- 1 root root     0 Aug 21 15:53 general.log
[root@speed-app logs]#

实际上,我将旋转文件设置为 10M perfile 和最多 10 个文件。

我google了很多,很多人都打过这个,好像日志模块本身不支持这个。

我发现有人提到了 ConcurrentLogHandler(https://pypi.python.org/pypi/ConcurrentLogHandler/0.9.1)。 有人用过这个人吗?我看是基于文件锁的,不知道这家伙性能好不好。

或者任何人有更好的想法将多个 uwsig 实例记录到同一个旋转文件?

谢谢。 卫斯理

【问题讨论】:

标签: python django logging uwsgi


【解决方案1】:

顺便说一句,这里有一个完整的解决方案示例,它使用 python StreamHandler、uWSGI“守护程序文件日志记录”和logrotate 守护程序来循环记录到文件。

如您所见,uWSGI 日志记录从您的应用程序中捕获 stdout/stderr 并将其重定向到 stdout/stderr(默认情况下)或定义的其他记录器/处理程序。

设置 Django/uWSGI

你的 Django settings.py

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'default': {
            'format': '%(asctime)s - %(process)s - %(levelname)s - %(name)s : %(message)s',
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
        },
    },
    'root': {
        'handlers': ['console'],
        'level': 'DEBUG',
    },
}

代码中的某处

log = logging.getLogger(__name__)
log.info("test log!")

使用一些日志参数运行 uWSGI

$ uwsgi --http :9090 --chdir=`pwd -P` --wsgi-file=wsgi.py \
    --daemonize=test.log \  # daemonize AND set log file
    --log-maxsize=10000  \  # a 10k file rotate
    --workers=4             # start 4 workers

输出

test.log 的摘录

*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 79755)
spawned uWSGI worker 1 (pid: 79813, cores: 1)
spawned uWSGI worker 2 (pid: 79814, cores: 1)
spawned uWSGI worker 3 (pid: 79815, cores: 1)
spawned uWSGI worker 4 (pid: 79816, cores: 1)
spawned uWSGI http 1 (pid: 79817)
2015-10-12 07:55:48,458 - 79816 - INFO - testapp.views : test log!
2015-10-12 07:55:51,440 - 79813 - INFO - testapp.views : test log!
2015-10-12 07:55:51,965 - 79814 - INFO - testapp.views : test log!
2015-10-12 07:55:52,810 - 79815 - INFO - testapp.views : test log!

在同一个目录下,过了一会儿:

-rw-r-----   1 big  staff   1.0K Oct 12 09:56 test.log
-rw-r-----   1 big  staff    11K Oct 12 09:55 test.log.1444636554

日志旋转

或者,要自己处理文件旋转,请省略 --log-maxsize 参数并使用 logrotate 配置文件 (/etc/logrotate.d/uwsgi-test-app):

/home/demo/test_django/*log {
    rotate 10
    size 10k
    daily
    compress
    delaycompress
}

请注意,上述值是为了举例说明,您可能不希望旋转大小为 10k。有关 logrotate 格式的更多信息,请参阅an example blog post

【讨论】:

  • 你好,谢谢详细的回复,但是你有没有尝试过uwsgiworker超过1的时候?
  • 我已经验证 uWSGI 工作正常,4 个进程都很好地(轮流)记录到同一个文件,一旦达到大小就会旋转。该示例已更新以显示新的输出
【解决方案2】:

如果你使用 python 的 logrotation(当多个 gunicorn 进程指向同一个日志文件时),那么你应该确保主日志文件只被编辑而不是重命名,移动等回转。为此,您复制主日志文件,然后将其清除!

翻转方法的片段(在logging.handlers.RotatingFileHandler的代码中编辑)

def doRollover(self):
    self.stream.close()
    if self.backupCount > 0:
        for i in range(self.backupCount - 1, 0, -1):
            sfn = "%s.%d" % (self.baseFilename, i)
            dfn = "%s.%d" % (self.baseFilename, i + 1)
            if os.path.exists(sfn):
                if os.path.exists(dfn):
                    os.remove(dfn)
                os.rename(sfn, dfn)
        dfn = self.baseFilename + ".1"
        if os.path.exists(dfn):
            os.remove(dfn)
        # os.rename(self.baseFilename, dfn) # Intead of this
        # Do this
        shutil.copyfile(self.baseFilename, dfn)
        open(self.baseFilename, 'w').close()
    if self.encoding:
        self.stream = codecs.open(self.baseFilename, "w", self.encoding)
    else:
        self.stream = open(self.baseFilename, "w")

然后你可以像这样创建你的记录器:

logger = logging.getLogger(logfile_name)
logfile = '{}/{}.log'.format(logfile_folder, logfile_name)
handler = RotatingFileHandler(
    logfile, maxBytes=maxBytes, backupCount=10
)
formatter = logging.Formatter(format, "%Y-%m-%d_%H:%M:%S")
formatter.converter = time.gmtime
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.INFO)
logger.isEnabledFor = lambda level: True
logger.propagate = 0

logger.warning("This is a log")

【讨论】:

  • 你救了我的命。对您的方法进行一些更新。从 py3.3 开始,有一个 rotator 属性供您覆盖这些代码。 docs.python.org/3/library/…
猜你喜欢
  • 1970-01-01
  • 2020-01-05
  • 2019-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-27
  • 2020-01-20
  • 1970-01-01
相关资源
最近更新 更多