3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MemoryHandler,BufferingHandlerのcapacityは何を指しているのか

3
Last updated at Posted at 2018-08-18

Pythonのlogging.handlerに含まれるMemoryHandlerはログをメモリにバッファリングできるモジュールです。

下記のように使用するとファイルのへの出力がメモリにバッファされます。

import logging.handlers
logger = logging.getLogger()
file_handler = logging.FileHandler("/tmp/root.log")
mem_handler = logging.handlers.MemoryHandler(capacity=100, target=file_handler)
logger.addHandler(mem_handler)

定義は下記のようになっているのですがcapacityの単位が不明でした。
バイトなのかメガバイトなのか文字数なのか行数なのか・・・。
ドキュメントにもThe instance is initialized with a buffer size of capacityと書いてあるだけでした。

class logging.handlers.MemoryHandler(capacity, flushLevel=ERROR, target=None, flushOnClose=True)

仕方ないのでソースコードを読んでみることにしました。
capacityは継承されているBufferingHandler由来なのでそっちを読んでみます。
https://github.com/python/cpython/blob/master/Lib/logging/handlers.py#L1202

どうやらMemoryHandler,BufferingHandlerのcapacityはメモリ容量でも文字数でもなく__ログ行数__を指しているようです。

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?