Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

日志rotate进行滚动的时候如何自定义滚动的文件名 #529

Closed
Gu-f opened this issue Oct 19, 2021 · 1 comment
Closed

日志rotate进行滚动的时候如何自定义滚动的文件名 #529

Gu-f opened this issue Oct 19, 2021 · 1 comment
Labels
question Further information is requested

Comments

@Gu-f
Copy link

Gu-f commented Oct 19, 2021

通过document看到可以给文件名用{time}去创建带有时间的日志文件名,但是我每找到如何自定义文件名或使用其他的key。
目前生成的滚动文件如下:
error.log
error.2021-10-19_17-42-14_757655.log
error.2021-10-19_17-42-14_757675.log
error.2021-10-19_17-42-14_757755.log
......
我希望能够自定义成如下的文件名
error.log
error.1.log
error.2.log
error.3.log
......
或者是下面这种
error.log
error.log.1
error.log.2
error.log.3
......
当前是否支持这种功能.
如果不支持的话希望能够作为新功能去更新一个版本。

@Delgan
Copy link
Owner

Delgan commented Dec 5, 2021

Hi.

It's possible to customize the name of rotated files using the compression argument with a custom function (which is executed after each successful rotation). It's up to you to implement the logic to avoid two files having the same name, though.

def rename_rotated(filepath):
    head, tail = os.path.split(filepath)
    base, *_ = tail.split(".")
    num = 1
    while os.path.exists(os.path.join(head, f"{base}.{num}.log")):
        num += 1
    os.rename(filepath, os.path.join(head, f"{base}.{num}.log"))

logger.add("error.log", rotation="10 MB", compression=rename_rotated)

@Delgan Delgan added the question Further information is requested label Dec 5, 2021
@Delgan Delgan closed this as completed May 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants