How can I rotate / limit the size of home-assistant.log file in docker

I’ve found a lot of topics about limiting the size of this file, but most seem to only talk about command line options, like --log-rotate-days. I run HA from a docker-compose file, so AFAIK, I can’t use command line options.

So, to keep it short:
In 2023, running HA in a Docker container, how do we limit the size of the home-assistant.log file found in the config folder?

Well, I’d say that if everything work fine, log file is not getting large enough to rotate it between restarts (accompanying core updates). If it goes big, then most likely there is some prblem in configuration/setup, that causes so many lines to be recorded and eliminating these errors should be the focus. Said that all I did was to set default logging level to warning and set it to error for some integrations that throws some warning messages, even if work perfectly and there is nothing to be done. After that if I see that my log is more than 50 lines long I know I need to dig into details…

1 Like

While that may be true, it’s not the answer to my question (well it is, since I asked about limiting the size of the log, but I meant limiting as in doing something when it reaches a certain size). Limiting the amount of input the log gets doesn’t prevent it from growing too large, it only extends the amount of time it takes to reach the problematic size. That being said, I did have an excessive amount of debug logging in there that was completely unnecessary.

I still think there should be a way to rotate the log file though. The question is: Is there a way?

Fair point! Even with precausions I applied I experienced in the past cases wher due to some cloud service unavailability logger put something like 300k lines of errors into log within 24h! And then it becomes unmanageable to read and troubleshoot. But I do not think at the moment there is any build in functinality to achieve log limiting/rotation. Perhaps some 3rd party tools that could help, like externall script monitoring size of log and doing some magic…

There is an option to limit number of entries in the log file:
max_entries
But this option does not work.
I created an issue on GitHub, so far no any movement there.

1 Like

I agree this is a problem. I run my logs at info level as that provides enough data to troubleshoot most things after the fact. I’d like to be able to roll the log file over every day and keep N days.

2 Likes

Old topic on which I faced a similar issue (huge log file) and that I bump up for those with similar question.

After investigating, I realised that there are actually two different « logs »: an « in-memory » one which is the one on which the max_entries setting applies (this is the log that can be seen through the UI) and a more traditional one, file based. The max_entries setting has absolutely no impact on the file based topic as this last one is handled through the python logging facility and this is the reason why the log file can grow infinitely (default as per source code, which one could argue is bad design, but this is not the point).

The only options that can be changed through the command line when launching homeassistant are:
—log-rotate-days
—log-file
see: https://github.com/home-assistant/core/blob/fb3105bdc004ec8283fd61966c68509b1ade596c/homeassistant/main.py#L134

If launching through docker (which is my case), the log also comes as a docker log, making the log file redundant. My option has thus been simply to set the log-file option to /dev/null (on Linux of course, not sure what this would be for Windows). This part is a bit tricky as it requires to modify the init script which launches homeassistant. For that I mounted the directory containing the startup script as another volume (-v /config:/etc/services.d/home-assistant), copied there both the startup and exit scripts (called « run » and « finish ») and modify the run script accordingly by adding —log-file /dev/null to the last line of the script (exec python etc.).

The best would be to consider more options through some configuration file, but this would require more changes to the source code.

I hope this will help the ones facing the same issue. Happy to explain further if not clear enough.

This is an interesting info, but it should be definitely reflected in Docs.
Currently it is clearly said that


So either there is a bug or Docs are outdated.
In both cases there should be some feedback from Devs - which I do not see in Github issue.