awk

Nettoyage de fichier de logs

Pour supprimer les lignes de logs DEBUG et TRACE sur de multiples lignes, exemple :

2023/02/23 11:51:13 - /static/images/favicon.png - 127.0.0.1 - anonymous - TRACE - URL: Register pattern '|^$|' on HTTP POST with :
array(4) {
  ["handler"]=>
  string(13) "default_index"
  ["additional_info"]=>
  array(0) {
  }
  ["authenticated"]=>
  bool(false)
  ["api_mode"]=>
  bool(false)
}
awk '
BEGIN { buffer = ""; ignore = 0 }
{
    if ($0 ~ /^[0-9]{4}\/[0-9]{2}\/[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} - /) {
        if (length(buffer) > 0 && !ignore) {
            printf "%s", buffer;
        }
        buffer = $0 ORS;
        ignore = 0;
    } else {
        buffer = buffer $0 ORS;
    }
    if ($0 ~ / - (TRACE|DEBUG) - /) {
        ignore = 1;
    }
}
END {
    if (length(buffer) > 0 && !ignore) {
        printf "%s", buffer;
    }
}' app.log > app-clean.log