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

The log file will not be created automatically #471

Closed
blackAzrael opened this issue Jun 23, 2021 · 6 comments
Closed

The log file will not be created automatically #471

blackAzrael opened this issue Jun 23, 2021 · 6 comments
Labels
feature Request for adding a new feature

Comments

@blackAzrael
Copy link

blackAzrael commented Jun 23, 2021

from loguru import logger
logger.add("log/waf_api.log",  rotation="00:00", encoding="utf-8", enqueue=True, retention="2 months")
import time
if __name__ == "__main__":

    while True:
        logger.debug("11111")
        time.sleep(1)

When I delete my log file manually when the program is running, the log file waf_api.log will not be created. How can I solve this?
😕

@Delgan
Copy link
Owner

Delgan commented Jun 23, 2021

For optimizations reasons, the file is opened once while starting the logger. The logged messages are then written to the file object. If the file has been deleted, then the file object is still valid but will point to nothing. However, the file will not be recreated because it will not been reopened.

You can use a custom sink to open() the file before each write:

def sink(message):
    with open("log/waf_api.log", "a") as file:
        file.write(message)

logger.add(sink)

Unfortunately, that means you won't be able to use custom arguments like rotation and retention, you will need to re-implement it by yourself.

You could also try to force rotation if file does not exist:

def rotation(message, file):
    return not os.path.isfile(file.name)

logger.add("log/waf_api.log", rotation)

However, it generates an error because Loguru does not expect the file to be deleted.

Obviously, the easiest solution is to avoid deleting a file in use if possible. As we can see, this raises some problems.

@blackAzrael
Copy link
Author

If the log file is lost in some cases, it will not be able to record the log. Will this problem be solved in the future?

@Delgan
Copy link
Owner

Delgan commented Jun 23, 2021

I don't really plan to integrate it to Loguru. If your file is likely to be deleted, you'll have to create your own handler and use it to re-open the file.

@blackAzrael
Copy link
Author

blackAzrael commented Jun 24, 2021

If in a large-scale project, the operator mistakenly deletes some log files, and re-creates the file with the same name. The log cannot be written, which will lose a lot of logs. I think this is a problem.🥺

@Delgan
Copy link
Owner

Delgan commented Jun 24, 2021

Well... I was going to suggest you to use the WatchedFileHandler from standard library or WatchedRotatingFileHandler from third party. Since Loguru supports standard Handler, it should work fine.

However, that made me realize this could easily be integrated to Loguru as a new parameter while adding a file sink.

logger.add("file.log", watch=True)

It shouldn't be too cumbersome, and it is surely useful as demonstrated by your example and by the existence of such handler in the standard library.

I will add it in the next version. Thanks for insisting. 🙂

@Delgan Delgan added the feature Request for adding a new feature label Jun 24, 2021
@Delgan
Copy link
Owner

Delgan commented May 2, 2022

The new watch option is implemented on master branch and will be available in next release.

As discussed, if this option is set to True, then Loguru will re-create the file if it detects it has been deleted or modified (if its device or inode changes).

@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
feature Request for adding a new feature
Projects
None yet
Development

No branches or pull requests

2 participants