Log rotate on restart and at midnight

Maybe someone finds this useful. Based on this thread from 2020

Create log_backups inside the config dir.

configuration.yaml:

shell_command:
  log_backup: >-
    cp ./home-assistant.log ./config/log_backups/log-`date +"%Y%m%d%H%M%S"`.log
  log_rotate: >-
    cp ./home-assistant.log ./config/log_backups/log-`date +"%Y%m%d%H%M%S"`.log &&
    truncate -s 0 ./home-assistant.log

Automation:

alias: Rotate log on shutdown and at midnight
description: ""
trigger:
  - platform: homeassistant
    event: shutdown
  - platform: time
    at: "00:00:00"
condition: []
action:
  - action: shell_command.log_rotate
    data: {}
mode: single

Not sure if any logging is lost during the operation (esp. the one invoked at midnight) but should be minimal and better than losing the log completely on restart.

Works on docker supervised.

Without any special configuration there’s always the log of the previous session after the restart in homeassistant.log.1 so what are you losing after a restart?

My case was I wasn’t restarting and the log just grew.

What one’s loosing with last log is all the previous ones with multiple restarts.

HA always wipes the current log file on restart and it always saves the previous log file as .1 in your config folder.

What you’ve done here is created a rotating log based on the days.

You can also do this in HA without an automation depending on what install method you are using. If you’re using core or docker, you can modify the log rotation schedule without an automation.

You can probably do the same with supervised as well, but you might have to modify the setup files.

Yes, exactly.

Haven’t found anything about this. There is a flag you can pass with standalone install but not the supervised one. Happy to be pointed to a documentation or anything really as I haven’t found anything useful for Supervised.

There isn’t any documentation on this, I just know the code.

If you pass --log-rotate-days with a number, you’ll get that behavior

EDIT: There might be documentation somewhere, but I’ve never bothered looking

This is what I found today too but AFAIK it’s not passed to the supervised:

Some other discussions: