Compare commits
9
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dcb09e619e | ||
|
|
087d9e7b30 | ||
|
|
e87186e476 | ||
|
|
7e9aefa755 | ||
|
|
78e363652b | ||
|
|
a57531c95f | ||
|
|
7f053a0fe9 | ||
|
|
5b28bff48b | ||
|
|
c2c3926135 |
No files matched your search
+6
-2
@@ -1,6 +1,6 @@
|
||||
# Copy this file to .env and adjust it.
|
||||
SYSLOG_PORT=514
|
||||
HTTP_PORT=8080
|
||||
SYSLOG_PORT=5514
|
||||
HTTP_PORT=8989
|
||||
TZ=Europe/Paris
|
||||
# How long logs are kept (e.g. 7d, 30d, 12w, 1y)
|
||||
RETENTION=30d
|
||||
@@ -67,3 +67,7 @@ DMESG_BACKFILL=1h
|
||||
AUDIT_BACKFILL=1h
|
||||
# Host name of the audit events (empty: node= of the records, else /host/etc/hostname, else localhost)
|
||||
AUDIT_HOST=
|
||||
# Log files of other programs (Traefik, nginx, applications…), declared in Settings > Log sources >
|
||||
# Log files: mount their directory read-only in docker-compose.yml (e.g. /var/log/traefik:/logs/traefik:ro).
|
||||
# History read when a file is turned on (e.g. 30m, 1h, 24h; 0 = only new lines)
|
||||
FILES_BACKFILL=1h
|
||||
+77
-16
@@ -55,7 +55,7 @@ Ouvrez ensuite <http://localhost:8080>.
|
||||
- **Test manuel** : `logger -n 127.0.0.1 -P 514 -d "hello error"` (util-linux) ou
|
||||
`echo "<14>test ok" | nc -u -w1 127.0.0.1 514`.
|
||||
|
||||
**Paramètres > Sources > Syslog** active ou désactive la réception syslog et choisit les
|
||||
**Paramètres > Sources de logs > Syslog** active ou désactive la réception syslog et choisit les
|
||||
protocoles (UDP, TCP) sans redémarrage ; le choix est enregistré dans `/data/syslog.json`. Cet
|
||||
onglet affiche aussi l'état d'écoute (et l'erreur si le port est déjà utilisé). Le port
|
||||
lui-même est publié par docker-compose : modifiez `SYSLOG_PORT` dans `.env`, puis lancez
|
||||
@@ -98,7 +98,7 @@ affiche tous ses champs.
|
||||
Les en-têtes de colonnes restent visibles pendant le défilement. Faites glisser le bord d'un
|
||||
en-tête (réception, heure message, sévérité, hôte, app) pour redimensionner la colonne, et
|
||||
double-cliquez dessus pour revenir à la largeur automatique ; le message occupe l'espace
|
||||
restant. Les largeurs sont mémorisées par le navigateur (**Paramètres > Interface >
|
||||
restant. Les largeurs sont mémorisées par le navigateur (**Paramètres > Affichage >
|
||||
Réinitialiser les colonnes** les rétablit toutes). Sur téléphone, la liste garde sa
|
||||
présentation sur deux lignes, sans colonnes.
|
||||
|
||||
@@ -126,7 +126,7 @@ lignes).
|
||||
chaque nouvel intervalle. Rien n'est rafraîchi tant que l'onglet du navigateur est masqué ;
|
||||
la frise se met à jour dès qu'il redevient visible.
|
||||
|
||||
**Paramètres > Interface > Frise** (mémorisé par navigateur) : échelle (linéaire, √ par défaut,
|
||||
**Paramètres > Affichage > Frise** (mémorisé par navigateur) : échelle (linéaire, √ par défaut,
|
||||
log), hauteur (S/M/L : 40/80/120 px), couleur (empilement par sévérité, intensité comparée à
|
||||
la médiane de la fenêtre : calme, rafale au-delà de 3×, anomalie au-delà de 10×, ou aucune),
|
||||
barres ou aire, division (automatique, environ 100 intervalles, ou fixe : 1 s, 10 s, 1 min,
|
||||
@@ -155,7 +155,7 @@ colorés et exportés comme les messages syslog :
|
||||
(`"level":"error"`), logfmt (`level=warn`), `[ERROR]`, ou un niveau en majuscules au début de
|
||||
la ligne (`ERROR`, `WARN`…). Sinon, elle vaut `info`. Les codes de couleur du terminal sont
|
||||
supprimés.
|
||||
- **Paramètres > Sources** affiche une étiquette par conteneur (`project/service`) dans un seul
|
||||
- **Paramètres > Sources de logs** affiche une étiquette par conteneur (`project/service`) dans un seul
|
||||
champ : les conteneurs suivis en couleur, puis les non suivis en gris ; un clic bascule une
|
||||
étiquette. La couleur identifie le projet compose, et les noms d'applications Docker de la
|
||||
liste utilisent la même couleur. Les conteneurs arrêtés sont masqués par défaut (« Afficher
|
||||
@@ -181,7 +181,7 @@ moteur (`GET` uniquement).
|
||||
|
||||
Logstream peut aussi collecter les logs système de la machine qui héberge la stack, sans rien
|
||||
configurer sur l'hôte. La source est **désactivée par défaut** : activez-la dans
|
||||
**Paramètres > Sources > Logs système de l'hôte** (enregistré dans `/data/hostlogs.json`).
|
||||
**Paramètres > Sources de logs > Logs système de l'hôte** (enregistré dans `/data/hostlogs.json`).
|
||||
|
||||
- `docker-compose.yml` monte `/var/log` et `/run/log/journal` en lecture seule sous `/host`.
|
||||
- Si l'hôte utilise systemd, Logstream lit directement les fichiers du **journal systemd**
|
||||
@@ -196,7 +196,7 @@ configurer sur l'hôte. La source est **désactivée par défaut** : activez-la
|
||||
- **Droits** : le conteneur tourne avec un utilisateur sans privilège et reçoit le groupe `adm`
|
||||
(gid 4), qui peut lire le journal et `/var/log` sur Debian et Ubuntu. Sur d'autres systèmes,
|
||||
réglez `HOST_LOGS_GID` dans `.env` sur le gid de `systemd-journal`
|
||||
(`getent group systemd-journal | cut -d: -f3`). Paramètres > Sources affiche un message clair
|
||||
(`getent group systemd-journal | cut -d: -f3`). Paramètres > Sources de logs affiche un message clair
|
||||
si l'accès est refusé.
|
||||
- Limites : les champs du journal compressés par journald (messages de plus de 512 octets,
|
||||
compressés en zstd/lz4/xz) ne peuvent pas être décodés sans bibliothèque supplémentaire ; ils
|
||||
@@ -207,7 +207,7 @@ configurer sur l'hôte. La source est **désactivée par défaut** : activez-la
|
||||
|
||||
Logstream peut collecter le buffer circulaire du noyau de l'hôte (ce qu'affiche `dmesg` :
|
||||
matériel, pilotes, OOM killer, pare-feu…) en lisant `/dev/kmsg`. La source est **désactivée par
|
||||
défaut** : activez-la dans **Paramètres > Sources > Messages du noyau (dmesg)** (enregistré dans
|
||||
défaut** : activez-la dans **Paramètres > Sources de logs > Messages du noyau (dmesg)** (enregistré dans
|
||||
`/data/dmesg.json`).
|
||||
|
||||
- **Périphérique** : le conteneur ne voit pas `/dev/kmsg` par défaut. Décommentez la section
|
||||
@@ -223,7 +223,7 @@ défaut** : activez-la dans **Paramètres > Sources > Messages du noyau (dmesg)*
|
||||
- ou lancer Logstream en root avec la capacité `SYSLOG` : décommentez `user: "0:0"` et
|
||||
`cap_add: [SYSLOG]` dans `docker-compose.yml`.
|
||||
|
||||
Paramètres > Sources indique si le périphérique est absent ou l'accès refusé ; le reste de
|
||||
Paramètres > Sources de logs indique si le périphérique est absent ou l'accès refusé ; le reste de
|
||||
Logstream continue de fonctionner et le périphérique est réessayé toutes les 10 secondes.
|
||||
- **Champs** : **app** vaut `kernel` (ou le nom du programme pour les messages écrits par des
|
||||
programmes, comme `systemd[1]: …`), la sévérité et la facility viennent de l'enregistrement,
|
||||
@@ -289,10 +289,69 @@ source est **désactivée par défaut** : activez-la dans **Réglages > Sources
|
||||
enregistrés dans `/data/audit-state.json` : après un redémarrage, la lecture reprend là ;
|
||||
après une rotation par auditd, la fin de `audit.log.1` est lue avant le nouveau fichier.
|
||||
|
||||
## Fichiers de logs (Traefik, nginx, applications…)
|
||||
|
||||
Logstream peut suivre les fichiers de logs d'autres programmes : Traefik, nginx, Apache, ou
|
||||
toute application qui écrit dans un fichier. Déclarez chaque fichier dans **Paramètres > Sources
|
||||
de logs > Fichiers de logs** (enregistré dans `/data/files.json`, 50 au plus) : un **nom** (l'**app**
|
||||
de ses entrées, par défaut le nom du fichier), le **chemin** du fichier tel que monté dans le
|
||||
conteneur, un **format** (*Automatique* par défaut), un **hôte** facultatif (par défaut
|
||||
`/host/etc/hostname`, sinon `localhost`) et un interrupteur.
|
||||
|
||||
- **Montage** du dossier en lecture seule, par ex. dans `docker-compose.yml` :
|
||||
`- /var/log/traefik:/logs/traefik:ro`, puis `docker compose up -d`. Les fichiers de `/var/log`
|
||||
sont déjà accessibles sous `/host/var/log/…`. Les fichiers doivent être lisibles par l'UID
|
||||
`10001` ou le groupe de `HOST_LOGS_GID` (`adm` par défaut) ; Paramètres > Sources de logs
|
||||
indique quand aucun fichier ne correspond ou que l'accès est refusé.
|
||||
- **Chemin** : un fichier, par ex. `/logs/traefik/access.log` (pas de jokers). Dès la saisie, le
|
||||
fichier est vérifié : trouvé ou non, lisible ou non, son format détecté et sa dernière ligne.
|
||||
- **Détection du format** (*Automatique*) : à partir des dernières lignes du fichier (50 au
|
||||
plus), le format structuré que suivent au moins 80 % d'entre elles — accès Traefik JSON, JSON
|
||||
lines, journal d'accès CLF, logfmt — sinon texte brut. Elle est refaite quand le format est
|
||||
changé ; le format trouvé s'affiche dans l'état du fichier. Choisissez un autre format pour
|
||||
le forcer (une expression régulière n'est jamais détectée).
|
||||
- **Formats** :
|
||||
- *Texte brut* : une entrée par ligne ; le niveau est détecté comme pour les logs Docker, un
|
||||
horodatage en début de ligne (ISO 8601, `2006/01/02 15:04:05`, `[02/Jan/2006:15:04:05 +0200]`,
|
||||
`Jan 2 15:04:05`) devient l'heure du message et est retiré du message. Les lignes indentées
|
||||
(stack traces) sont rattachées à l'entrée précédente.
|
||||
- *JSON lines* : `msg` / `message`, `time` / `ts` / `@timestamp` (texte ou heure Unix) et
|
||||
`level` (mot ou nombre, pino/bunyan compris) sont reconnus ; les autres clés deviennent des
|
||||
champs, les objets imbriqués avec des noms pointés (`req.id`).
|
||||
- *logfmt* : paires `clé=valeur`, comme les logs par défaut de Traefik ou des programmes Go ;
|
||||
mêmes clés que le JSON.
|
||||
- *Journal d'accès (CLF)* : Common / Combined Log Format de nginx et Apache, et le journal
|
||||
d'accès de Traefik dans ce format (routeur, URL du serveur et durée compris). Champs
|
||||
`client_ip`, `http_method`, `http_path`, `http_status`, `http_size`, `user_agent`,
|
||||
`http_referer`, `router`, `backend`, `duration_ms`.
|
||||
- *Accès Traefik (JSON)* : le journal d'accès JSON de Traefik (`accessLog.format: json`),
|
||||
résumé en `GET app.example.com/ 200 · 12.5 ms · app@docker · 203.0.113.9`, toutes les clés
|
||||
dans les champs. Les logs de Traefik lui-même dans le même fichier sont lus comme du JSON.
|
||||
- *Expression régulière* (syntaxe RE2 / Go) : les groupes nommés `time`, `level`, `msg`,
|
||||
`host` et `app` remplissent ces champs, `status` donne le niveau d'un journal d'accès, les
|
||||
autres groupes deviennent des champs, par ex.
|
||||
`^(?P<time>\S+ \S+) \[(?P<level>\w+)\] (?P<module>\S+): (?P<msg>.*)$`. Une ligne qui ne
|
||||
correspond pas est rattachée à l'entrée précédente.
|
||||
|
||||
Pour les journaux d'accès, les statuts 5xx sont en `err`, les 4xx en `warning`, les autres en
|
||||
`info`. Une ligne qui ne suit pas le format choisi est gardée comme du texte brut. Les champs
|
||||
qui portent le nom d'un champ de LogStream (`host`, `app`…) sont renommés `field_host`,
|
||||
`field_app`…
|
||||
- **Affichage** : source `file`, le chemin dans `log_file`. Dans la liste, ces lignes ont un
|
||||
liseré gauche vert, une légère teinte et un badge **FILE** avant l'app ; le filtre **Source**
|
||||
les affiche seules.
|
||||
- À l'activation d'un fichier, les lignes de la dernière heure déjà présentes sont lues d'abord
|
||||
(`FILES_BACKFILL`, `0` = seulement les nouvelles lignes ; les lignes sans horodatage sont alors
|
||||
ignorées) ; un fichier créé ensuite est lu depuis son début. L'inode et une
|
||||
position sont enregistrés par fichier dans `/data/files-state.json` : après un redémarrage la
|
||||
lecture reprend là ; après une rotation (`access.log` renommé `access.log.1`), la fin de
|
||||
l'ancien fichier est lue avant le nouveau ; un fichier vidé sur place (`copytruncate`) est
|
||||
relu depuis son début.
|
||||
|
||||
## Logs internes de LogStream
|
||||
|
||||
Logstream enregistre ses propres événements comme des logs, avec l'**hôte** `logstream`,
|
||||
l'**app** du composant (`store`, `syslog`, `docker`, `host`, `dmesg`, `audit`, `main`) et la
|
||||
l'**app** du composant (`store`, `syslog`, `docker`, `host`, `dmesg`, `audit`, `files`, `main`) et la
|
||||
source `logstream` : VictoriaLogs injoignable puis de retour, lots perdus, lots refusés depuis
|
||||
le tampon disque, erreurs du listener syslog, API Docker ou flux d'un conteneur décrochés,
|
||||
sources qui ne peuvent pas lire, démarrage et arrêt. Ils sont actifs par défaut à partir du
|
||||
@@ -330,13 +389,13 @@ En ligne de commande : `curl -o logs.csv 'localhost:8080/api/export.csv?q=error&
|
||||
L'icône en forme d'engrenage ouvre les paramètres, organisés en onglets. Tout, sauf les tags de
|
||||
couleur, est mémorisé par navigateur.
|
||||
|
||||
- **Localisation**
|
||||
- **Localisation et langue**
|
||||
- *Langue* : anglais ou français.
|
||||
- *Date et heure* : fuseau horaire (celui du navigateur, UTC ou environ 80 fuseaux courants)
|
||||
et format d'affichage de l'heure de réception : `DD/MM/YYYY HH:MM:SS` (par défaut,
|
||||
l'affichage français habituel), avec millisecondes, `YYYY-MM-DD`, horloge sur 12 heures,
|
||||
ISO 8601 ou epoch Unix. Le fuseau horaire s'applique à toutes les dates affichées.
|
||||
- **Filtres** : tags de couleur. Chaque tag a un mot-clé, une couleur de fond (le texte passe
|
||||
- **Motifs de recherche** : tags de couleur. Chaque tag a un mot-clé, une couleur de fond (le texte passe
|
||||
automatiquement en noir ou en blanc pour rester lisible) et des options : mot entier, respect
|
||||
de la casse, expression régulière, actif. Les tags sont stockés sur le serveur dans
|
||||
`/data/tags.json` (volume `logstream-data`) : ils sont donc partagés par tous les navigateurs.
|
||||
@@ -355,10 +414,10 @@ couleur, est mémorisé par navigateur.
|
||||
les préréglages s'en servent pour colorer le code de statut ou la méthode, pas le texte autour.
|
||||
Chaque tag reçoit un code à deux chiffres (`01`, `02`…) attribué par le serveur : il reste
|
||||
attaché au tag jusqu'à sa suppression (les tags créés par les versions précédentes en reçoivent
|
||||
un aussi). La colonne *Filtres* de la liste affiche, en badges gris, les codes des tags actifs
|
||||
un aussi). La colonne *Motif de recherche* de la liste affiche, en badges gris, les codes des tags actifs
|
||||
trouvés dans chaque message ; elle a la place pour 3, au-delà elle en affiche 2 et `+N`, et
|
||||
l'infobulle les liste tous.
|
||||
- **Interface**
|
||||
- **Affichage**
|
||||
- *Thème* : Système (suit la préférence de l'ordinateur ou du téléphone), Clair ou Sombre. Le
|
||||
bouton soleil/lune de l'en-tête bascule entre clair et sombre.
|
||||
- *Affichage des logs* : taille du texte (très petite, petite, moyenne, grande), densité
|
||||
@@ -372,7 +431,7 @@ couleur, est mémorisé par navigateur.
|
||||
européen de polices respectueux de la vie privée ; sans accès à internet, la police du
|
||||
système est utilisée. Les ligatures sont désactivées pour que `->` ou `!=` s'affichent tels
|
||||
quels. Le réglage le plus dense est Très petite + Compacte + Inconsolata Condensed.
|
||||
- **Données** : chiffres de la base lus dans VictoriaLogs (actualisés au plus toutes les 30 s) :
|
||||
- **Base de données** : chiffres de la base lus dans VictoriaLogs (actualisés au plus toutes les 30 s) :
|
||||
lignes stockées, taille sur disque (index compris), taille brute et taux de compression,
|
||||
période couverte avec la rétention, lignes des dernières 24 h et de la dernière heure, hôtes
|
||||
et applications distincts, espace disque libre. Les tailles sont en Ko/Mo/Go (KB/MB/GB en
|
||||
@@ -503,6 +562,7 @@ résolutions.
|
||||
| `AUDIT_LOG` | `/var/log/audit/audit.log` | chemin par défaut du journal d'audit Linux (modifiable dans Réglages > Sources) |
|
||||
| `AUDIT_BACKFILL` | `1h` | historique lu à l'activation de la source « audit Linux » |
|
||||
| `AUDIT_HOST` | vide | nom d'hôte des événements d'audit quand les enregistrements n'ont pas de `node=` |
|
||||
| `FILES_BACKFILL` | `1h` | historique lu à l'activation d'un fichier de logs (Paramètres > Sources de logs > Fichiers de logs) |
|
||||
| `TZ` | `Europe/Paris` | fuseau horaire des horodatages RFC 3164 (qui n'en portent pas) |
|
||||
| `BATCH_SIZE`, `FLUSH_MS`, `QUEUE_SIZE` | `1000`, `1000`, `100000` | réglage de l'ingestion |
|
||||
| `SPOOL_MAX_MB` | `1024` | taille du tampon disque des lots refusés par VictoriaLogs (`0` = pas de tampon : 15 s de tentatives, puis perte) |
|
||||
@@ -543,7 +603,7 @@ Pour mettre à jour l'une d'elles :
|
||||
VictoriaLogs conserve son format de stockage entre versions mineures ; lisez le changelog
|
||||
avant un changement de version majeure.
|
||||
2. Modifiez la version dans le fichier indiqué ci-dessus, puis lancez `docker compose up -d --build`.
|
||||
3. Vérifiez la barre du bas (reçus / stockés / perdus) et **Paramètres > Sources**. Pour revenir
|
||||
3. Vérifiez la barre du bas (reçus / stockés / perdus) et **Paramètres > Sources de logs**. Pour revenir
|
||||
en arrière, remettez la version précédente et lancez la même commande.
|
||||
|
||||
## Organisation du code
|
||||
@@ -561,10 +621,11 @@ Pour mettre à jour l'une d'elles :
|
||||
| `rdns.go` | résolutions DNS inverses avec cache |
|
||||
| `export.go` | export CSV en flux |
|
||||
| `docker.go` | logs des conteneurs Docker (API, lecteurs, positions, détection du niveau) |
|
||||
| `syslogserver.go` | écoutes syslog ouvertes et fermées depuis Paramètres > Sources |
|
||||
| `syslogserver.go` | écoutes syslog ouvertes et fermées depuis Paramètres > Sources de logs |
|
||||
| `hostlogs.go`, `journal.go` | logs système de l'hôte : lecteur du journal systemd (sans `journalctl`) et suivi de `/var/log` |
|
||||
| `dmesg.go` | messages du noyau : lecture de `/dev/kmsg`, analyse des enregistrements, position par identifiant de démarrage et numéro de séquence |
|
||||
| `audit.go` | audit Linux : suivi de `audit.log` (rotation, position enregistrée), analyse des enregistrements, regroupement par événement, résumé |
|
||||
| `filesource.go`, `fileformats.go` | fichiers de logs : suivi (rotation, positions enregistrées, lignes de suite) et formats (détection, texte, JSON, logfmt, CLF, Traefik, expression régulière) |
|
||||
| `selflog.go` | logs internes de LogStream : `logEvent`, niveau minimal, limitation des répétitions, file bornée |
|
||||
| `tags.go` | stockage des tags de couleur |
|
||||
| `presets.go`, `presets.json` | préréglages de tags (`/api/presets`), liste intégrée |
|
||||
|
||||
@@ -50,7 +50,7 @@ Then open <http://localhost:8080>.
|
||||
- **Manual test**: `logger -n 127.0.0.1 -P 514 -d "hello error"` (util-linux) or
|
||||
`echo "<14>test ok" | nc -u -w1 127.0.0.1 514`.
|
||||
|
||||
**Settings > Sources > Syslog** turns syslog reception on or off and chooses the protocols
|
||||
**Settings > Log sources > Syslog** turns syslog reception on or off and chooses the protocols
|
||||
(UDP, TCP) without restarting; the choice is saved in `/data/syslog.json`. It also shows the
|
||||
listening state (and the error if the port is already in use). The port itself is published
|
||||
by docker-compose: change `SYSLOG_PORT` in `.env`, then run `docker compose up -d`.
|
||||
@@ -89,7 +89,7 @@ Shortcuts: `/` focuses the search box, `Esc` clears it. Clicking a row shows all
|
||||
The column headers stay visible while scrolling. Drag the edge of a header (received, message
|
||||
time, severity, host, app) to resize the column, double-click it to go back to the automatic
|
||||
width; the message takes the remaining space. Widths are remembered by the browser
|
||||
(**Settings > Interface > Reset column widths** restores them all). On phones the list keeps its
|
||||
(**Settings > Appearance > Reset column widths** restores them all). On phones the list keeps its
|
||||
two-line layout without columns.
|
||||
|
||||
## Stream and Time range modes
|
||||
@@ -114,7 +114,7 @@ time** on the server clock (so it matches the times shown in the rows).
|
||||
- In live mode the last interval grows as messages arrive, and the timeline reloads at each new
|
||||
interval. Nothing is refreshed while the browser tab is hidden; it catches up when shown again.
|
||||
|
||||
**Settings > Interface > Timeline** (remembered per browser): scale (linear, √ by default, log),
|
||||
**Settings > Appearance > Timeline** (remembered per browser): scale (linear, √ by default, log),
|
||||
height (S/M/L: 40/80/120 px), color (stacked by severity, intensity compared with the median of
|
||||
the window: calm, burst above 3×, anomaly above 10×, or none), bars or area, division
|
||||
(automatic, about 100 intervals, or fixed: 1 s, 10 s, 1 min, 5 min, 1 h, 1 day) and refresh
|
||||
@@ -141,7 +141,7 @@ colored and exported like syslog messages:
|
||||
- The severity comes from the line itself when the application writes it: JSON
|
||||
(`"level":"error"`), logfmt (`level=warn`), `[ERROR]`, or an upper-case level word at the
|
||||
start of the line (`ERROR`, `WARN`…). Otherwise it is `info`. Terminal color codes are removed.
|
||||
- **Settings > Sources** shows one label per container (`project/service`) in a single field:
|
||||
- **Settings > Log sources** shows one label per container (`project/service`) in a single field:
|
||||
followed containers in color, then not followed ones in grey; a click switches a label.
|
||||
The color identifies the compose project, and Docker app names in the log list use the
|
||||
same color. Stopped containers are hidden by default ("Show stopped containers" displays
|
||||
@@ -165,7 +165,7 @@ which only lets through listing containers, reading logs, events and engine info
|
||||
|
||||
Logstream can also collect the system logs of the machine hosting the stack, without
|
||||
configuring anything on the host. The source is **off by default**: turn it on in
|
||||
**Settings > Sources > Host system logs** (saved in `/data/hostlogs.json`).
|
||||
**Settings > Log sources > Host system logs** (saved in `/data/hostlogs.json`).
|
||||
|
||||
- `docker-compose.yml` mounts `/var/log` and `/run/log/journal` read-only under `/host`.
|
||||
- When the host runs systemd, Logstream reads the **systemd journal** files directly (no
|
||||
@@ -180,7 +180,7 @@ configuring anything on the host. The source is **off by default**: turn it on i
|
||||
- **Permissions**: the container runs as an unprivileged user and gets the `adm` group
|
||||
(gid 4), which can read the journal and `/var/log` on Debian and Ubuntu. On other systems,
|
||||
set `HOST_LOGS_GID` in `.env` to the gid of `systemd-journal`
|
||||
(`getent group systemd-journal | cut -d: -f3`). Settings > Sources shows a clear message when
|
||||
(`getent group systemd-journal | cut -d: -f3`). Settings > Log sources shows a clear message when
|
||||
access is denied.
|
||||
- Limits: journal fields compressed by journald (messages longer than 512 bytes, compressed
|
||||
with zstd/lz4/xz) cannot be decoded without extra libraries; they are counted in Settings and
|
||||
@@ -191,7 +191,7 @@ configuring anything on the host. The source is **off by default**: turn it on i
|
||||
|
||||
Logstream can collect the kernel ring buffer of the host (what `dmesg` shows: hardware,
|
||||
drivers, OOM killer, firewall…) by reading `/dev/kmsg`. The source is **off by default**: turn it
|
||||
on in **Settings > Sources > Kernel messages (dmesg)** (saved in `/data/dmesg.json`).
|
||||
on in **Settings > Log sources > Kernel messages (dmesg)** (saved in `/data/dmesg.json`).
|
||||
|
||||
- **Device**: the container does not see `/dev/kmsg` by default. Uncomment the `devices`
|
||||
section of `docker-compose.yml` (`/dev/kmsg:/dev/kmsg:r`, read-only), then
|
||||
@@ -205,7 +205,7 @@ on in **Settings > Sources > Kernel messages (dmesg)** (saved in `/data/dmesg.js
|
||||
- or run Logstream as root with the `SYSLOG` capability: uncomment `user: "0:0"` and
|
||||
`cap_add: [SYSLOG]` in `docker-compose.yml`.
|
||||
|
||||
Settings > Sources says when the device is missing or access is denied; the rest of
|
||||
Settings > Log sources says when the device is missing or access is denied; the rest of
|
||||
Logstream keeps working and the device is tried again every 10 seconds.
|
||||
- **Fields**: **app** is `kernel` (or the program name for messages written by programs such
|
||||
as `systemd[1]: …`), severity and facility come from the record, **host** is `localhost`,
|
||||
@@ -224,7 +224,7 @@ on in **Settings > Sources > Kernel messages (dmesg)** (saved in `/data/dmesg.js
|
||||
|
||||
Logstream can follow the log of the Linux audit daemon (`/var/log/audit/audit.log`): logins,
|
||||
`sudo`, watched files, syscalls matched by your `auditctl` rules… The source is **off by
|
||||
default**: turn it on in **Settings > Sources > Linux audit (auditd)**, where the file path can
|
||||
default**: turn it on in **Settings > Log sources > Linux audit (auditd)**, where the file path can
|
||||
also be changed (saved in `/data/audit.json`; `AUDIT_LOG` sets the default path).
|
||||
|
||||
- **Mount**: uncomment `/var/log/audit:/var/log/audit:ro` in `docker-compose.yml`, then
|
||||
@@ -240,7 +240,7 @@ also be changed (saved in `/data/audit.json`; `AUDIT_LOG` sets the default path)
|
||||
```
|
||||
auditd then creates every rotated file with mode `0640` and group `adm`. An ACL also works
|
||||
(`sudo setfacl -m u:10001:rx /var/log/audit && sudo setfacl -m u:10001:r /var/log/audit/audit.log`)
|
||||
but must be applied again to each new file after a rotation. Settings > Sources says when the
|
||||
but must be applied again to each new file after a rotation. Settings > Log sources says when the
|
||||
file is missing or access is denied; it is tried again every second.
|
||||
- **Events**: the records of one event (`SYSCALL`, `EXECVE`, `CWD`, `PATH`, `PROCTITLE`…) share
|
||||
the same `msg=audit(time:serial)` and become **one** log, once no record came for 2 seconds.
|
||||
@@ -265,13 +265,66 @@ also be changed (saved in `/data/audit.json`; `AUDIT_LOG` sets the default path)
|
||||
`/data/audit-state.json`: after a restart, reading resumes there; after a rotation by auditd,
|
||||
the end of `audit.log.1` is read before the new file.
|
||||
|
||||
## Log files (Traefik, nginx, applications…)
|
||||
|
||||
Logstream can follow the log files of other programs: Traefik, nginx, Apache, or any application
|
||||
that writes to a file. Declare each file in **Settings > Log sources > Log files** (saved in
|
||||
`/data/files.json`, up to 50): a **name** (the **app** of its entries, by default the file name),
|
||||
the **path** of the file as mounted in the container, a **format** (*Automatic* by default), an
|
||||
optional **host** (default: `/host/etc/hostname`, else `localhost`) and a switch.
|
||||
|
||||
- **Mount** the directory read-only, e.g. in `docker-compose.yml`:
|
||||
`- /var/log/traefik:/logs/traefik:ro`, then `docker compose up -d`. Files under `/var/log`
|
||||
are already reachable as `/host/var/log/…`. The files must be readable by UID `10001` or the
|
||||
group of `HOST_LOGS_GID` (`adm` by default); Settings > Log sources says when no file matches
|
||||
or access is denied.
|
||||
- **Path**: one file, e.g. `/logs/traefik/access.log` (no wildcards). As soon as it is typed, the
|
||||
file is checked: found or not, readable or not, its detected format and its last line.
|
||||
- **Format detection** (*Automatic*): from the last lines of the file (50 at most), the
|
||||
structured format that at least 80 % of them follow — Traefik access JSON, JSON lines, access
|
||||
log CLF, logfmt — else plain text. It is done again when the format is changed; the format
|
||||
found is shown in the status of the file. Choose another format to force it (a regular
|
||||
expression is never detected).
|
||||
- **Formats**:
|
||||
- *Plain text*: one entry per line; the level is detected as for Docker logs, a timestamp at
|
||||
the start of the line (ISO 8601, `2006/01/02 15:04:05`, `[02/Jan/2006:15:04:05 +0200]`,
|
||||
`Jan 2 15:04:05`) becomes the message time and is removed from the message. Indented
|
||||
lines (stack traces) are added to the previous entry.
|
||||
- *JSON lines*: `msg` / `message`, `time` / `ts` / `@timestamp` (text or Unix time) and
|
||||
`level` (word or number, pino/bunyan included) are recognized; the other keys become
|
||||
fields, nested objects with dotted names (`req.id`).
|
||||
- *logfmt*: `key=value` pairs, as in Traefik's default logs or Go programs; same keys as JSON.
|
||||
- *Access log (CLF)*: Common / Combined Log Format of nginx and Apache, and Traefik's access
|
||||
log in this format (router, server URL and duration included). Fields `client_ip`,
|
||||
`http_method`, `http_path`, `http_status`, `http_size`, `user_agent`, `http_referer`,
|
||||
`router`, `backend`, `duration_ms`.
|
||||
- *Traefik access (JSON)*: the JSON access log of Traefik (`accessLog.format: json`),
|
||||
summarized as `GET app.example.com/ 200 · 12.5 ms · app@docker · 203.0.113.9`, every key in
|
||||
the fields. Traefik's own logs in the same file are read as JSON lines.
|
||||
- *Regular expression* (RE2 / Go syntax): the named groups `time`, `level`, `msg`, `host`
|
||||
and `app` fill these fields, `status` gives the level of an access log, the other groups
|
||||
become fields, e.g. `^(?P<time>\S+ \S+) \[(?P<level>\w+)\] (?P<module>\S+): (?P<msg>.*)$`.
|
||||
A line that does not match is added to the previous entry.
|
||||
|
||||
For access logs, 5xx statuses are `err`, 4xx `warning`, the others `info`. A line that does
|
||||
not fit the chosen format is kept as plain text. Fields named like LogStream's own ones
|
||||
(`host`, `app`…) are renamed `field_host`, `field_app`…
|
||||
- **Display**: source `file`, the path in `log_file`. In the list these lines have a green left
|
||||
edge, a light tint and a **FILE** badge before the app; the **Source** filter shows them alone.
|
||||
- When a file is turned on, the lines of the last hour already in it are read first
|
||||
(`FILES_BACKFILL`, `0` = only new lines; lines without a timestamp are then left out); a file
|
||||
created later is read from its start. The inode and an offset are saved per file in
|
||||
`/data/files-state.json`: after a restart reading resumes there; after a rotation
|
||||
(`access.log` renamed `access.log.1`), the end of the old file is read before the new one;
|
||||
a file truncated in place (`copytruncate`) is read again from its start.
|
||||
|
||||
## LogStream internal logs
|
||||
|
||||
Logstream records its own events as logs, with **host** `logstream`, **app** the component
|
||||
(`store`, `syslog`, `docker`, `host`, `dmesg`, `audit`, `main`) and the `logstream` source:
|
||||
(`store`, `syslog`, `docker`, `host`, `dmesg`, `audit`, `files`, `main`) and the `logstream` source:
|
||||
VictoriaLogs unreachable and back, lost batches, batches refused from the disk buffer, syslog
|
||||
listener errors, Docker API or container streams lost, sources that cannot read, start and
|
||||
stop. They are on by default from level **warning**; **Settings > Sources > LogStream internal
|
||||
stop. They are on by default from level **warning**; **Settings > Log sources > LogStream internal
|
||||
logs** turns them off or changes the minimum level (saved in `/data/selflog.json`). The same
|
||||
event (component and message) is recorded at most once every 30 seconds, with the number of
|
||||
repeats left out; events go through a bounded queue and never wait for the storage, so an
|
||||
@@ -307,7 +360,7 @@ remembered per browser.
|
||||
format of the reception time: `DD/MM/YYYY HH:MM:SS` (default, the usual French display),
|
||||
with milliseconds, `YYYY-MM-DD`, 12-hour clock, ISO 8601 or Unix epoch. The time zone
|
||||
applies to every date shown.
|
||||
- **Filters**: color tags. Each tag has a keyword, a background color (the text automatically
|
||||
- **Patterns**: color tags. Each tag has a keyword, a background color (the text automatically
|
||||
switches to black or white to stay readable) and options: whole word, match case,
|
||||
regular expression, active. Tags are stored on the server in `/data/tags.json`
|
||||
(`logstream-data` volume), so they are shared by every browser. Default tags (pastel):
|
||||
@@ -324,10 +377,10 @@ remembered per browser.
|
||||
or the method, not the text around it.
|
||||
Each tag gets a two-digit code (`01`, `02`…) assigned by the server: it stays with the tag
|
||||
until the tag is deleted (codes are also given to tags created by earlier versions). The
|
||||
*Filters* column of the log list shows, as grey badges, the codes of the active tags found in
|
||||
*Pattern* column of the log list shows, as grey badges, the codes of the active tags found in
|
||||
each message; it has room for 3, beyond that it shows 2 and `+N`, and the tooltip lists them
|
||||
all.
|
||||
- **Interface**
|
||||
- **Appearance**
|
||||
- *Theme*: System (follows the computer/phone preference), Light or Dark. The sun/moon
|
||||
button in the header switches between light and dark.
|
||||
- *Log display*: font size (tiny, small, medium, large), density (normal, or compact to
|
||||
@@ -339,7 +392,7 @@ remembered per browser.
|
||||
[Bunny Fonts](https://fonts.bunny.net), a privacy-friendly European font service; without
|
||||
internet access, the system font is used. Ligatures are disabled so `->` or `!=` show as
|
||||
typed. The densest setting is Tiny + Compact + Inconsolata Condensed.
|
||||
- **Data**: database figures read from VictoriaLogs (refreshed at most every 30 s): stored
|
||||
- **Database**: database figures read from VictoriaLogs (refreshed at most every 30 s): stored
|
||||
lines, size on disk (index included), raw size and compression ratio, period covered with
|
||||
the retention, lines of the last 24 h and last hour, distinct hosts and apps, free disk
|
||||
space. Sizes use KB/MB/GB (Ko/Mo/Go in French). "Delete all logs" permanently erases every stored log (you must type
|
||||
@@ -457,9 +510,10 @@ are only known by your router or a local DNS (Pi-hole, AdGuard, Unbound…), set
|
||||
| `HOST_LOGS_ROOT` | `/host` | where the host directories are mounted |
|
||||
| `DMESG_BACKFILL` | `1h` | history read when the kernel messages source is turned on |
|
||||
| `DMESG_DEVICE` | `/dev/kmsg` | kernel log device read by the kernel messages source |
|
||||
| `AUDIT_LOG` | `/var/log/audit/audit.log` | default path of the Linux audit log (can be changed in Settings > Sources) |
|
||||
| `AUDIT_LOG` | `/var/log/audit/audit.log` | default path of the Linux audit log (can be changed in Settings > Log sources) |
|
||||
| `AUDIT_BACKFILL` | `1h` | history read when the Linux audit source is turned on |
|
||||
| `AUDIT_HOST` | empty | host name of the audit events when the records carry no `node=` |
|
||||
| `FILES_BACKFILL` | `1h` | history read when a log file is turned on (Settings > Log sources > Log files) |
|
||||
| `TZ` | `Europe/Paris` | time zone for RFC 3164 timestamps (which carry none) |
|
||||
| `BATCH_SIZE`, `FLUSH_MS`, `QUEUE_SIZE` | `1000`, `1000`, `100000` | ingestion tuning |
|
||||
| `SPOOL_MAX_MB` | `1024` | disk buffer size for batches VictoriaLogs could not take (`0` = no buffer: retried for 15 s, then dropped) |
|
||||
@@ -498,7 +552,7 @@ To update one of them:
|
||||
VictoriaLogs keeps its storage format across minor versions; read the changelog before a
|
||||
major version change.
|
||||
2. Change the version in the file above, then run `docker compose up -d --build`.
|
||||
3. Check the bottom bar (received / stored / dropped) and **Settings > Sources**. To go back,
|
||||
3. Check the bottom bar (received / stored / dropped) and **Settings > Log sources**. To go back,
|
||||
restore the previous version and run the same command.
|
||||
|
||||
## Code layout
|
||||
@@ -516,10 +570,11 @@ To update one of them:
|
||||
| `rdns.go` | cached reverse DNS lookups |
|
||||
| `export.go` | streamed CSV export |
|
||||
| `docker.go` | Docker container logs (API, followers, positions, level detection) |
|
||||
| `syslogserver.go` | syslog listeners opened and closed from Settings > Sources |
|
||||
| `syslogserver.go` | syslog listeners opened and closed from Settings > Log sources |
|
||||
| `hostlogs.go`, `journal.go` | host system logs: systemd journal reader (no `journalctl`) and `/var/log` follower |
|
||||
| `dmesg.go` | kernel messages: `/dev/kmsg` reader, record parsing, position by boot ID and sequence number |
|
||||
| `audit.go` | Linux audit: `audit.log` follower (rotation, saved offset), record parsing, grouping by event, summary |
|
||||
| `filesource.go`, `fileformats.go` | log files: follower (rotation, saved offsets, continuation lines) and formats (detection, text, JSON, logfmt, CLF, Traefik, regular expression) |
|
||||
| `selflog.go` | LogStream internal logs: `logEvent`, minimum level, throttling, bounded queue |
|
||||
| `tags.go` | color tag storage |
|
||||
| `presets.go`, `presets.json` | color tag presets (`/api/presets`), built-in list |
|
||||
|
||||
@@ -25,6 +25,7 @@ type API struct {
|
||||
host *HostLogs
|
||||
dmesg *Dmesg
|
||||
audit *Audit
|
||||
files *Files
|
||||
selfLog *SelfLog
|
||||
dbstats dbStatsCache
|
||||
}
|
||||
@@ -56,6 +57,9 @@ func (a *API) Routes(mux *http.ServeMux) {
|
||||
mux.HandleFunc("PUT /api/dmesg", a.dmesgConfigure)
|
||||
mux.HandleFunc("GET /api/audit", a.auditStatus)
|
||||
mux.HandleFunc("PUT /api/audit", a.auditConfigure)
|
||||
mux.HandleFunc("GET /api/files", a.filesStatus)
|
||||
mux.HandleFunc("PUT /api/files", a.filesConfigure)
|
||||
mux.HandleFunc("POST /api/files/detect", a.filesDetect)
|
||||
mux.HandleFunc("GET /api/selflog", a.selfLogStatus)
|
||||
mux.HandleFunc("PUT /api/selflog", a.selfLogConfigure)
|
||||
}
|
||||
@@ -80,6 +84,41 @@ func (a *API) auditConfigure(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, a.audit.Status())
|
||||
}
|
||||
|
||||
// GET /api/files: the log files of the "Files" source (Settings > Sources).
|
||||
func (a *API) filesStatus(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, a.files.Status())
|
||||
}
|
||||
|
||||
// PUT /api/files {"sources": [{"id", "name", "path", "format" (auto or a format), "regex", "host", "enabled"}]}
|
||||
// (no id: a new file).
|
||||
func (a *API) filesConfigure(w http.ResponseWriter, r *http.Request) {
|
||||
var cfg filesConfig
|
||||
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 64<<10)).Decode(&cfg); err != nil {
|
||||
writeErr(w, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
if err := a.files.Configure(cfg); err != nil {
|
||||
writeErr(w, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
log.Printf("file sources changed from %s: %d files", r.RemoteAddr, len(cfg.Sources))
|
||||
writeJSON(w, http.StatusOK, a.files.Status())
|
||||
}
|
||||
|
||||
// POST /api/files/detect {"path": "/logs/traefik/access.log"}: whether the file
|
||||
// can be read, its detected format and its last line (Settings > Sources). A
|
||||
// POST, so that read-only accounts cannot read files through it.
|
||||
func (a *API) filesDetect(w http.ResponseWriter, r *http.Request) {
|
||||
var req struct {
|
||||
Path string `json:"path"`
|
||||
}
|
||||
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 4096)).Decode(&req); err != nil {
|
||||
writeErr(w, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, DetectFile(req.Path))
|
||||
}
|
||||
|
||||
// GET /api/selflog: state of the LogStream internal logs (Settings > Sources).
|
||||
func (a *API) selfLogStatus(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, a.selfLog.Status())
|
||||
|
||||
+6
-3
@@ -7,9 +7,9 @@ services:
|
||||
- victorialogs
|
||||
- docker-proxy
|
||||
ports:
|
||||
- "${SYSLOG_PORT:-514}:5514/udp"
|
||||
- "${SYSLOG_PORT:-514}:5514/tcp"
|
||||
- "${HTTP_PORT:-8080}:8080"
|
||||
- "${SYSLOG_PORT:-5514}:5514/udp"
|
||||
- "${SYSLOG_PORT:-5514}:5514/tcp"
|
||||
- "${HTTP_PORT:-8989}:8080"
|
||||
environment:
|
||||
VLOGS_URL: http://victorialogs:9428
|
||||
SYSLOG_PUBLIC_PORT: ${SYSLOG_PORT:-514} # le port d'ecoute syslog par defaut (attention aux ports <1024)
|
||||
@@ -39,6 +39,7 @@ services:
|
||||
HOST_LOGS_BACKFILL: ${HOST_LOGS_BACKFILL:-1h} # historique lu a l'activation des logs systeme de l'hote
|
||||
DMESG_BACKFILL: ${DMESG_BACKFILL:-1h} # historique lu a l'activation des messages du noyau (dmesg)
|
||||
AUDIT_BACKFILL: ${AUDIT_BACKFILL:-1h} # historique lu a l'activation de l'audit Linux (auditd)
|
||||
FILES_BACKFILL: ${FILES_BACKFILL:-1h} # historique lu a l'activation d'un fichier de logs (Reglages > Sources)
|
||||
AUDIT_HOST: ${AUDIT_HOST:-} # nom d'hote des evenements d'audit (vide : node= des enregistrements, sinon /host/etc/hostname)
|
||||
group_add:
|
||||
- "${HOST_LOGS_GID:-4}" # groupe autorise a lire les logs de l'hote (4 = adm sur Debian/Ubuntu)
|
||||
@@ -63,6 +64,8 @@ services:
|
||||
# noms des utilisateurs et de l'hote pour les evenements d'audit (optionnel)
|
||||
# - /etc/passwd:/host/etc/passwd:ro
|
||||
# - /etc/hostname:/host/etc/hostname:ro
|
||||
# fichiers de logs d'autres programmes (Traefik, nginx...), a declarer dans Reglages > Sources > Fichiers de logs
|
||||
# - /var/log/traefik:/logs/traefik:ro
|
||||
# logo de la page de connexion, avec LOGIN_LOGO=/config/logo.png dans .env
|
||||
# - ./logo.png:/config/logo.png:ro
|
||||
# prereglages de tags personnalises, avec PRESETS_FILE=/config/presets.json dans .env
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
|
||||
# Préréglages de tags de couleur
|
||||
|
||||
Dans **Paramètres › Filtres**, le menu **+ Préréglage…** ajoute d'un clic un groupe de tags de
|
||||
Dans **Paramètres › Motifs de recherche**, le menu **+ Préréglage…** ajoute d'un clic un groupe de tags de
|
||||
couleur tout faits. Les tags ajoutés sont des tags ordinaires : vous pouvez changer leur couleur,
|
||||
leur motif ou leurs options, ou les supprimer. Un tag dont le motif est déjà dans la liste n'est
|
||||
pas ajouté en double.
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
|
||||
# Color tag presets
|
||||
|
||||
In **Settings › Filters**, the **+ Preset…** menu adds a group of ready-made color tags in one
|
||||
In **Settings › Patterns**, the **+ Preset…** menu adds a group of ready-made color tags in one
|
||||
click. Added tags are ordinary tags: you can change their color, pattern or options, or delete
|
||||
them. A tag whose pattern is already in the list is not added twice.
|
||||
|
||||
|
||||
+689
@@ -0,0 +1,689 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// Formats of the "Files" source (Settings > Sources), one per declared file.
|
||||
const (
|
||||
fmtAuto = "auto" // detected from the last lines of the file
|
||||
fmtText = "text" // one line per entry, level and leading timestamp detected
|
||||
fmtJSON = "json" // JSON lines: msg/time/level recognized, other keys as fields
|
||||
fmtLogfmt = "logfmt" // key=value pairs (Traefik "common" logs, Go programs…)
|
||||
fmtRegex = "regex" // regular expression with named groups
|
||||
fmtCLF = "clf" // access logs in Common/Combined Log Format (nginx, Apache, Traefik)
|
||||
fmtTraefik = "traefik" // Traefik access log in JSON
|
||||
)
|
||||
|
||||
var fileFormats = map[string]bool{fmtText: true, fmtJSON: true, fmtLogfmt: true, fmtRegex: true, fmtCLF: true, fmtTraefik: true}
|
||||
|
||||
// fileRec is one parsed line (or several, for continuation lines).
|
||||
type fileRec struct {
|
||||
t time.Time
|
||||
dated bool // t comes from the line
|
||||
sev int
|
||||
msg string
|
||||
host string
|
||||
app string
|
||||
fields map[string]string
|
||||
}
|
||||
|
||||
const (
|
||||
maxFileFields = 64
|
||||
maxFieldValue = 4096
|
||||
maxFileMsg = 64 << 10 // message with its continuation lines
|
||||
maxContinuation = 500 // continuation lines added to one entry
|
||||
)
|
||||
|
||||
// fileParser turns a line of a file into an entry, for one format.
|
||||
type fileParser struct {
|
||||
format string
|
||||
re *regexp.Regexp // fmtRegex
|
||||
}
|
||||
|
||||
func newFileParser(format, expr string) (*fileParser, error) {
|
||||
if !fileFormats[format] {
|
||||
return nil, fmt.Errorf("unknown format %q", format)
|
||||
}
|
||||
p := &fileParser{format: format}
|
||||
if format == fmtRegex {
|
||||
if strings.TrimSpace(expr) == "" {
|
||||
return nil, fmt.Errorf("the regex format needs a regular expression")
|
||||
}
|
||||
re, err := regexp.Compile(expr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid regular expression: %v", err)
|
||||
}
|
||||
p.re = re
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// multiline tells whether lines that do not start an entry are added to the
|
||||
// previous one (stack traces…). Structured formats are one entry per line.
|
||||
func (p *fileParser) multiline() bool {
|
||||
return p.format == fmtText || p.format == fmtRegex
|
||||
}
|
||||
|
||||
// continuation reports a line that belongs to the previous entry: indented,
|
||||
// or not matching the regular expression.
|
||||
func (p *fileParser) continuation(line string) bool {
|
||||
if line != "" && (line[0] == ' ' || line[0] == '\t') {
|
||||
return true
|
||||
}
|
||||
return p.format == fmtRegex && !p.re.MatchString(line)
|
||||
}
|
||||
|
||||
// parse decodes one line; it never fails (a line in another format is kept as text).
|
||||
func (p *fileParser) parse(line string, now time.Time) *fileRec {
|
||||
line = cleanLine(line)
|
||||
switch p.format {
|
||||
case fmtJSON, fmtTraefik:
|
||||
if obj, ok := decodeJSONLine(line); ok {
|
||||
if p.format == fmtTraefik {
|
||||
if r := traefikRec(obj, line, now); r != nil {
|
||||
return r
|
||||
}
|
||||
}
|
||||
return jsonRec(obj, line, now)
|
||||
}
|
||||
case fmtLogfmt:
|
||||
if kv := parseLogfmt(line); len(kv) > 0 {
|
||||
return logfmtRec(kv, line, now)
|
||||
}
|
||||
case fmtRegex:
|
||||
if m := p.re.FindStringSubmatch(line); m != nil {
|
||||
return p.regexRec(m, line, now)
|
||||
}
|
||||
case fmtCLF:
|
||||
if r := clfRec(line, now); r != nil {
|
||||
return r
|
||||
}
|
||||
}
|
||||
return textRec(line, now)
|
||||
}
|
||||
|
||||
func cleanLine(s string) string {
|
||||
s = strings.TrimRight(s, "\r")
|
||||
s = ansiRe.ReplaceAllString(s, "")
|
||||
if !utf8.ValidString(s) {
|
||||
s = strings.ToValidUTF8(s, "�")
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// textRec: the line itself, without its leading timestamp when there is one.
|
||||
func textRec(line string, now time.Time) *fileRec {
|
||||
r := &fileRec{t: now, sev: 6, msg: line}
|
||||
if m := leadTimeRe.FindStringSubmatchIndex(line); m != nil {
|
||||
if t, ok := parseLogTime(line[m[2]:m[3]], now); ok {
|
||||
r.t, r.dated = t, true
|
||||
if rest := line[m[1]:]; strings.TrimSpace(rest) != "" {
|
||||
r.msg = rest
|
||||
}
|
||||
}
|
||||
}
|
||||
if n, ok := detectLevel(r.msg); ok {
|
||||
r.sev = n
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
/* ---------- Timestamps ---------- */
|
||||
|
||||
// leadTimeRe: a timestamp at the start of a line, possibly in brackets
|
||||
// (ISO 8601 / RFC 3339, 2006/01/02 15:04:05, CLF, syslog "Jan _2 15:04:05").
|
||||
var leadTimeRe = regexp.MustCompile(`^\[?(\d{4}[-/]\d{2}[-/]\d{2}[T ]\d{2}:\d{2}:\d{2}(?:[.,]\d+)?(?: ?(?:Z|[+-]\d{2}:?\d{2}))?|\d{2}/[A-Z][a-z]{2}/\d{4}:\d{2}:\d{2}:\d{2} [+-]\d{4}|[A-Z][a-z]{2} +\d{1,2} \d{2}:\d{2}:\d{2})\]?[ \t]*(?:[-|:][ \t]+)?`)
|
||||
|
||||
var timeLayouts = []string{
|
||||
time.RFC3339Nano,
|
||||
"2006-01-02T15:04:05Z0700",
|
||||
"2006-01-02T15:04:05",
|
||||
"02/Jan/2006:15:04:05 -0700",
|
||||
"Jan _2 15:04:05",
|
||||
time.RFC1123Z,
|
||||
time.RFC1123,
|
||||
time.UnixDate,
|
||||
time.ANSIC,
|
||||
}
|
||||
|
||||
// parseLogTime reads the usual log timestamps; times without a zone are local.
|
||||
func parseLogTime(s string, now time.Time) (time.Time, bool) {
|
||||
s = strings.TrimSpace(s)
|
||||
if s == "" {
|
||||
return time.Time{}, false
|
||||
}
|
||||
if s[0] >= '0' && s[0] <= '9' && !strings.ContainsAny(s, ":/-") {
|
||||
if whole, _, _ := strings.Cut(s, "."); len(whole) < 9 {
|
||||
return time.Time{}, false // not a Unix time of this century
|
||||
}
|
||||
return epochTime(s)
|
||||
}
|
||||
// 2006/01/02 15:04:05,123 +02:00 -> 2006-01-02T15:04:05.123+02:00
|
||||
if len(s) >= 19 && s[4] == '/' && s[7] == '/' {
|
||||
s = s[:4] + "-" + s[5:7] + "-" + s[8:]
|
||||
}
|
||||
if len(s) >= 19 && s[4] == '-' && s[10] == ' ' {
|
||||
s = s[:10] + "T" + s[11:]
|
||||
}
|
||||
if len(s) > 19 && s[4] == '-' {
|
||||
s = s[:19] + strings.Replace(strings.Replace(s[19:], ",", ".", 1), " ", "", 1)
|
||||
}
|
||||
s = strings.Join(strings.Fields(s), " ") // "Jan 5" -> "Jan 5"
|
||||
for _, layout := range timeLayouts {
|
||||
if layout == "Jan _2 15:04:05" {
|
||||
layout = "Jan 2 15:04:05"
|
||||
}
|
||||
t, err := time.ParseInLocation(layout, s, time.Local)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if t.Year() == 0 { // syslog timestamps have no year
|
||||
t = t.AddDate(now.Year(), 0, 0)
|
||||
if t.After(now.Add(24 * time.Hour)) {
|
||||
t = t.AddDate(-1, 0, 0)
|
||||
}
|
||||
}
|
||||
return t, true
|
||||
}
|
||||
return time.Time{}, false
|
||||
}
|
||||
|
||||
// epochTime reads a Unix time in seconds, milliseconds, microseconds or
|
||||
// nanoseconds (guessed from its size), with an optional fraction.
|
||||
func epochTime(s string) (time.Time, bool) {
|
||||
f, err := strconv.ParseFloat(s, 64)
|
||||
if err != nil || f <= 0 {
|
||||
return time.Time{}, false
|
||||
}
|
||||
switch {
|
||||
case f < 1e11:
|
||||
return time.Unix(0, int64(f*1e9)), true
|
||||
case f < 1e14:
|
||||
return time.UnixMilli(int64(f)), true
|
||||
case f < 1e17:
|
||||
return time.UnixMicro(int64(f)), true
|
||||
}
|
||||
return time.Unix(0, int64(f)), true
|
||||
}
|
||||
|
||||
/* ---------- Fields ---------- */
|
||||
|
||||
// reservedFields are stored by LogStream itself: fields of the logs with the
|
||||
// same name are renamed field_<name>.
|
||||
var reservedFields = map[string]bool{
|
||||
"_time": true, "_msg": true, "_stream": true, "_stream_id": true, "received": true, "msg_time": true,
|
||||
"host": true, "host_name": true, "host_ip": true, "app": true, "facility": true, "severity": true,
|
||||
"sevnum": true, "source": true, "proto": true, "procid": true, "sd": true, "source_type": true, "log_file": true,
|
||||
}
|
||||
|
||||
func fieldName(k string) string {
|
||||
k = strings.Map(func(r rune) rune {
|
||||
if r == '_' || r == '.' || r == '-' || r >= '0' && r <= '9' || r >= 'a' && r <= 'z' || r >= 'A' && r <= 'Z' {
|
||||
return r
|
||||
}
|
||||
return '_'
|
||||
}, k)
|
||||
if k == "" || reservedFields[k] || k[0] == '_' {
|
||||
k = "field_" + strings.TrimLeft(k, "_")
|
||||
}
|
||||
return k
|
||||
}
|
||||
|
||||
func (r *fileRec) set(k, v string) {
|
||||
if v == "" {
|
||||
return
|
||||
}
|
||||
if r.fields == nil {
|
||||
r.fields = map[string]string{}
|
||||
}
|
||||
k = fieldName(k)
|
||||
if _, ok := r.fields[k]; !ok && len(r.fields) >= maxFileFields {
|
||||
return
|
||||
}
|
||||
if len(v) > maxFieldValue {
|
||||
v = strings.ToValidUTF8(v[:maxFieldValue], "") + "…"
|
||||
}
|
||||
r.fields[k] = v
|
||||
}
|
||||
|
||||
/* ---------- JSON lines ---------- */
|
||||
|
||||
func decodeJSONLine(line string) (map[string]any, bool) {
|
||||
s := strings.TrimSpace(line)
|
||||
if len(s) < 2 || s[0] != '{' {
|
||||
return nil, false
|
||||
}
|
||||
dec := json.NewDecoder(strings.NewReader(s))
|
||||
dec.UseNumber()
|
||||
var obj map[string]any
|
||||
if dec.Decode(&obj) != nil {
|
||||
return nil, false
|
||||
}
|
||||
return obj, true
|
||||
}
|
||||
|
||||
var (
|
||||
jsonMsgKeys = []string{"msg", "message", "@message", "log", "text"}
|
||||
jsonTimeKeys = []string{"time", "ts", "timestamp", "@timestamp", "@t", "t", "datetime", "date"}
|
||||
jsonLevelKeys = []string{"level", "lvl", "severity", "log.level", "@level", "@l", "loglevel", "levelname"}
|
||||
)
|
||||
|
||||
// takeString removes the first of keys found in obj and returns it as text.
|
||||
func takeString(obj map[string]any, keys []string) (string, bool) {
|
||||
for _, k := range keys {
|
||||
if v, ok := obj[k]; ok {
|
||||
if s := scalarString(v); s != "" {
|
||||
delete(obj, k)
|
||||
return s, true
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
func scalarString(v any) string {
|
||||
switch v := v.(type) {
|
||||
case string:
|
||||
return v
|
||||
case json.Number:
|
||||
return v.String()
|
||||
case bool:
|
||||
return strconv.FormatBool(v)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func jsonRec(obj map[string]any, line string, now time.Time) *fileRec {
|
||||
r := &fileRec{t: now, sev: 6, msg: line}
|
||||
for _, k := range jsonTimeKeys {
|
||||
if t, ok := parseLogTime(scalarString(obj[k]), now); ok {
|
||||
r.t, r.dated = t, true
|
||||
delete(obj, k)
|
||||
break
|
||||
}
|
||||
}
|
||||
lvl, hasLevel := takeString(obj, jsonLevelKeys)
|
||||
if hasLevel {
|
||||
if n, ok := levelNum(lvl); ok {
|
||||
r.sev = n
|
||||
} else if n, err := strconv.Atoi(lvl); err == nil {
|
||||
r.sev = numericLevel(n)
|
||||
} else {
|
||||
r.set("level", lvl)
|
||||
}
|
||||
}
|
||||
if s, ok := takeString(obj, jsonMsgKeys); ok {
|
||||
r.msg = strings.TrimRight(s, "\n")
|
||||
}
|
||||
flattenJSON("", obj, r)
|
||||
if !hasLevel {
|
||||
if n, ok := detectLevel(r.msg); ok {
|
||||
r.sev = n
|
||||
}
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// numericLevel maps numeric levels: syslog (0-7) or pino/bunyan (10-60).
|
||||
func numericLevel(n int) int {
|
||||
switch {
|
||||
case n >= 0 && n <= 7:
|
||||
return n
|
||||
case n >= 60:
|
||||
return 2
|
||||
case n >= 50:
|
||||
return 3
|
||||
case n >= 40:
|
||||
return 4
|
||||
case n >= 30:
|
||||
return 6
|
||||
}
|
||||
return 7
|
||||
}
|
||||
|
||||
// flattenJSON stores the remaining keys as fields; nested objects give
|
||||
// dotted names, arrays stay in JSON.
|
||||
func flattenJSON(prefix string, obj map[string]any, r *fileRec) {
|
||||
for k, v := range obj {
|
||||
name := prefix + k
|
||||
switch v := v.(type) {
|
||||
case map[string]any:
|
||||
flattenJSON(name+".", v, r)
|
||||
case []any:
|
||||
b, _ := json.Marshal(v)
|
||||
r.set(name, string(b))
|
||||
case nil:
|
||||
default:
|
||||
r.set(name, scalarString(v))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------- logfmt ---------- */
|
||||
|
||||
// parseLogfmt splits `k=v k2="v 2"` (with \" escapes); words without a value
|
||||
// are skipped. Nil when the line has no pair.
|
||||
func parseLogfmt(s string) [][2]string {
|
||||
var out [][2]string
|
||||
for i := 0; i < len(s); {
|
||||
for i < len(s) && (s[i] == ' ' || s[i] == '\t') {
|
||||
i++
|
||||
}
|
||||
start := i
|
||||
for i < len(s) && s[i] != '=' && s[i] != ' ' && s[i] != '\t' {
|
||||
i++
|
||||
}
|
||||
if i >= len(s) || s[i] != '=' || i == start {
|
||||
for i < len(s) && s[i] != ' ' && s[i] != '\t' {
|
||||
i++
|
||||
}
|
||||
continue
|
||||
}
|
||||
k := s[start:i]
|
||||
i++
|
||||
var v string
|
||||
if i < len(s) && s[i] == '"' {
|
||||
var b strings.Builder
|
||||
i++
|
||||
for i < len(s) && s[i] != '"' {
|
||||
if s[i] == '\\' && i+1 < len(s) {
|
||||
i++
|
||||
switch s[i] {
|
||||
case 'n':
|
||||
b.WriteByte('\n')
|
||||
case 't':
|
||||
b.WriteByte('\t')
|
||||
default:
|
||||
b.WriteByte(s[i])
|
||||
}
|
||||
} else {
|
||||
b.WriteByte(s[i])
|
||||
}
|
||||
i++
|
||||
}
|
||||
i++ // closing quote
|
||||
v = b.String()
|
||||
} else {
|
||||
end := i
|
||||
for end < len(s) && s[end] != ' ' && s[end] != '\t' {
|
||||
end++
|
||||
}
|
||||
v, i = s[i:end], end
|
||||
}
|
||||
out = append(out, [2]string{k, v})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func logfmtRec(kv [][2]string, line string, now time.Time) *fileRec {
|
||||
obj := make(map[string]any, len(kv))
|
||||
for _, p := range kv {
|
||||
obj[p[0]] = p[1]
|
||||
}
|
||||
return jsonRec(obj, line, now)
|
||||
}
|
||||
|
||||
/* ---------- Regular expression ---------- */
|
||||
|
||||
func (p *fileParser) regexRec(m []string, line string, now time.Time) *fileRec {
|
||||
r := &fileRec{t: now, sev: 6, msg: line}
|
||||
hasLevel := false
|
||||
for i, name := range p.re.SubexpNames() {
|
||||
if i == 0 || name == "" || m[i] == "" {
|
||||
continue
|
||||
}
|
||||
v := m[i]
|
||||
switch strings.ToLower(name) {
|
||||
case "time", "timestamp", "ts":
|
||||
if t, ok := parseLogTime(v, now); ok {
|
||||
r.t, r.dated = t, true
|
||||
}
|
||||
case "level", "severity", "lvl":
|
||||
if n, ok := levelNum(v); ok {
|
||||
r.sev, hasLevel = n, true
|
||||
}
|
||||
case "msg", "message":
|
||||
r.msg = v
|
||||
case "host":
|
||||
r.host = v
|
||||
case "app":
|
||||
r.app = v
|
||||
case "status":
|
||||
r.set(name, v)
|
||||
if n, ok := statusLevel(v); ok && !hasLevel {
|
||||
r.sev, hasLevel = n, true
|
||||
}
|
||||
default:
|
||||
r.set(name, v)
|
||||
}
|
||||
}
|
||||
if !hasLevel {
|
||||
if n, ok := detectLevel(r.msg); ok {
|
||||
r.sev = n
|
||||
}
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
/* ---------- Access logs ---------- */
|
||||
|
||||
// statusLevel: 5xx are errors, 4xx warnings, the rest info.
|
||||
func statusLevel(s string) (int, bool) {
|
||||
n, err := strconv.Atoi(s)
|
||||
if err != nil || n < 100 || n > 999 {
|
||||
return 0, false
|
||||
}
|
||||
switch {
|
||||
case n >= 500:
|
||||
return 3, true
|
||||
case n >= 400:
|
||||
return 4, true
|
||||
}
|
||||
return 6, true
|
||||
}
|
||||
|
||||
// clfRe: Common Log Format, the combined extension (referer, user agent) and
|
||||
// the Traefik one (request count, router, server URL, duration).
|
||||
var clfRe = regexp.MustCompile(`^(\S+) (\S+) (\S+) \[([^\]]+)\] "((?:[^"\\]|\\.)*)" (\d{3}) (\d+|-)(?: "((?:[^"\\]|\\.)*)" "((?:[^"\\]|\\.)*)")?(?: (\d+) "([^"]*)" "([^"]*)" (\d+)ms)?`)
|
||||
|
||||
func clfRec(line string, now time.Time) *fileRec {
|
||||
m := clfRe.FindStringSubmatch(line)
|
||||
if m == nil {
|
||||
return nil
|
||||
}
|
||||
r := &fileRec{t: now, sev: 6, msg: line}
|
||||
if t, ok := parseLogTime(m[4], now); ok {
|
||||
r.t, r.dated = t, true
|
||||
}
|
||||
dash := func(s string) string {
|
||||
if s == "-" {
|
||||
return ""
|
||||
}
|
||||
return s
|
||||
}
|
||||
r.set("client_ip", m[1])
|
||||
r.set("http_user", dash(m[3]))
|
||||
if req := strings.Fields(m[5]); len(req) == 3 {
|
||||
r.set("http_method", req[0])
|
||||
r.set("http_path", req[1])
|
||||
r.set("http_proto", req[2])
|
||||
} else {
|
||||
r.set("http_request", m[5])
|
||||
}
|
||||
r.set("http_status", m[6])
|
||||
r.set("http_size", dash(m[7]))
|
||||
r.set("http_referer", dash(m[8]))
|
||||
r.set("user_agent", dash(m[9]))
|
||||
r.set("router", dash(m[11]))
|
||||
r.set("backend", dash(m[12]))
|
||||
r.set("duration_ms", m[13])
|
||||
r.sev, _ = statusLevel(m[6])
|
||||
return r
|
||||
}
|
||||
|
||||
// traefikRec reads a Traefik access log line in JSON; nil for the other
|
||||
// lines (Traefik's own logs), read as generic JSON.
|
||||
func traefikRec(obj map[string]any, line string, now time.Time) *fileRec {
|
||||
status := scalarString(obj["DownstreamStatus"])
|
||||
if status == "" {
|
||||
status = scalarString(obj["OriginStatus"])
|
||||
}
|
||||
method := scalarString(obj["RequestMethod"])
|
||||
if status == "" && method == "" {
|
||||
return nil
|
||||
}
|
||||
r := &fileRec{t: now, sev: 6}
|
||||
if t, ok := parseLogTime(firstNonEmpty(scalarString(obj["StartUTC"]), scalarString(obj["time"])), now); ok {
|
||||
r.t, r.dated = t, true
|
||||
}
|
||||
if n, ok := statusLevel(status); ok {
|
||||
r.sev = n
|
||||
}
|
||||
rename := map[string]string{
|
||||
"ClientHost": "client_ip", "ClientUsername": "http_user", "RequestMethod": "http_method",
|
||||
"RequestHost": "http_host", "RequestPath": "http_path", "RequestProtocol": "http_proto",
|
||||
"DownstreamStatus": "http_status", "DownstreamContentSize": "http_size", "OriginStatus": "origin_status",
|
||||
"RouterName": "router", "ServiceName": "service", "ServiceURL": "backend", "entryPointName": "entrypoint",
|
||||
"request_User-Agent": "user_agent", "request_Referer": "http_referer",
|
||||
}
|
||||
dur := ""
|
||||
if d, err := strconv.ParseFloat(scalarString(obj["Duration"]), 64); err == nil {
|
||||
dur = strconv.FormatFloat(d/1e6, 'f', -1, 64)
|
||||
r.set("duration_ms", dur)
|
||||
}
|
||||
for _, k := range []string{"Duration", "StartUTC", "StartLocal", "time", "level", "msg"} {
|
||||
delete(obj, k)
|
||||
}
|
||||
for k, v := range obj {
|
||||
if to, ok := rename[k]; ok {
|
||||
r.set(to, scalarString(v))
|
||||
delete(obj, k)
|
||||
}
|
||||
}
|
||||
flattenJSON("", obj, r)
|
||||
// "GET example.com/path 404 · 12.5 ms · router@docker"
|
||||
var b strings.Builder
|
||||
b.WriteString(firstNonEmpty(method, "-"))
|
||||
b.WriteByte(' ')
|
||||
b.WriteString(r.fields["http_host"])
|
||||
b.WriteString(r.fields["http_path"])
|
||||
b.WriteByte(' ')
|
||||
b.WriteString(firstNonEmpty(status, "-"))
|
||||
if dur != "" {
|
||||
b.WriteString(" · " + dur + " ms")
|
||||
}
|
||||
if rt := r.fields["router"]; rt != "" {
|
||||
b.WriteString(" · " + rt)
|
||||
}
|
||||
if ip := r.fields["client_ip"]; ip != "" {
|
||||
b.WriteString(" · " + ip)
|
||||
}
|
||||
r.msg = b.String()
|
||||
return r
|
||||
}
|
||||
|
||||
/* ---------- Detection ---------- */
|
||||
|
||||
const detectWindow = 64 << 10
|
||||
|
||||
// sampleLines returns the last complete lines of a file (50 at most).
|
||||
func sampleLines(path string) ([]string, error) {
|
||||
fh, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer fh.Close()
|
||||
fi, err := fh.Stat()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
off := fi.Size() - detectWindow
|
||||
if off < 0 {
|
||||
off = 0
|
||||
}
|
||||
buf, err := io.ReadAll(io.NewSectionReader(fh, off, detectWindow))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
end := bytes.LastIndexByte(buf, '\n')
|
||||
if end < 0 {
|
||||
return nil, nil
|
||||
}
|
||||
buf = buf[:end]
|
||||
if off > 0 { // first line cut by the window
|
||||
if nl := bytes.IndexByte(buf, '\n'); nl >= 0 {
|
||||
buf = buf[nl+1:]
|
||||
} else {
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
var lines []string
|
||||
for _, b := range bytes.Split(buf, []byte{'\n'}) {
|
||||
if len(bytes.TrimSpace(b)) > 0 {
|
||||
lines = append(lines, string(bytes.TrimRight(b, "\r")))
|
||||
}
|
||||
}
|
||||
if len(lines) > 50 {
|
||||
lines = lines[len(lines)-50:]
|
||||
}
|
||||
return lines, nil
|
||||
}
|
||||
|
||||
// logfmtKeys: the pairs have one of the usual logfmt keys.
|
||||
func logfmtKeys(kv [][2]string) bool {
|
||||
for _, p := range kv {
|
||||
switch strings.ToLower(p[0]) {
|
||||
case "level", "lvl", "msg", "message", "time", "ts", "t":
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// detectFormat guesses the format of a file from some of its lines: the
|
||||
// structured format most lines follow, else plain text (a regular
|
||||
// expression is never guessed).
|
||||
func detectFormat(lines []string) string {
|
||||
var js, traefik, clf, logfmt int
|
||||
for _, l := range lines {
|
||||
l = strings.TrimSpace(l)
|
||||
if obj, ok := decodeJSONLine(l); ok {
|
||||
js++
|
||||
if _, ok := obj["RequestMethod"]; ok {
|
||||
traefik++
|
||||
} else if _, ok := obj["DownstreamStatus"]; ok {
|
||||
traefik++
|
||||
}
|
||||
continue
|
||||
}
|
||||
if clfRe.MatchString(l) {
|
||||
clf++
|
||||
continue
|
||||
}
|
||||
if kv := parseLogfmt(l); len(kv) >= 3 && logfmtKeys(kv) {
|
||||
logfmt++
|
||||
}
|
||||
}
|
||||
most := func(n int) bool { return n > 0 && n*5 >= len(lines)*4 } // 80 %
|
||||
switch {
|
||||
case most(js) && traefik*2 >= js:
|
||||
return fmtTraefik
|
||||
case most(js):
|
||||
return fmtJSON
|
||||
case most(clf):
|
||||
return fmtCLF
|
||||
case most(logfmt):
|
||||
return fmtLogfmt
|
||||
}
|
||||
return fmtText
|
||||
}
|
||||
+639
@@ -0,0 +1,639 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// fileSourceDef is one log file declared in Settings > Sources.
|
||||
type fileSourceDef struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"` // app of the entries
|
||||
Path string `json:"path"` // the file, as mounted in the container
|
||||
Format string `json:"format"` // auto (detected), or text, json, logfmt, regex, clf, traefik
|
||||
Regex string `json:"regex,omitempty"` // format regex: named groups time, level, msg, host, app…
|
||||
Host string `json:"host,omitempty"` // host of the entries (empty: the host name of the machine)
|
||||
Enabled bool `json:"enabled"`
|
||||
}
|
||||
|
||||
// filesConfig is saved in /data/files.json.
|
||||
type filesConfig struct {
|
||||
Sources []fileSourceDef `json:"sources"`
|
||||
}
|
||||
|
||||
// filePos is where reading resumes in a file: its inode and an offset before
|
||||
// which every entry was sent. Until: end of the history found when the
|
||||
// source was turned on; entries before it are kept only when dated within
|
||||
// FILES_BACKFILL.
|
||||
type filePos struct {
|
||||
Ino uint64 `json:"ino"`
|
||||
Off int64 `json:"off"`
|
||||
Until int64 `json:"until,omitempty"`
|
||||
}
|
||||
|
||||
type filesState struct {
|
||||
Pos map[string]filePos `json:"pos"` // by source id
|
||||
Started map[string]time.Time `json:"started"` // when each source was turned on
|
||||
}
|
||||
|
||||
type fileSourceStatus struct {
|
||||
Open bool
|
||||
Read uint64
|
||||
Detected string // format found when the source is in auto
|
||||
Code string
|
||||
Error string
|
||||
}
|
||||
|
||||
// fileSource is a definition with its parser (nil in auto: detected from
|
||||
// the file).
|
||||
type fileSource struct {
|
||||
fileSourceDef
|
||||
parser *fileParser
|
||||
}
|
||||
|
||||
// pendingEntry is an entry waiting for its continuation lines.
|
||||
type pendingEntry struct {
|
||||
rec *fileRec
|
||||
off int64 // offset of its first line
|
||||
seen time.Time // when its last line was read
|
||||
history bool // read from the history found when the source was turned on
|
||||
lines int
|
||||
}
|
||||
|
||||
// fileTail is the reading state of one file, owned by the Run goroutine.
|
||||
type fileTail struct {
|
||||
readOff int64 // where the next read starts (after the lines of the pending entry)
|
||||
known bool // readOff is set
|
||||
pending *pendingEntry
|
||||
auto *fileParser // format detected from the file, in auto
|
||||
}
|
||||
|
||||
// Files follows log files of other programs (Traefik, nginx, applications…)
|
||||
// mounted in the container, one entry per line in the format detected or
|
||||
// chosen for each file. Source type "file".
|
||||
type Files struct {
|
||||
sink func(*Entry)
|
||||
hostName string
|
||||
backfill time.Duration
|
||||
cfgPath string
|
||||
statePath string
|
||||
wake chan struct{}
|
||||
|
||||
// Owned by the Run goroutine.
|
||||
st filesState
|
||||
tails map[string]*fileTail
|
||||
dirty bool
|
||||
|
||||
mu sync.Mutex
|
||||
cfg []*fileSource
|
||||
resets map[string]bool // sources to read again from FILES_BACKFILL ago
|
||||
redetect map[string]bool // sources whose format changed
|
||||
status map[string]*fileSourceStatus
|
||||
}
|
||||
|
||||
func NewFiles(hostRoot, dataDir string, backfill time.Duration, sink func(*Entry)) *Files {
|
||||
f := &Files{
|
||||
sink: sink,
|
||||
hostName: readTrim(filepath.Join(hostRoot, "etc/hostname")),
|
||||
backfill: backfill,
|
||||
cfgPath: filepath.Join(dataDir, "files.json"),
|
||||
statePath: filepath.Join(dataDir, "files-state.json"),
|
||||
wake: make(chan struct{}, 1),
|
||||
tails: map[string]*fileTail{},
|
||||
resets: map[string]bool{},
|
||||
redetect: map[string]bool{},
|
||||
status: map[string]*fileSourceStatus{},
|
||||
}
|
||||
if f.hostName == "" {
|
||||
f.hostName = "localhost"
|
||||
}
|
||||
var cfg filesConfig
|
||||
if b, err := os.ReadFile(f.cfgPath); err == nil {
|
||||
_ = json.Unmarshal(b, &cfg)
|
||||
}
|
||||
for _, d := range cfg.Sources {
|
||||
src, err := compileFileSource(d)
|
||||
if err != nil {
|
||||
logEvent("files", 4, "file %q ignored: %v", d.Name, err)
|
||||
continue
|
||||
}
|
||||
f.cfg = append(f.cfg, src)
|
||||
}
|
||||
if b, err := os.ReadFile(f.statePath); err == nil {
|
||||
_ = json.Unmarshal(b, &f.st)
|
||||
}
|
||||
if f.st.Pos == nil {
|
||||
f.st.Pos = map[string]filePos{}
|
||||
}
|
||||
if f.st.Started == nil {
|
||||
f.st.Started = map[string]time.Time{}
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
const maxFileSources = 50
|
||||
|
||||
var fileIDRe = regexp.MustCompile(`^[0-9a-f]{1,32}$`)
|
||||
|
||||
// checkFilePath: one file, absolute, without wildcards.
|
||||
func checkFilePath(p string) (string, error) {
|
||||
p = strings.TrimSpace(p)
|
||||
switch {
|
||||
case !filepath.IsAbs(p):
|
||||
return "", errors.New("the path must be absolute (as mounted in the container)")
|
||||
case strings.ContainsAny(p, "*?["):
|
||||
return "", errors.New("give the path of one file (no wildcards)")
|
||||
}
|
||||
return filepath.Clean(p), nil
|
||||
}
|
||||
|
||||
// compileFileSource checks a definition and builds its parser.
|
||||
func compileFileSource(d fileSourceDef) (*fileSource, error) {
|
||||
d.Name = strings.TrimSpace(d.Name)
|
||||
d.Host = strings.TrimSpace(d.Host)
|
||||
if d.Format == "" {
|
||||
d.Format = fmtAuto
|
||||
}
|
||||
if d.Format != fmtRegex {
|
||||
d.Regex = ""
|
||||
}
|
||||
switch {
|
||||
case !fileIDRe.MatchString(d.ID):
|
||||
return nil, errors.New("invalid id")
|
||||
case d.Name == "" || len(d.Name) > 64:
|
||||
return nil, errors.New("the name is required (64 characters at most)")
|
||||
case len(d.Host) > 253:
|
||||
return nil, fmt.Errorf("%s: host name too long", d.Name)
|
||||
}
|
||||
p, err := checkFilePath(d.Path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: %v", d.Name, err)
|
||||
}
|
||||
d.Path = p
|
||||
src := &fileSource{fileSourceDef: d}
|
||||
if d.Format != fmtAuto {
|
||||
if src.parser, err = newFileParser(d.Format, d.Regex); err != nil {
|
||||
return nil, fmt.Errorf("%s: %v", d.Name, err)
|
||||
}
|
||||
}
|
||||
return src, nil
|
||||
}
|
||||
|
||||
func newFileID() string {
|
||||
b := make([]byte, 6)
|
||||
_, _ = rand.Read(b)
|
||||
return hex.EncodeToString(b)
|
||||
}
|
||||
|
||||
// Configure checks, saves and applies the list of files. Files turned off,
|
||||
// removed or given another path are read again from FILES_BACKFILL ago the
|
||||
// next time they are on.
|
||||
func (f *Files) Configure(cfg filesConfig) error {
|
||||
if len(cfg.Sources) > maxFileSources {
|
||||
return fmt.Errorf("%d files at most", maxFileSources)
|
||||
}
|
||||
var srcs []*fileSource
|
||||
ids := map[string]bool{}
|
||||
for _, d := range cfg.Sources {
|
||||
if d.ID == "" {
|
||||
d.ID = newFileID()
|
||||
}
|
||||
if ids[d.ID] {
|
||||
return fmt.Errorf("duplicate id %s", d.ID)
|
||||
}
|
||||
ids[d.ID] = true
|
||||
src, err := compileFileSource(d)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
srcs = append(srcs, src)
|
||||
}
|
||||
saved := filesConfig{Sources: []fileSourceDef{}}
|
||||
for _, s := range srcs {
|
||||
saved.Sources = append(saved.Sources, s.fileSourceDef)
|
||||
}
|
||||
if err := writeJSONFile(f.cfgPath, saved); err != nil {
|
||||
return err
|
||||
}
|
||||
f.mu.Lock()
|
||||
old := map[string]*fileSource{}
|
||||
for _, s := range f.cfg {
|
||||
old[s.ID] = s
|
||||
}
|
||||
for _, s := range srcs {
|
||||
o := old[s.ID]
|
||||
if o != nil && ((o.Enabled && !s.Enabled) || o.Path != s.Path) {
|
||||
f.resets[s.ID] = true
|
||||
delete(f.status, s.ID)
|
||||
}
|
||||
if o != nil && (o.Format != s.Format || o.Regex != s.Regex) {
|
||||
f.redetect[s.ID] = true
|
||||
}
|
||||
delete(old, s.ID)
|
||||
}
|
||||
for id := range old { // removed
|
||||
f.resets[id] = true
|
||||
delete(f.status, id)
|
||||
}
|
||||
f.cfg = srcs
|
||||
f.mu.Unlock()
|
||||
select {
|
||||
case f.wake <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Status is the state shown in Settings > Sources.
|
||||
func (f *Files) Status() map[string]any {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
list := make([]map[string]any, 0, len(f.cfg))
|
||||
for _, s := range f.cfg {
|
||||
m := map[string]any{
|
||||
"id": s.ID, "name": s.Name, "path": s.Path, "format": s.Format, "regex": s.Regex,
|
||||
"host": s.Host, "enabled": s.Enabled, "open": false, "read": 0, "detected": "", "code": "", "error": "",
|
||||
}
|
||||
if st := f.status[s.ID]; st != nil && s.Enabled {
|
||||
m["open"], m["read"], m["detected"], m["code"], m["error"] = st.Open, st.Read, st.Detected, st.Code, st.Error
|
||||
}
|
||||
list = append(list, m)
|
||||
}
|
||||
return map[string]any{"sources": list, "defaultHost": f.hostName}
|
||||
}
|
||||
|
||||
// Run polls the files every second.
|
||||
func (f *Files) Run(ctx context.Context) {
|
||||
tick := time.NewTicker(time.Second)
|
||||
defer tick.Stop()
|
||||
n := 0
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
now := time.Now()
|
||||
for id := range f.tails {
|
||||
f.flushPending(id, now)
|
||||
}
|
||||
f.checkpointAll()
|
||||
f.saveState()
|
||||
return
|
||||
case <-tick.C:
|
||||
case <-f.wake:
|
||||
}
|
||||
f.poll(time.Now())
|
||||
if n++; n%5 == 0 {
|
||||
f.saveState()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (f *Files) saveState() {
|
||||
if !f.dirty {
|
||||
return
|
||||
}
|
||||
f.dirty = false
|
||||
if err := writeJSONFile(f.statePath, f.st); err != nil {
|
||||
logEvent("files", 4, "saving positions: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// poll reads what was added to every file that is on.
|
||||
func (f *Files) poll(now time.Time) {
|
||||
f.mu.Lock()
|
||||
srcs := f.cfg
|
||||
resets, redetect := f.resets, f.redetect
|
||||
f.resets, f.redetect = map[string]bool{}, map[string]bool{}
|
||||
f.mu.Unlock()
|
||||
for id := range resets {
|
||||
delete(f.st.Pos, id)
|
||||
delete(f.st.Started, id)
|
||||
delete(f.tails, id)
|
||||
f.dirty = true
|
||||
}
|
||||
for id := range redetect {
|
||||
if tl := f.tails[id]; tl != nil {
|
||||
tl.auto = nil
|
||||
}
|
||||
}
|
||||
active := map[string]bool{}
|
||||
for _, s := range srcs {
|
||||
if !s.Enabled {
|
||||
continue
|
||||
}
|
||||
active[s.ID] = true
|
||||
f.setStatus(s.ID, f.pollFile(s, now))
|
||||
}
|
||||
// Files turned off: their position is kept until the next poll forgets it.
|
||||
for id := range f.tails {
|
||||
if !active[id] {
|
||||
delete(f.tails, id)
|
||||
}
|
||||
}
|
||||
f.checkpointAll()
|
||||
}
|
||||
|
||||
const (
|
||||
maxFileRead = 4 << 20 // per file and per poll
|
||||
fileContinueDelay = 2 * time.Second // an entry waits this long for continuation lines
|
||||
)
|
||||
|
||||
func (f *Files) setStatus(id string, err error) {
|
||||
code, detail := "", ""
|
||||
if err != nil {
|
||||
code, detail = errCode(err), err.Error()
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
code = "not_found"
|
||||
}
|
||||
}
|
||||
detected := ""
|
||||
if tl := f.tails[id]; tl != nil && tl.auto != nil {
|
||||
detected = tl.auto.format
|
||||
}
|
||||
f.mu.Lock()
|
||||
st := f.status[id]
|
||||
if st == nil {
|
||||
st = &fileSourceStatus{}
|
||||
f.status[id] = st
|
||||
}
|
||||
changed := code != st.Code
|
||||
st.Open, st.Detected, st.Code, st.Error = err == nil, detected, code, detail
|
||||
name := id
|
||||
for _, s := range f.cfg {
|
||||
if s.ID == id {
|
||||
name = s.Name
|
||||
}
|
||||
}
|
||||
f.mu.Unlock()
|
||||
if changed && code != "" && code != "not_found" {
|
||||
logEvent("files", 4, "cannot read the file %s: %v", name, err)
|
||||
}
|
||||
}
|
||||
|
||||
func (f *Files) count(id string) {
|
||||
f.mu.Lock()
|
||||
if st := f.status[id]; st != nil {
|
||||
st.Read++
|
||||
}
|
||||
f.mu.Unlock()
|
||||
}
|
||||
|
||||
// parserOf is the parser of a file: the chosen format, or the one detected
|
||||
// from its last lines (nil while the file has none).
|
||||
func (f *Files) parserOf(s *fileSource, tl *fileTail, path string) *fileParser {
|
||||
if s.parser != nil {
|
||||
return s.parser
|
||||
}
|
||||
if tl.auto == nil {
|
||||
lines, err := sampleLines(path)
|
||||
if err != nil || len(lines) == 0 {
|
||||
return nil
|
||||
}
|
||||
tl.auto, _ = newFileParser(detectFormat(lines), "")
|
||||
}
|
||||
return tl.auto
|
||||
}
|
||||
|
||||
func (f *Files) pollFile(s *fileSource, now time.Time) error {
|
||||
path := s.Path
|
||||
first := false
|
||||
if _, ok := f.st.Started[s.ID]; !ok {
|
||||
f.st.Started[s.ID], f.dirty, first = now, true, true
|
||||
}
|
||||
fi, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !fi.Mode().IsRegular() {
|
||||
return fmt.Errorf("%s is not a regular file", path)
|
||||
}
|
||||
tl := f.tails[s.ID]
|
||||
if tl == nil {
|
||||
tl = &fileTail{}
|
||||
f.tails[s.ID] = tl
|
||||
}
|
||||
ino := inodeOf(fi)
|
||||
pos, known := f.st.Pos[s.ID]
|
||||
if !known {
|
||||
pos = filePos{Ino: ino}
|
||||
if first {
|
||||
// Turned on: the last FILES_BACKFILL of what is already there
|
||||
// (0: only new lines).
|
||||
if f.backfill > 0 {
|
||||
pos.Until = fi.Size()
|
||||
} else {
|
||||
pos.Off = fi.Size()
|
||||
}
|
||||
}
|
||||
f.st.Pos[s.ID], f.dirty = pos, true
|
||||
}
|
||||
if !tl.known {
|
||||
tl.readOff, tl.known = pos.Off, true
|
||||
}
|
||||
if pos.Ino != ino {
|
||||
// Rotated (access.log -> access.log.1): finish the old file, then
|
||||
// read the new one from its start.
|
||||
if old, err := os.Stat(path + ".1"); err == nil && inodeOf(old) == pos.Ino {
|
||||
if p := f.parserOf(s, tl, path+".1"); p != nil {
|
||||
for {
|
||||
next, more, err := f.readFrom(s, tl, p, path+".1", tl.readOff, pos.Until, now)
|
||||
if err != nil || !more {
|
||||
break
|
||||
}
|
||||
tl.readOff = next
|
||||
}
|
||||
}
|
||||
}
|
||||
f.flushPending(s.ID, now)
|
||||
pos, tl.readOff = filePos{Ino: ino}, 0
|
||||
f.st.Pos[s.ID], f.dirty = pos, true
|
||||
} else if fi.Size() < tl.readOff {
|
||||
f.flushPending(s.ID, now) // truncated (copytruncate)
|
||||
pos.Until, tl.readOff = 0, 0
|
||||
f.st.Pos[s.ID], f.dirty = pos, true
|
||||
}
|
||||
if fi.Size() > tl.readOff {
|
||||
p := f.parserOf(s, tl, path)
|
||||
if p == nil {
|
||||
return nil // nothing complete to detect the format from yet
|
||||
}
|
||||
next, _, err := f.readFrom(s, tl, p, path, tl.readOff, pos.Until, now)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tl.readOff = next
|
||||
}
|
||||
if pe := tl.pending; pe != nil && now.Sub(pe.seen) >= fileContinueDelay {
|
||||
f.flushPending(s.ID, now)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// readFrom parses the complete lines written after off and returns the new
|
||||
// offset; more tells whether the read window was full.
|
||||
func (f *Files) readFrom(s *fileSource, tl *fileTail, p *fileParser, path string, off, until int64, now time.Time) (int64, bool, error) {
|
||||
fh, err := os.Open(path)
|
||||
if err != nil {
|
||||
return off, false, err
|
||||
}
|
||||
defer fh.Close()
|
||||
buf, err := io.ReadAll(io.NewSectionReader(fh, off, maxFileRead))
|
||||
if err != nil {
|
||||
return off, false, err
|
||||
}
|
||||
full := len(buf) == maxFileRead
|
||||
end := bytes.LastIndexByte(buf, '\n')
|
||||
if end < 0 {
|
||||
if !full {
|
||||
return off, false, nil // partial line: wait for its end
|
||||
}
|
||||
end = len(buf) - 1 // line longer than the read window: cut it
|
||||
}
|
||||
start := off
|
||||
for _, b := range bytes.Split(buf[:end], []byte{'\n'}) {
|
||||
f.line(s, tl, p, string(b), start, start < until, now)
|
||||
start += int64(len(b)) + 1
|
||||
}
|
||||
return off + int64(end) + 1, full, nil
|
||||
}
|
||||
|
||||
// line handles one line: a continuation of the pending entry, or a new one.
|
||||
func (f *Files) line(s *fileSource, tl *fileTail, p *fileParser, line string, off int64, history bool, now time.Time) {
|
||||
if strings.TrimSpace(line) == "" {
|
||||
return
|
||||
}
|
||||
if pe := tl.pending; p.multiline() && pe != nil && p.continuation(line) {
|
||||
if pe.lines < maxContinuation && len(pe.rec.msg) < maxFileMsg {
|
||||
pe.rec.msg += "\n" + strings.TrimRight(cleanLine(line), " \t")
|
||||
pe.lines++
|
||||
}
|
||||
pe.seen = now
|
||||
return
|
||||
}
|
||||
f.flushPending(s.ID, now)
|
||||
pe := &pendingEntry{rec: p.parse(line, now), off: off, seen: now, history: history}
|
||||
if p.multiline() {
|
||||
tl.pending = pe
|
||||
return
|
||||
}
|
||||
f.emit(s, pe, now)
|
||||
}
|
||||
|
||||
func (f *Files) flushPending(id string, now time.Time) {
|
||||
tl := f.tails[id]
|
||||
if tl == nil || tl.pending == nil {
|
||||
return
|
||||
}
|
||||
pe := tl.pending
|
||||
tl.pending = nil
|
||||
f.mu.Lock()
|
||||
var src *fileSource
|
||||
for _, s := range f.cfg {
|
||||
if s.ID == id {
|
||||
src = s
|
||||
}
|
||||
}
|
||||
f.mu.Unlock()
|
||||
if src != nil {
|
||||
f.emit(src, pe, now)
|
||||
}
|
||||
}
|
||||
|
||||
// emit sends an entry; history entries are kept only when dated within
|
||||
// FILES_BACKFILL of when the file was turned on.
|
||||
func (f *Files) emit(s *fileSource, pe *pendingEntry, now time.Time) {
|
||||
r := pe.rec
|
||||
if pe.history {
|
||||
notBefore := f.st.Started[s.ID].Add(-f.backfill)
|
||||
if !r.dated || r.t.Before(notBefore) {
|
||||
return
|
||||
}
|
||||
}
|
||||
if len(r.msg) > maxFileMsg {
|
||||
r.msg = strings.ToValidUTF8(r.msg[:maxFileMsg], "") + "…"
|
||||
}
|
||||
extra := map[string]string{"log_file": s.Path}
|
||||
for k, v := range r.fields {
|
||||
extra[k] = v
|
||||
}
|
||||
f.sink(&Entry{
|
||||
Time: r.t,
|
||||
Received: now,
|
||||
Host: firstNonEmpty(r.host, s.Host, f.hostName),
|
||||
App: firstNonEmpty(r.app, s.Name),
|
||||
SevNum: r.sev,
|
||||
Severity: severityNames[r.sev],
|
||||
Message: r.msg,
|
||||
Proto: "file",
|
||||
SourceType: "file",
|
||||
Extra: extra,
|
||||
Wait: true, // the file keeps the lines: wait for room in the queue
|
||||
})
|
||||
f.count(s.ID)
|
||||
}
|
||||
|
||||
// checkpointAll saves, for each file, an offset before which every entry is
|
||||
// sent: the start of its pending entry, or the read offset.
|
||||
func (f *Files) checkpointAll() {
|
||||
for id, tl := range f.tails {
|
||||
pos, ok := f.st.Pos[id]
|
||||
if !ok || !tl.known {
|
||||
continue
|
||||
}
|
||||
off := tl.readOff
|
||||
if pe := tl.pending; pe != nil && pe.off < off {
|
||||
off = pe.off
|
||||
}
|
||||
if off != pos.Off {
|
||||
pos.Off, f.dirty = off, true
|
||||
f.st.Pos[id] = pos
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// DetectFile tells, for the settings, whether a file can be read and its
|
||||
// format, with a line as example.
|
||||
func DetectFile(path string) map[string]any {
|
||||
res := map[string]any{"format": "", "sample": "", "code": "", "error": ""}
|
||||
p, err := checkFilePath(path)
|
||||
if err != nil {
|
||||
res["code"], res["error"] = "invalid", err.Error()
|
||||
return res
|
||||
}
|
||||
var lines []string
|
||||
if fi, err2 := os.Stat(p); err2 != nil {
|
||||
err = err2
|
||||
} else if !fi.Mode().IsRegular() {
|
||||
err = fmt.Errorf("%s is not a regular file", p)
|
||||
} else {
|
||||
lines, err = sampleLines(p)
|
||||
}
|
||||
switch {
|
||||
case errors.Is(err, fs.ErrNotExist):
|
||||
res["code"], res["error"] = "not_found", err.Error()
|
||||
case err != nil:
|
||||
res["code"], res["error"] = errCode(err), err.Error()
|
||||
case len(lines) == 0:
|
||||
res["code"] = "empty"
|
||||
default:
|
||||
res["format"] = detectFormat(lines)
|
||||
sample := lines[len(lines)-1]
|
||||
if len(sample) > 300 {
|
||||
sample = strings.ToValidUTF8(sample[:300], "") + "…"
|
||||
}
|
||||
res["sample"] = cleanLine(sample)
|
||||
}
|
||||
return res
|
||||
}
|
||||
@@ -0,0 +1,384 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func mustParser(t *testing.T, format, expr string) *fileParser {
|
||||
t.Helper()
|
||||
p, err := newFileParser(format, expr)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func TestParseLogTime(t *testing.T) {
|
||||
now := time.Date(2026, 10, 5, 12, 0, 0, 0, time.UTC)
|
||||
utc := func(s string) time.Time { t, _ := time.Parse(time.RFC3339Nano, s); return t }
|
||||
tests := []struct {
|
||||
in string
|
||||
want time.Time
|
||||
}{
|
||||
{"2026-10-05T10:00:00.123Z", utc("2026-10-05T10:00:00.123Z")},
|
||||
{"2026-10-05T10:00:00+02:00", utc("2026-10-05T08:00:00Z")},
|
||||
{"2026-10-05 10:00:00,250 +0200", utc("2026-10-05T08:00:00.25Z")},
|
||||
{"2026/10/05 10:00:00Z", utc("2026-10-05T10:00:00Z")},
|
||||
{"05/Oct/2026:10:00:00 +0200", utc("2026-10-05T08:00:00Z")},
|
||||
{"1759572000", time.Unix(1759572000, 0)},
|
||||
{"1759572000123", time.UnixMilli(1759572000123)},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
got, ok := parseLogTime(tt.in, now)
|
||||
if !ok || !got.Equal(tt.want) {
|
||||
t.Errorf("%q: got %v %v, want %v", tt.in, got, ok, tt.want)
|
||||
}
|
||||
}
|
||||
// Syslog timestamps: no year, the last one before tomorrow.
|
||||
if got, ok := parseLogTime("Dec 31 23:00:00", now); !ok || got.Year() != 2025 {
|
||||
t.Errorf("Dec 31: %v %v", got, ok)
|
||||
}
|
||||
if got, ok := parseLogTime("Oct 5 11:00:00", now); !ok || got.Year() != 2026 || got.Day() != 5 {
|
||||
t.Errorf("Oct 5: %v %v", got, ok)
|
||||
}
|
||||
for _, bad := range []string{"", "hello", "200", "12345"} {
|
||||
if _, ok := parseLogTime(bad, now); ok {
|
||||
t.Errorf("%q parsed", bad)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseText(t *testing.T) {
|
||||
now := time.Now()
|
||||
p := mustParser(t, fmtText, "")
|
||||
r := p.parse("2026-10-05 10:00:00.5 ERROR db: connection refused", now)
|
||||
if !r.dated || r.msg != "ERROR db: connection refused" || r.sev != 3 {
|
||||
t.Errorf("got %+v", r)
|
||||
}
|
||||
r = p.parse("[2026-10-05T10:00:00Z] - started", now)
|
||||
if !r.dated || r.msg != "started" || r.sev != 6 {
|
||||
t.Errorf("got %+v", r)
|
||||
}
|
||||
r = p.parse("\x1b[33mWARN\x1b[0m no timestamp here", now)
|
||||
if r.dated || r.msg != "WARN no timestamp here" || r.sev != 4 || !r.t.Equal(now) {
|
||||
t.Errorf("got %+v", r)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseJSON(t *testing.T) {
|
||||
now := time.Now()
|
||||
p := mustParser(t, fmtJSON, "")
|
||||
r := p.parse(`{"time":"2026-10-05T10:00:00Z","level":"warn","msg":"slow query","host":"db.example","req":{"id":7,"tags":["a"]},"ok":true}`, now)
|
||||
if !r.dated || r.sev != 4 || r.msg != "slow query" {
|
||||
t.Errorf("got %+v", r)
|
||||
}
|
||||
for k, want := range map[string]string{"field_host": "db.example", "req.id": "7", "req.tags": `["a"]`, "ok": "true"} {
|
||||
if r.fields[k] != want {
|
||||
t.Errorf("%s = %q, want %q", k, r.fields[k], want)
|
||||
}
|
||||
}
|
||||
// pino: numeric level, epoch in ms.
|
||||
r = p.parse(`{"level":50,"time":1759572000123,"msg":"boom"}`, now)
|
||||
if r.sev != 3 || !r.t.Equal(time.UnixMilli(1759572000123)) {
|
||||
t.Errorf("got %+v", r)
|
||||
}
|
||||
// No message key: the whole line; not JSON: text.
|
||||
line := `{"event":"login","user":"bob"}`
|
||||
if r = p.parse(line, now); r.msg != line || r.fields["user"] != "bob" {
|
||||
t.Errorf("got %+v", r)
|
||||
}
|
||||
if r = p.parse("plain ERROR line", now); r.msg != "plain ERROR line" || r.sev != 3 {
|
||||
t.Errorf("got %+v", r)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseLogfmt(t *testing.T) {
|
||||
p := mustParser(t, fmtLogfmt, "")
|
||||
r := p.parse(`time="2026-10-05T10:00:00Z" level=error msg="Cannot connect: \"docker\" down" providerName=docker flag`, time.Now())
|
||||
if !r.dated || r.sev != 3 || r.msg != `Cannot connect: "docker" down` || r.fields["providerName"] != "docker" {
|
||||
t.Errorf("got %+v", r)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseRegex(t *testing.T) {
|
||||
if _, err := newFileParser(fmtRegex, "("); err == nil {
|
||||
t.Error("invalid regex accepted")
|
||||
}
|
||||
if _, err := newFileParser(fmtRegex, ""); err == nil {
|
||||
t.Error("empty regex accepted")
|
||||
}
|
||||
p := mustParser(t, fmtRegex, `^(?P<time>\S+ \S+) \[(?P<level>\w+)\] (?P<module>[\w.]+): (?P<msg>.*)$`)
|
||||
r := p.parse("2026-10-05 10:00:00 [warning] app.db: pool exhausted", time.Now())
|
||||
if !r.dated || r.sev != 4 || r.msg != "pool exhausted" || r.fields["module"] != "app.db" {
|
||||
t.Errorf("got %+v", r)
|
||||
}
|
||||
if !p.continuation("Traceback (most recent call last):") || p.continuation("2026-10-05 10:00:01 [info] a: b") {
|
||||
t.Error("continuation")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseCLF(t *testing.T) {
|
||||
p := mustParser(t, fmtCLF, "")
|
||||
r := p.parse(`203.0.113.9 - - [05/Oct/2026:10:00:00 +0200] "GET /api/x?y=1 HTTP/1.1" 502 157 "-" "curl/8.0" 4211 "api@docker" "http://172.18.0.5:8080" 31ms`, time.Now())
|
||||
if !r.dated || r.sev != 3 || !strings.HasPrefix(r.msg, "203.0.113.9 - - [") {
|
||||
t.Errorf("got %+v", r)
|
||||
}
|
||||
for k, want := range map[string]string{"client_ip": "203.0.113.9", "http_method": "GET", "http_path": "/api/x?y=1", "http_status": "502",
|
||||
"user_agent": "curl/8.0", "router": "api@docker", "backend": "http://172.18.0.5:8080", "duration_ms": "31"} {
|
||||
if r.fields[k] != want {
|
||||
t.Errorf("%s = %q, want %q", k, r.fields[k], want)
|
||||
}
|
||||
}
|
||||
if _, ok := r.fields["http_referer"]; ok {
|
||||
t.Error("empty referer kept")
|
||||
}
|
||||
// nginx combined, 404.
|
||||
r = p.parse(`10.0.0.1 - alice [05/Oct/2026:10:00:00 +0000] "POST /login HTTP/2.0" 404 0 "https://example.com/" "Mozilla/5.0"`, time.Now())
|
||||
if r.sev != 4 || r.fields["http_user"] != "alice" || r.fields["http_referer"] != "https://example.com/" {
|
||||
t.Errorf("got %+v", r)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseTraefikJSON(t *testing.T) {
|
||||
p := mustParser(t, fmtTraefik, "")
|
||||
r := p.parse(`{"ClientHost":"203.0.113.9","DownstreamStatus":200,"Duration":12500000,"RequestHost":"app.example.com","RequestMethod":"GET","RequestPath":"/","RequestProtocol":"HTTP/2.0","RouterName":"app@docker","StartUTC":"2026-10-05T10:00:00.1Z","entryPointName":"websecure","level":"info","msg":"","time":"2026-10-05T10:00:00Z","TLSVersion":"1.3"}`, time.Now())
|
||||
if !r.dated || r.sev != 6 || r.msg != "GET app.example.com/ 200 · 12.5 ms · app@docker · 203.0.113.9" {
|
||||
t.Errorf("got %+v", r)
|
||||
}
|
||||
for k, want := range map[string]string{"entrypoint": "websecure", "http_status": "200", "TLSVersion": "1.3", "duration_ms": "12.5"} {
|
||||
if r.fields[k] != want {
|
||||
t.Errorf("%s = %q, want %q", k, r.fields[k], want)
|
||||
}
|
||||
}
|
||||
// Traefik's own logs in the same file: generic JSON.
|
||||
r = p.parse(`{"level":"error","msg":"Provider error","time":"2026-10-05T10:00:00Z"}`, time.Now())
|
||||
if r.sev != 3 || r.msg != "Provider error" {
|
||||
t.Errorf("got %+v", r)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetectFormat(t *testing.T) {
|
||||
tests := map[string][]string{
|
||||
fmtJSON: {`{"level":"info","msg":"a"}`, `{"level":"warn","msg":"b"}`},
|
||||
fmtTraefik: {`{"RequestMethod":"GET","DownstreamStatus":200}`, `{"level":"info","msg":"Configuration loaded"}`},
|
||||
fmtCLF: {`10.0.0.1 - - [05/Oct/2026:10:00:00 +0000] "GET / HTTP/1.1" 200 12 "-" "curl"`},
|
||||
fmtLogfmt: {`time="2026-10-05T10:00:00Z" level=info msg="Starting provider" providerName=docker`},
|
||||
fmtText: {`2026-10-05 10:00:00 INFO started`, `user=bob logged in`},
|
||||
}
|
||||
for want, lines := range tests {
|
||||
if got := detectFormat(lines); got != want {
|
||||
t.Errorf("%q: got %s, want %s", lines, got, want)
|
||||
}
|
||||
}
|
||||
// A few lines of another format do not change the answer (80 %).
|
||||
lines := []string{`{"a":1}`, `{"a":2}`, `{"a":3}`, `{"a":4}`, `not json`}
|
||||
if got := detectFormat(lines); got != fmtJSON {
|
||||
t.Errorf("got %s", got)
|
||||
}
|
||||
lines = append(lines, `still not json`)
|
||||
if got := detectFormat(lines); got != fmtText {
|
||||
t.Errorf("got %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetectFile(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "access.log")
|
||||
appendFile(t, path, `10.0.0.1 - - [05/Oct/2026:10:00:00 +0000] "GET / HTTP/1.1" 200 12 "-" "curl"`+"\n"+`partial`)
|
||||
res := DetectFile(path)
|
||||
if res["format"] != fmtCLF || !strings.HasPrefix(res["sample"].(string), "10.0.0.1 - -") || res["code"] != "" {
|
||||
t.Errorf("got %+v", res)
|
||||
}
|
||||
for p, code := range map[string]string{
|
||||
filepath.Join(dir, "nope.log"): "not_found", "relative.log": "invalid", filepath.Join(dir, "*.log"): "invalid", dir: "read",
|
||||
} {
|
||||
if res := DetectFile(p); res["code"] != code {
|
||||
t.Errorf("%s: got %+v, want %s", p, res, code)
|
||||
}
|
||||
}
|
||||
empty := filepath.Join(dir, "empty.log")
|
||||
appendFile(t, empty, "")
|
||||
if res := DetectFile(empty); res["code"] != "empty" {
|
||||
t.Errorf("got %+v", res)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFileSourceValidation(t *testing.T) {
|
||||
f := NewFiles(t.TempDir(), t.TempDir(), time.Hour, func(*Entry) {})
|
||||
bad := []fileSourceDef{
|
||||
{Name: "", Path: "/logs/a.log"},
|
||||
{Name: "a", Path: "relative.log"},
|
||||
{Name: "a", Path: "/logs/*.log"},
|
||||
{Name: "a", Path: "/logs/a.log", Format: "xml"},
|
||||
{Name: "a", Path: "/logs/a.log", Format: fmtRegex, Regex: "(?P<x"},
|
||||
}
|
||||
for _, d := range bad {
|
||||
if err := f.Configure(filesConfig{Sources: []fileSourceDef{d}}); err == nil {
|
||||
t.Errorf("%+v accepted", d)
|
||||
}
|
||||
}
|
||||
if err := f.Configure(filesConfig{Sources: []fileSourceDef{{Name: " traefik ", Path: "/logs/traefik/access.log"}}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
src := f.Status()["sources"].([]map[string]any)
|
||||
if len(src) != 1 || src[0]["name"] != "traefik" || src[0]["id"] == "" || src[0]["format"] != fmtAuto {
|
||||
t.Errorf("got %+v", src)
|
||||
}
|
||||
}
|
||||
|
||||
type fileSink struct {
|
||||
mu sync.Mutex
|
||||
e []*Entry
|
||||
}
|
||||
|
||||
func (s *fileSink) add(e *Entry) { s.mu.Lock(); s.e = append(s.e, e); s.mu.Unlock() }
|
||||
func (s *fileSink) take() []*Entry {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
e := s.e
|
||||
s.e = nil
|
||||
return e
|
||||
}
|
||||
|
||||
func appendFile(t *testing.T, path, s string) {
|
||||
t.Helper()
|
||||
fh, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o644)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := fh.WriteString(s); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fh.Close()
|
||||
}
|
||||
|
||||
func msgs(es []*Entry) []string {
|
||||
var out []string
|
||||
for _, e := range es {
|
||||
out = append(out, e.Message)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func TestFilesFollow(t *testing.T) {
|
||||
dir, data := t.TempDir(), t.TempDir()
|
||||
now := time.Now()
|
||||
old := now.Add(-3 * time.Hour).UTC().Format(time.RFC3339)
|
||||
recent := now.Add(-10 * time.Minute).UTC().Format(time.RFC3339)
|
||||
app := filepath.Join(dir, "app.log")
|
||||
appendFile(t, app, old+" INFO too old\n"+recent+" ERROR recent\n at line 1\nundated history line\n")
|
||||
|
||||
sink := &fileSink{}
|
||||
f := NewFiles(dir, data, time.Hour, sink.add)
|
||||
if err := f.Configure(filesConfig{Sources: []fileSourceDef{{Name: "myapp", Path: app, Host: "web1", Enabled: true}}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f.poll(now)
|
||||
f.poll(now.Add(3 * time.Second)) // the pending entry waited for continuation lines
|
||||
got := sink.take()
|
||||
if len(got) != 1 || got[0].Message != "ERROR recent\n at line 1" || got[0].SevNum != 3 || got[0].App != "myapp" ||
|
||||
got[0].Host != "web1" || got[0].SourceType != "file" || got[0].Extra["log_file"] != app {
|
||||
t.Fatalf("history: got %q %+v", msgs(got), got)
|
||||
}
|
||||
if st := f.Status()["sources"].([]map[string]any)[0]; st["detected"] != fmtText || st["open"] != true {
|
||||
t.Errorf("status %+v", st)
|
||||
}
|
||||
|
||||
appendFile(t, app, "plain new line\n\tcontinued\n")
|
||||
f.poll(now.Add(4 * time.Second))
|
||||
appendFile(t, app, "after\n")
|
||||
f.poll(now.Add(5 * time.Second))
|
||||
f.poll(now.Add(8 * time.Second))
|
||||
if got = sink.take(); strings.Join(msgs(got), "|") != "plain new line\n\tcontinued|after" {
|
||||
t.Fatalf("new lines: got %q", msgs(got))
|
||||
}
|
||||
|
||||
appendFile(t, app, "before rotation\n")
|
||||
if err := os.Rename(app, app+".1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
appendFile(t, app, "after rotation\n")
|
||||
f.poll(now.Add(9 * time.Second))
|
||||
f.poll(now.Add(12 * time.Second))
|
||||
if got = sink.take(); strings.Join(msgs(got), "|") != "before rotation|after rotation" {
|
||||
t.Fatalf("rotation: got %q", msgs(got))
|
||||
}
|
||||
|
||||
// Restart: resumes where it stopped, nothing sent twice.
|
||||
f.checkpointAll()
|
||||
f.dirty = true
|
||||
f.saveState()
|
||||
appendFile(t, app, "while stopped\n")
|
||||
f2 := NewFiles(dir, data, time.Hour, sink.add)
|
||||
f2.poll(now.Add(13 * time.Second))
|
||||
f2.poll(now.Add(16 * time.Second))
|
||||
if got = sink.take(); strings.Join(msgs(got), "|") != "while stopped" {
|
||||
t.Fatalf("restart: got %q", msgs(got))
|
||||
}
|
||||
|
||||
// Truncated in place (copytruncate).
|
||||
os.Truncate(app, 0)
|
||||
appendFile(t, app, "x\n")
|
||||
f2.poll(now.Add(17 * time.Second))
|
||||
f2.poll(now.Add(20 * time.Second))
|
||||
if got = sink.take(); strings.Join(msgs(got), "|") != "x" {
|
||||
t.Fatalf("truncate: got %q", msgs(got))
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilesAutoAndErrors(t *testing.T) {
|
||||
dir, data := t.TempDir(), t.TempDir()
|
||||
path := filepath.Join(dir, "access.json")
|
||||
sink := &fileSink{}
|
||||
f := NewFiles(dir, data, 0, sink.add) // FILES_BACKFILL=0: only new lines
|
||||
cfg := filesConfig{Sources: []fileSourceDef{
|
||||
{ID: "a1", Name: "api", Path: path, Enabled: true}, // auto
|
||||
{ID: "b2", Name: "missing", Path: filepath.Join(dir, "nope.log"), Format: fmtText, Enabled: true},
|
||||
}}
|
||||
if err := f.Configure(cfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
now := time.Now()
|
||||
appendFile(t, path, "")
|
||||
f.poll(now) // empty file: format not known yet
|
||||
appendFile(t, path, `{"msg":"one","level":"error"}`+"\n"+`{"msg":"two"}`+"\n"+`{"msg":"partial`)
|
||||
f.poll(now.Add(time.Second))
|
||||
got := sink.take()
|
||||
if strings.Join(msgs(got), "|") != "one|two" || got[0].SevNum != 3 {
|
||||
t.Fatalf("got %q", msgs(got))
|
||||
}
|
||||
src := f.Status()["sources"].([]map[string]any)
|
||||
if src[0]["detected"] != fmtJSON || src[0]["open"] != true || src[1]["code"] != "not_found" {
|
||||
t.Errorf("status %+v", src)
|
||||
}
|
||||
|
||||
// Forced format: the JSON lines are read as text.
|
||||
cfg.Sources[0].Format = fmtText
|
||||
f.Configure(cfg)
|
||||
appendFile(t, path, `"}`+"\n")
|
||||
f.poll(now.Add(2 * time.Second))
|
||||
f.poll(now.Add(5 * time.Second))
|
||||
if got = sink.take(); strings.Join(msgs(got), "|") != `{"msg":"partial"}` {
|
||||
t.Fatalf("forced: got %q", msgs(got))
|
||||
}
|
||||
|
||||
// Turned off then on: starts over (only new lines here).
|
||||
cfg.Sources[0].Enabled = false
|
||||
f.Configure(cfg)
|
||||
f.poll(now.Add(6 * time.Second))
|
||||
appendFile(t, path, "while off\n")
|
||||
cfg.Sources[0].Enabled = true
|
||||
f.Configure(cfg)
|
||||
f.poll(now.Add(7 * time.Second))
|
||||
appendFile(t, path, "three\n")
|
||||
f.poll(now.Add(8 * time.Second))
|
||||
f.poll(now.Add(11 * time.Second))
|
||||
if got = sink.take(); strings.Join(msgs(got), "|") != "three" {
|
||||
t.Fatalf("re-enabled: got %q", msgs(got))
|
||||
}
|
||||
}
|
||||
@@ -43,6 +43,7 @@ type config struct {
|
||||
auditLog string
|
||||
auditHost string
|
||||
auditFill time.Duration
|
||||
filesFill time.Duration
|
||||
batchSize int
|
||||
queueSize int
|
||||
flushEvery time.Duration
|
||||
@@ -125,6 +126,7 @@ func main() {
|
||||
auditLog: getenv("AUDIT_LOG", "/var/log/audit/audit.log"),
|
||||
auditHost: os.Getenv("AUDIT_HOST"),
|
||||
auditFill: getenvDuration("AUDIT_BACKFILL", time.Hour),
|
||||
filesFill: getenvDuration("FILES_BACKFILL", time.Hour),
|
||||
batchSize: getenvInt("BATCH_SIZE", 1000),
|
||||
queueSize: getenvInt("QUEUE_SIZE", 100000),
|
||||
flushEvery: time.Duration(getenvInt("FLUSH_MS", 1000)) * time.Millisecond,
|
||||
@@ -218,6 +220,10 @@ func main() {
|
||||
// Linux audit log of the host (auditd), off until enabled in Settings > Sources.
|
||||
api.audit = NewAudit(cfg.auditLog, cfg.hostRoot, cfg.auditHost, cfg.dataDir, cfg.auditFill, sink)
|
||||
go api.audit.Run(ctx)
|
||||
// Log files of other programs (Traefik, nginx…) mounted in the container,
|
||||
// declared in Settings > Sources.
|
||||
api.files = NewFiles(cfg.hostRoot, cfg.dataDir, cfg.filesFill, sink)
|
||||
go api.files.Run(ctx)
|
||||
api.selfLog = selfLog
|
||||
api.Routes(mux)
|
||||
mux.Handle("GET /", http.FileServer(http.FS(static)))
|
||||
|
||||
@@ -119,10 +119,10 @@ func (f Filter) filterExpr() string {
|
||||
parts = append(parts, "app:="+strconv.Quote(f.App))
|
||||
}
|
||||
switch f.Source {
|
||||
case "docker", "host", "kernel", "audit", "logstream":
|
||||
case "docker", "host", "kernel", "audit", "logstream", "file":
|
||||
parts = append(parts, `source_type:=`+strconv.Quote(f.Source))
|
||||
case "syslog": // also matches logs stored before source_type existed
|
||||
parts = append(parts, `!(source_type:in("docker","host","kernel","audit","logstream"))`)
|
||||
parts = append(parts, `!(source_type:in("docker","host","kernel","audit","logstream","file"))`)
|
||||
}
|
||||
if f.Severity >= 0 && f.Severity < 7 {
|
||||
names := make([]string, 0, 8)
|
||||
|
||||
@@ -36,7 +36,7 @@ type Entry struct {
|
||||
Proto string // udp or tcp
|
||||
HostIP string // original host value when it was an IP resolved through DNS
|
||||
|
||||
SourceType string // "syslog", "docker", "host", "kernel", "audit" or "logstream"
|
||||
SourceType string // "syslog", "docker", "host", "kernel", "audit", "file" or "logstream"
|
||||
Extra map[string]string // additional fields (docker: container, image, …)
|
||||
|
||||
// Not stored. Wait: the producer can be slowed down when the queue is full
|
||||
|
||||
+227
-9
@@ -75,9 +75,37 @@ const I18N = {
|
||||
msgTimeTitle: 'Timestamp from the message',
|
||||
fHostName: 'host name (DNS)', fHostIp: 'host IP',
|
||||
clickHost: 'Click to filter on this host', clickApp: 'Click to filter on this app',
|
||||
sourceAria: 'Source', srcAll: 'All sources', tabSources: 'Sources',
|
||||
sourceAria: 'Source', srcAll: 'All sources', tabSources: 'Log sources',
|
||||
srcHost: 'Host system',
|
||||
srcKernel: 'Kernel (dmesg)',
|
||||
srcFile: 'Files',
|
||||
filesTitle: 'Log files',
|
||||
addFile: '+ Add a file',
|
||||
filesNone: 'No file yet: add one to follow the logs of Traefik, nginx or any other program.',
|
||||
fileName: 'Name (app)',
|
||||
filePath: '/logs/traefik/access.log',
|
||||
fileHost: 'Host',
|
||||
fileRegex: '(?P<time>\\S+ \\S+) \\[(?P<level>\\w+)\\] (?P<msg>.*)',
|
||||
fileEnabledAria: 'Follow this file',
|
||||
fmt_auto: 'Automatic',
|
||||
fmt_text: 'Plain text',
|
||||
fmt_json: 'JSON lines',
|
||||
fmt_logfmt: 'logfmt (key=value)',
|
||||
fmt_clf: 'Access log (CLF)',
|
||||
fmt_traefik: 'Traefik access (JSON)',
|
||||
fmt_regex: 'Regular expression',
|
||||
fileOff: 'Off.',
|
||||
fileDraft: 'Fill in the name and the path to save this file.',
|
||||
fileOk: (r) => `Followed: ${r} entries since startup.`,
|
||||
fileDetected: (f) => ` Format detected: ${f}.`,
|
||||
probeFound: (f) => `File found, format detected: ${f}.`,
|
||||
probeSample: 'Last line: ',
|
||||
file_empty: 'The file is empty for now: its format will be detected from its first lines.',
|
||||
file_invalid: '',
|
||||
file_not_found: 'No file at this path: mount it in the container (see docker-compose.yml) and check the path.',
|
||||
file_permission: 'Permission denied: the file must be readable by UID 10001 or the adm group (see the README). ',
|
||||
file_read: 'Cannot read the file: ',
|
||||
filesHelp: 'Follows the log file of another program, mounted read-only in the container (e.g. /var/log/traefik:/logs/traefik:ro, then /logs/traefik/access.log here). Once the path is filled in, the file is checked and its format detected from its last lines (JSON lines, logfmt, access logs of nginx, Apache or Traefik, else plain text); choose a format to force it, or a regular expression whose named groups time, level, msg, host and app fill these fields, the other groups giving extra fields. Each line gives an entry (badge FILE, app = the name); indented lines are added to the previous entry; for access logs 5xx are errors and 4xx warnings. When turned on, the last hour of dated lines is read first (FILES_BACKFILL); rotations are followed and reading resumes where it stopped.',
|
||||
auditTitle: 'Linux audit (auditd)',
|
||||
auditEnabled: 'Collect the audit events of this machine',
|
||||
auditPath: 'File',
|
||||
@@ -142,7 +170,7 @@ const I18N = {
|
||||
exportNote: (n) => `Up to ${n} logs matching the current filters, newest first. Dates use the time zone chosen in Settings.`,
|
||||
exporting: 'Exporting…', exportDone: (f) => `Downloaded: ${f}`, exportError: 'Export failed: ',
|
||||
err_export_pipes: 'Export does not support LogsQL pipes (| …): keep only the filter part',
|
||||
tabLocale: 'Localization', tabFilters: 'Filters', tabInterface: 'Interface', tabData: 'Data',
|
||||
tabLocale: 'Localization', tabFilters: 'Patterns', tabInterface: 'Appearance', tabData: 'Database',
|
||||
themeTitle: 'Theme', themeSystem: 'System', themeLight: 'Light', themeDark: 'Dark',
|
||||
themeHelp: 'System follows the light/dark preference of your computer or phone.',
|
||||
logDisplay: 'Log display', fontSize: 'Font size',
|
||||
@@ -196,7 +224,7 @@ const I18N = {
|
||||
hCappedTitle: 'The chosen division would exceed 300 intervals over this range: it has been enlarged.',
|
||||
hUnzoom: '× Back to stream', unitDay: 'd',
|
||||
toTop: 'Back to top',
|
||||
colRcv: 'Received', colMt: 'Message time', colSev: 'Severity', colHost: 'Host', colApp: 'App', colCodes: 'Filters', colMsg: 'Message',
|
||||
colRcv: 'Received', colMt: 'Message time', colSev: 'Severity', colHost: 'Host', colApp: 'App', colCodes: 'Pattern', colMsg: 'Message',
|
||||
codesTitle: 'Codes of the color tags found in the message',
|
||||
tagCodeTitle: 'Code shown on the log lines this tag matches',
|
||||
colGrip: 'Drag to resize, double-click for the automatic width',
|
||||
@@ -275,9 +303,37 @@ const I18N = {
|
||||
msgTimeTitle: 'Horodatage contenu dans le message',
|
||||
fHostName: 'nom d\'hôte (DNS)', fHostIp: 'IP de l\'hôte',
|
||||
clickHost: 'Cliquer pour filtrer sur cet hôte', clickApp: 'Cliquer pour filtrer sur cette appli',
|
||||
sourceAria: 'Source', srcAll: 'Toutes les sources', tabSources: 'Sources',
|
||||
sourceAria: 'Source', srcAll: 'Toutes les sources', tabSources: 'Sources de logs',
|
||||
srcHost: 'Système hôte',
|
||||
srcKernel: 'Noyau (dmesg)',
|
||||
srcFile: 'Fichiers',
|
||||
filesTitle: 'Fichiers de logs',
|
||||
addFile: '+ Ajouter un fichier',
|
||||
filesNone: 'Aucun fichier pour l\'instant : ajoutez-en un pour suivre les logs de Traefik, nginx ou de tout autre programme.',
|
||||
fileName: 'Nom (app)',
|
||||
filePath: '/logs/traefik/access.log',
|
||||
fileHost: 'Hôte',
|
||||
fileRegex: '(?P<time>\\S+ \\S+) \\[(?P<level>\\w+)\\] (?P<msg>.*)',
|
||||
fileEnabledAria: 'Suivre ce fichier',
|
||||
fmt_auto: 'Automatique',
|
||||
fmt_text: 'Texte brut',
|
||||
fmt_json: 'JSON lines',
|
||||
fmt_logfmt: 'logfmt (clé=valeur)',
|
||||
fmt_clf: 'Journal d\'accès (CLF)',
|
||||
fmt_traefik: 'Accès Traefik (JSON)',
|
||||
fmt_regex: 'Expression régulière',
|
||||
fileOff: 'Désactivé.',
|
||||
fileDraft: 'Renseignez le nom et le chemin pour enregistrer ce fichier.',
|
||||
fileOk: (r) => `Suivi : ${r} entrées depuis le démarrage.`,
|
||||
fileDetected: (f) => ` Format détecté : ${f}.`,
|
||||
probeFound: (f) => `Fichier trouvé, format détecté : ${f}.`,
|
||||
probeSample: 'Dernière ligne : ',
|
||||
file_empty: 'Le fichier est vide pour l\'instant : son format sera détecté à partir de ses premières lignes.',
|
||||
file_invalid: '',
|
||||
file_not_found: 'Aucun fichier à ce chemin : montez-le dans le conteneur (voir docker-compose.yml) et vérifiez le chemin.',
|
||||
file_permission: 'Accès refusé : le fichier doit être lisible par l\'UID 10001 ou le groupe adm (voir le README). ',
|
||||
file_read: 'Lecture du fichier impossible : ',
|
||||
filesHelp: 'Suit le fichier de logs d\'un autre programme, monté en lecture seule dans le conteneur (par ex. /var/log/traefik:/logs/traefik:ro, puis /logs/traefik/access.log ici). Une fois le chemin saisi, le fichier est vérifié et son format détecté à partir de ses dernières lignes (JSON lines, logfmt, journaux d\'accès de nginx, Apache ou Traefik, sinon texte brut) ; choisissez un format pour le forcer, ou une expression régulière dont les groupes nommés time, level, msg, host et app remplissent ces champs, les autres groupes donnant des champs supplémentaires. Chaque ligne donne une entrée (badge FILE, app = le nom) ; les lignes indentées sont rattachées à l\'entrée précédente ; pour les journaux d\'accès, 5xx en erreur et 4xx en warning. À l\'activation, la dernière heure de lignes datées est lue d\'abord (FILES_BACKFILL) ; les rotations sont suivies et la lecture reprend où elle s\'était arrêtée.',
|
||||
auditTitle: 'Audit Linux (auditd)',
|
||||
auditEnabled: 'Collecter les événements d\'audit de cette machine',
|
||||
auditPath: 'Fichier',
|
||||
@@ -342,7 +398,7 @@ const I18N = {
|
||||
exportNote: (n) => `Jusqu'à ${n} logs correspondant aux filtres, du plus récent au plus ancien. Dates dans le fuseau choisi dans Paramètres.`,
|
||||
exporting: 'Export…', exportDone: (f) => `Téléchargé : ${f}`, exportError: 'Échec de l\'export : ',
|
||||
err_export_pipes: 'L\'export ne gère pas les pipes LogsQL (| …) : gardez seulement la partie filtre',
|
||||
tabLocale: 'Localisation', tabFilters: 'Filtres', tabInterface: 'Interface', tabData: 'Données',
|
||||
tabLocale: 'Localisation et langue', tabFilters: 'Motifs de recherche', tabInterface: 'Affichage', tabData: 'Base de données',
|
||||
themeTitle: 'Thème', themeSystem: 'Système', themeLight: 'Clair', themeDark: 'Sombre',
|
||||
themeHelp: 'Système suit la préférence clair/sombre de votre ordinateur ou de votre téléphone.',
|
||||
logDisplay: 'Affichage des logs', fontSize: 'Taille du texte',
|
||||
@@ -396,7 +452,7 @@ const I18N = {
|
||||
hCappedTitle: 'La division choisie dépasserait 300 intervalles sur cette plage : elle a été élargie.',
|
||||
hUnzoom: '× Revenir au flux', unitDay: 'j',
|
||||
toTop: 'Revenir en haut',
|
||||
colRcv: 'Réception', colMt: 'Heure message', colSev: 'Sévérité', colHost: 'Hôte', colApp: 'App', colCodes: 'Filtres', colMsg: 'Message',
|
||||
colRcv: 'Réception', colMt: 'Heure message', colSev: 'Sévérité', colHost: 'Hôte', colApp: 'App', colCodes: 'Motif de recherche', colMsg: 'Message',
|
||||
codesTitle: 'Codes des tags de couleur trouvés dans le message',
|
||||
tagCodeTitle: 'Code affiché sur les lignes de log où ce tag est trouvé',
|
||||
colGrip: 'Glisser pour redimensionner, double-clic pour la largeur automatique',
|
||||
@@ -598,6 +654,7 @@ function setLang(next) {
|
||||
renderDmesg();
|
||||
renderAudit();
|
||||
renderSelfLog();
|
||||
renderFiles(true);
|
||||
renderDocker();
|
||||
}
|
||||
|
||||
@@ -907,7 +964,7 @@ function fmtTime(s) {
|
||||
|
||||
// Sources with their own look in the list: a colored edge and a badge
|
||||
// before the app name (the severity badge stays the same).
|
||||
const SRC_BADGE = { audit: 'AUDIT', logstream: 'LS' };
|
||||
const SRC_BADGE = { audit: 'AUDIT', logstream: 'LS', file: 'FILE' };
|
||||
|
||||
function rowHTML(r, isNew) {
|
||||
const sev = r.severity || '';
|
||||
@@ -1025,7 +1082,7 @@ function updateCount(tookMs) {
|
||||
}
|
||||
|
||||
function detailsHTML(r) {
|
||||
const order = ['received', 'msg_time', '_time', 'host', 'host_name', 'host_ip', 'source_type', 'audit_type', 'audit_serial', 'auid', 'user', 'exe', 'res', 'key', 'container', 'image', 'compose_project', 'compose_service', 'stream', 'unit', 'log_file', 'subsystem', 'device', 'app', 'procid', 'severity', 'facility', 'source', 'proto', 'sd', '_msg'];
|
||||
const order = ['received', 'msg_time', '_time', 'host', 'host_name', 'host_ip', 'source_type', 'audit_type', 'audit_serial', 'auid', 'user', 'exe', 'res', 'key', 'container', 'image', 'compose_project', 'compose_service', 'stream', 'unit', 'log_file', 'client_ip', 'http_method', 'http_host', 'http_path', 'http_status', 'duration_ms', 'router', 'subsystem', 'device', 'app', 'procid', 'severity', 'facility', 'source', 'proto', 'sd', '_msg'];
|
||||
const hidden = new Set(['_stream_id', '_stream', 'sevnum', 'audit_raw']); // audit_raw: shown under the message
|
||||
if (r.msg_time) hidden.add('_time'); // same as the reception time
|
||||
const keys = order.filter((k) => r[k] != null && r[k] !== '' && !hidden.has(k))
|
||||
@@ -2182,6 +2239,152 @@ async function configureSelfLog(change) {
|
||||
$('#selfEnabled').addEventListener('change', (ev) => configureSelfLog({ enabled: ev.target.checked }));
|
||||
$('#selfLevel').addEventListener('change', (ev) => configureSelfLog({ min_level: ev.target.value }));
|
||||
|
||||
/* ================= Log files ================= */
|
||||
|
||||
const FILE_FORMATS = ['auto', 'text', 'json', 'logfmt', 'clf', 'traefik', 'regex'];
|
||||
let filesState = null; // {sources, defaultHost} from the server
|
||||
let fileDrafts = []; // rows added here, saved once they have a name and a path
|
||||
let fileSeq = 0;
|
||||
const fileProbes = new Map(); // path -> result of /api/files/detect (format found, last line)
|
||||
|
||||
async function loadFiles() {
|
||||
try { filesState = await api('/api/files'); } catch { filesState = null; }
|
||||
renderFiles();
|
||||
}
|
||||
|
||||
// Status line of a row: the check of the path just typed, else the state
|
||||
// of the file once saved.
|
||||
function fileStatus(s) {
|
||||
const pr = fileProbes.get(s.path);
|
||||
if (pr && (s.draft || (s.enabled && !s.open && !s.code))) {
|
||||
if (pr.code) return [t('file_' + pr.code) + (pr.code === 'not_found' || pr.code === 'empty' ? '' : (pr.code === 'invalid' ? '' : ' ') + (pr.error || '')), pr.code === 'empty' ? 'muted' : 'bad'];
|
||||
return [t('probeFound', t('fmt_' + pr.format)), 'good'];
|
||||
}
|
||||
if (s.draft) return [t('fileDraft'), 'muted'];
|
||||
if (!s.enabled) return [t('fileOff'), 'muted'];
|
||||
if (s.code) return [t('file_' + s.code) + (s.code === 'not_found' ? '' : s.error || ''), 'bad'];
|
||||
if (s.open) return [t('fileOk', fmtNum(s.read)) + (s.format === 'auto' && s.detected ? t('fileDetected', t('fmt_' + s.detected)) : ''), 'good'];
|
||||
return [t('hostWaiting'), 'muted'];
|
||||
}
|
||||
|
||||
function fileSample(s) {
|
||||
const pr = fileProbes.get(s.path);
|
||||
return pr && pr.sample ? t('probeSample') + pr.sample : '';
|
||||
}
|
||||
|
||||
// Checks the path typed in a row: found or not, detected format, last line.
|
||||
async function probeFile(path) {
|
||||
if (!path) return;
|
||||
try {
|
||||
fileProbes.set(path, await api('/api/files/detect', { method: 'POST', body: { path } }));
|
||||
} catch { fileProbes.delete(path); }
|
||||
renderFiles();
|
||||
}
|
||||
|
||||
function fileRowHTML(s) {
|
||||
const key = s.id || s.draft;
|
||||
const [text, cls] = fileStatus(s);
|
||||
return `<div class="file-row${s.enabled ? '' : ' off'}" data-key="${esc(key)}">
|
||||
<input type="checkbox" class="switch" data-f="enabled"${s.enabled ? ' checked' : ''} aria-label="${esc(t('fileEnabledAria'))}">
|
||||
<input type="text" data-f="name" value="${esc(s.name)}" placeholder="${esc(t('fileName'))}" aria-label="${esc(t('fileName'))}" spellcheck="false" maxlength="64">
|
||||
<input type="text" data-f="path" class="mono" value="${esc(s.path)}" placeholder="${esc(t('filePath'))}" aria-label="${esc(t('filePath'))}" spellcheck="false">
|
||||
<select class="field" data-f="format">${FILE_FORMATS.map((f) => `<option value="${f}"${s.format === f ? ' selected' : ''}>${esc(t('fmt_' + f))}</option>`).join('')}</select>
|
||||
<input type="text" data-f="host" value="${esc(s.host)}" placeholder="${esc(filesState?.defaultHost || t('fileHost'))}" aria-label="${esc(t('fileHost'))}" title="${esc(t('fileHost'))}" spellcheck="false">
|
||||
<button class="icon-btn danger" type="button" data-del title="${esc(t('del'))}" aria-label="${esc(t('del'))}">
|
||||
<svg viewBox="0 0 24 24"><path d="M4 7h16M10 11v6M14 11v6M6 7l1 13h10l1-13M9 7V4h6v3"/></svg>
|
||||
</button>
|
||||
<input type="text" data-f="regex" class="mono fr-regex" value="${esc(s.regex)}" placeholder="${esc(t('fileRegex'))}" aria-label="${esc(t('fmt_regex'))}" spellcheck="false"${s.format === 'regex' ? '' : ' hidden'}>
|
||||
<p class="fr-status docker-status ${cls}">${esc(text)}</p>
|
||||
<p class="fr-sample"${fileSample(s) ? '' : ' hidden'}>${esc(fileSample(s))}</p>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
// full: rebuild the rows even while one is being edited (language change).
|
||||
function renderFiles(full) {
|
||||
const list = $('#fileList');
|
||||
if (!filesState) { list.innerHTML = ''; return; }
|
||||
const rows = filesState.sources.concat(fileDrafts);
|
||||
const keys = rows.map((s) => s.id || s.draft).join(',');
|
||||
if (!full && list.dataset.keys === keys && list.contains(document.activeElement)) {
|
||||
// Being edited: only refresh the status lines.
|
||||
for (const s of rows) {
|
||||
const row = list.querySelector(`.file-row[data-key="${CSS.escape(s.id || s.draft)}"]`);
|
||||
if (!row) continue;
|
||||
const [text, cls] = fileStatus(s);
|
||||
const p = row.querySelector('.fr-status');
|
||||
p.textContent = text;
|
||||
p.className = 'fr-status docker-status ' + cls;
|
||||
const sample = fileSample(s);
|
||||
row.querySelector('.fr-sample').textContent = sample;
|
||||
row.querySelector('.fr-sample').hidden = !sample;
|
||||
}
|
||||
return;
|
||||
}
|
||||
list.dataset.keys = keys;
|
||||
list.innerHTML = rows.map(fileRowHTML).join('') || `<p class="muted small">${esc(t('filesNone'))}</p>`;
|
||||
}
|
||||
|
||||
function fileRowOf(key) {
|
||||
return filesState?.sources.find((s) => s.id === key) || fileDrafts.find((s) => s.draft === key);
|
||||
}
|
||||
|
||||
async function saveFiles() {
|
||||
const ready = fileDrafts.filter((d) => d.name.trim() && d.path.trim());
|
||||
|
||||
const sources = filesState.sources.concat(ready).map(({ id, name, path, format, regex, host, enabled }) => ({ id: id || '', name, path, format, regex, host, enabled }));
|
||||
try {
|
||||
filesState = await api('/api/files', { method: 'PUT', body: { sources } });
|
||||
fileDrafts = fileDrafts.filter((d) => !ready.includes(d));
|
||||
|
||||
} catch (e) {
|
||||
toast(e.message);
|
||||
await loadFiles();
|
||||
return;
|
||||
}
|
||||
renderFiles();
|
||||
setTimeout(loadFiles, 1500); // the files are read in the background
|
||||
}
|
||||
|
||||
$('#fileList').addEventListener('change', (ev) => {
|
||||
const row = ev.target.closest('.file-row');
|
||||
const s = row && fileRowOf(row.dataset.key);
|
||||
const f = ev.target.dataset.f;
|
||||
if (!s || !f) return;
|
||||
s[f] = f === 'enabled' ? ev.target.checked : ev.target.value.trim();
|
||||
if (f === 'path') {
|
||||
// A name by default: the file name without its extension.
|
||||
if (!s.name && s.path) {
|
||||
s.name = s.path.split('/').pop().replace(/\.(log|txt|json)$/i, '');
|
||||
row.querySelector('[data-f="name"]').value = s.name;
|
||||
}
|
||||
probeFile(s.path);
|
||||
}
|
||||
if (f === 'format') row.querySelector('.fr-regex').hidden = s.format !== 'regex';
|
||||
if (f === 'format' && s.format === 'regex' && !s.regex) { row.querySelector('.fr-regex').focus(); return; }
|
||||
if (s.draft && !(s.name && s.path)) return;
|
||||
saveFiles();
|
||||
});
|
||||
|
||||
$('#fileList').addEventListener('click', (ev) => {
|
||||
const btn = ev.target.closest('[data-del]');
|
||||
if (!btn) return;
|
||||
const key = btn.closest('.file-row').dataset.key;
|
||||
if (fileDrafts.some((d) => d.draft === key)) {
|
||||
fileDrafts = fileDrafts.filter((d) => d.draft !== key);
|
||||
renderFiles(true);
|
||||
return;
|
||||
}
|
||||
filesState.sources = filesState.sources.filter((s) => s.id !== key);
|
||||
saveFiles();
|
||||
});
|
||||
|
||||
$('#addFile').addEventListener('click', () => {
|
||||
if (!filesState) return;
|
||||
fileDrafts.push({ draft: 'new' + (++fileSeq), name: '', path: '', format: 'auto', regex: '', host: '', enabled: true });
|
||||
renderFiles(true);
|
||||
$('#fileList').lastElementChild.querySelector('[data-f="path"]').focus();
|
||||
});
|
||||
|
||||
/* ================= Docker source ================= */
|
||||
|
||||
let dockerState = null;
|
||||
@@ -2301,6 +2504,7 @@ setInterval(() => {
|
||||
loadDmesg();
|
||||
loadAudit();
|
||||
loadSelfLog();
|
||||
loadFiles();
|
||||
}
|
||||
}, 4000);
|
||||
|
||||
@@ -2476,7 +2680,7 @@ function renderTagList() {
|
||||
filterTagList();
|
||||
}
|
||||
|
||||
// Settings > Filters: shows only the tags whose keyword, label or code contains the filter.
|
||||
// Settings > Pattern: shows only the tags whose keyword, label or code contains the filter.
|
||||
function filterTagList() {
|
||||
const q = $('#tagFilter').value.trim().toLowerCase();
|
||||
let shown = 0;
|
||||
@@ -2597,6 +2801,7 @@ $('#settingsBtn').addEventListener('click', () => {
|
||||
loadDmesg();
|
||||
loadAudit();
|
||||
loadSelfLog();
|
||||
loadFiles();
|
||||
loadDocker();
|
||||
renderDbStats();
|
||||
loadDbStats();
|
||||
@@ -2638,6 +2843,19 @@ renderPeriod();
|
||||
fetch('/auth/me').then((res) => (res.ok ? res.json() : null)).then((me) => {
|
||||
if (!me) return;
|
||||
if (me.mode === 'none' && store.get('authWarnHidden') !== '1') $('#authWarn').hidden = false;
|
||||
const who = $('#whoami');
|
||||
if (me.mode === 'none') {
|
||||
who.classList.add('none');
|
||||
who.querySelector('.who-name').textContent = '(no auth)';
|
||||
who.hidden = false;
|
||||
} else if (me.user) {
|
||||
who.querySelector('.who-name').textContent = me.user;
|
||||
who.title = me.user;
|
||||
const badge = who.querySelector('.who-badge');
|
||||
badge.textContent = me.mode;
|
||||
badge.classList.add(me.mode);
|
||||
who.hidden = false;
|
||||
}
|
||||
if (me.role === 'viewer') {
|
||||
document.body.classList.add('read-only');
|
||||
for (const p of document.querySelectorAll('[data-panel="filters"], [data-panel="sources"], [data-panel="data"]')) {
|
||||
|
||||
+17
-6
@@ -41,6 +41,7 @@
|
||||
<svg class="sun" viewBox="0 0 24 24"><circle cx="12" cy="12" r="4"/><path d="M12 2v2M12 20v2M4.9 4.9l1.4 1.4M17.7 17.7l1.4 1.4M2 12h2M20 12h2M4.9 19.1l1.4-1.4M17.7 6.3l1.4-1.4"/></svg>
|
||||
<svg class="moon" viewBox="0 0 24 24"><path d="M21 12.8A9 9 0 1 1 11.2 3a7 7 0 0 0 9.8 9.8z"/></svg>
|
||||
</button>
|
||||
<span id="whoami" class="whoami" hidden><span class="who-name"></span><span class="who-badge"></span></span>
|
||||
<a id="logoutBtn" class="icon-btn" href="/auth/logout" hidden data-i18n-title="logout" data-i18n-aria="logout">
|
||||
<!-- Log out icon (Lucide "log-out", ISC license) -->
|
||||
<svg viewBox="0 0 24 24"><path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"/><path d="m16 17 5-5-5-5"/><path d="M21 12H9"/></svg>
|
||||
@@ -104,6 +105,7 @@
|
||||
<option value="host" data-i18n="srcHost">Host system</option>
|
||||
<option value="kernel" data-i18n="srcKernel">Kernel (dmesg)</option>
|
||||
<option value="audit">Audit</option>
|
||||
<option value="file" data-i18n="srcFile">Files</option>
|
||||
<option value="logstream">LogStream</option>
|
||||
</select>
|
||||
<span class="spacer"></span>
|
||||
@@ -143,7 +145,7 @@
|
||||
<span data-col="sev"><span class="lbl" data-i18n="colSev">Severity</span><i class="grip" data-i18n-title="colGrip"></i></span>
|
||||
<span data-col="host"><span class="lbl" data-i18n="colHost">Host</span><i class="grip" data-i18n-title="colGrip"></i></span>
|
||||
<span data-col="app"><span class="lbl" data-i18n="colApp">App</span><i class="grip" data-i18n-title="colGrip"></i></span>
|
||||
<span data-col="codes"><span class="lbl" data-i18n="colCodes" data-i18n-title="codesTitle">Filters</span></span>
|
||||
<span data-col="codes"><span class="lbl" data-i18n="colCodes" data-i18n-title="codesTitle">Pattern</span></span>
|
||||
<span data-col="msg"><span class="lbl" data-i18n="colMsg">Message</span></span>
|
||||
</div>
|
||||
<main id="list" class="list"></main>
|
||||
@@ -179,19 +181,19 @@
|
||||
</button>
|
||||
<button type="button" role="tab" data-tab="filters">
|
||||
<svg viewBox="0 0 24 24"><path d="M3 12V4a1 1 0 0 1 1-1h8l9 9-9 9z"/><circle cx="8" cy="8" r="1.5"/></svg>
|
||||
<span data-i18n="tabFilters">Filters</span>
|
||||
<span data-i18n="tabFilters">Patterns</span>
|
||||
</button>
|
||||
<button type="button" role="tab" data-tab="sources">
|
||||
<svg viewBox="0 0 24 24"><path d="M21 8 12 3 3 8v8l9 5 9-5z"/><path d="m3 8 9 5 9-5M12 13v8"/></svg>
|
||||
<span data-i18n="tabSources">Sources</span>
|
||||
<span data-i18n="tabSources">Log sources</span>
|
||||
</button>
|
||||
<button type="button" role="tab" data-tab="interface">
|
||||
<svg viewBox="0 0 24 24"><rect x="3" y="4" width="18" height="12" rx="2"/><path d="M8 20h8M12 16v4"/></svg>
|
||||
<span data-i18n="tabInterface">Interface</span>
|
||||
<span data-i18n="tabInterface">Appearance</span>
|
||||
</button>
|
||||
<button type="button" role="tab" data-tab="data">
|
||||
<svg viewBox="0 0 24 24"><ellipse cx="12" cy="5" rx="8" ry="3"/><path d="M4 5v14c0 1.7 3.6 3 8 3s8-1.3 8-3V5M4 12c0 1.7 3.6 3 8 3s8-1.3 8-3"/></svg>
|
||||
<span data-i18n="tabData">Data</span>
|
||||
<span data-i18n="tabData">Database</span>
|
||||
</button>
|
||||
</nav>
|
||||
|
||||
@@ -218,7 +220,7 @@
|
||||
</section>
|
||||
</div>
|
||||
|
||||
<!-- Filters -->
|
||||
<!-- Pattern (color tags; the "filters" tab id is kept for saved settings) -->
|
||||
<div class="set-panel wide" role="tabpanel" data-panel="filters" hidden>
|
||||
<section class="set-section tags-section">
|
||||
<div class="sec-head">
|
||||
@@ -311,6 +313,15 @@
|
||||
<p class="muted small src-help" data-i18n="selfHelp"></p>
|
||||
</section>
|
||||
|
||||
<section class="set-section span">
|
||||
<h3 data-i18n="filesTitle">Log files</h3>
|
||||
<div id="fileList" class="file-list"></div>
|
||||
<div class="file-toolbar">
|
||||
<button id="addFile" class="btn primary" type="button" data-i18n="addFile">+ Add a file</button>
|
||||
</div>
|
||||
<p class="muted small src-help" data-i18n="filesHelp"></p>
|
||||
</section>
|
||||
|
||||
<section class="set-section span">
|
||||
<h3 data-i18n="dockerTitle">Docker containers</h3>
|
||||
<p id="dockerStatus" class="docker-status"></p>
|
||||
|
||||
+50
-5
@@ -24,6 +24,7 @@
|
||||
--chip-l: 38%; /* lightness of project-colored text (labels, Docker apps) */
|
||||
--src-audit: #7c4dbd; /* audit events (auditd) */
|
||||
--src-ls: #1f9aa3; /* LogStream internal logs */
|
||||
--src-file: #5f8a1f; /* log files (Settings > Sources > Log files) */
|
||||
--src-tint: 6%; /* background tint of their rows */
|
||||
|
||||
--sev-crit: #be123c;
|
||||
@@ -75,6 +76,7 @@
|
||||
--chip-l: 70%;
|
||||
--src-audit: #a983e6;
|
||||
--src-ls: #3cc3cc;
|
||||
--src-file: #9ccc54;
|
||||
--src-tint: 9%;
|
||||
--sev-crit: #fb7185;
|
||||
--sev-err: #f87171;
|
||||
@@ -110,6 +112,8 @@
|
||||
--chip-l: 70%;
|
||||
--src-audit: #a983e6;
|
||||
--src-ls: #3cc3cc;
|
||||
--src-file: #9ccc54;
|
||||
--src-file: #9ccc54;
|
||||
--src-tint: 9%;
|
||||
--sev-crit: #fb7185;
|
||||
--sev-err: #f87171;
|
||||
@@ -189,6 +193,16 @@ svg { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round;
|
||||
.chip.on { color: var(--accent); border-color: var(--accent); background: var(--accent-soft); }
|
||||
|
||||
.actions { display: flex; align-items: center; gap: 6px; margin-left: auto; }
|
||||
/* Logged-in user, with how they logged in (oidc / local) */
|
||||
.whoami { display: flex; align-items: center; gap: 6px; min-width: 0; margin-left: 6px; font-size: 13px; color: var(--muted); }
|
||||
.whoami .who-name { font-weight: 600; color: var(--text); max-width: 18ch; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||||
.whoami .who-badge {
|
||||
flex: none; border: 1px solid var(--border); border-radius: 6px;
|
||||
padding: 1px 6px; font-size: 10.5px; font-weight: 650; letter-spacing: .02em;
|
||||
}
|
||||
.whoami .who-badge.oidc { color: var(--accent); border-color: var(--accent); background: var(--accent-soft); }
|
||||
.whoami .who-badge:empty { display: none; }
|
||||
.whoami.none .who-name { font-weight: 400; color: var(--muted); }
|
||||
.icon-btn {
|
||||
width: 36px; height: 36px; display: grid; place-items: center;
|
||||
border: 1px solid transparent; background: transparent; border-radius: 9px; color: var(--muted);
|
||||
@@ -414,8 +428,9 @@ body.col-resizing, body.col-resizing * { cursor: col-resize !important; user-sel
|
||||
one), light tint, badge before the app name. */
|
||||
.row.src-audit { --src: var(--src-audit); }
|
||||
.row.src-logstream { --src: var(--src-ls); }
|
||||
.row.src-audit, .row.src-logstream { border-left-color: var(--src); background: color-mix(in srgb, var(--src) var(--src-tint), transparent); }
|
||||
.row.src-audit:hover, .row.src-logstream:hover { background: color-mix(in srgb, var(--src) 13%, var(--row-hover)); }
|
||||
.row.src-file { --src: var(--src-file); }
|
||||
.row.src-audit, .row.src-logstream, .row.src-file { border-left-color: var(--src); background: color-mix(in srgb, var(--src) var(--src-tint), transparent); }
|
||||
.row.src-audit:hover, .row.src-logstream:hover, .row.src-file:hover { background: color-mix(in srgb, var(--src) 13%, var(--row-hover)); }
|
||||
.src-badge {
|
||||
display: inline-block; margin-right: 5px; padding: 0 4px; border-radius: 4px; vertical-align: 1px;
|
||||
font: 700 9.5px/1.45 var(--sans); letter-spacing: .04em;
|
||||
@@ -528,7 +543,7 @@ dialog.settings {
|
||||
}
|
||||
dialog.settings .dlg { display: flex; flex-direction: column; height: 100%; padding: 0; }
|
||||
dialog.settings .dlg > header { padding: 14px 16px 14px 22px; border-bottom: 1px solid var(--border); }
|
||||
.set-body { flex: 1; min-height: 0; display: grid; grid-template-columns: 176px minmax(0, 1fr); }
|
||||
.set-body { flex: 1; min-height: 0; display: grid; grid-template-columns: minmax(176px, max-content) minmax(0, 1fr); }
|
||||
.set-nav {
|
||||
display: flex; flex-direction: column; gap: 2px;
|
||||
padding: 12px 10px; border-right: 1px solid var(--border);
|
||||
@@ -685,7 +700,7 @@ input.switch:checked::after { transform: translateX(14px); }
|
||||
input.switch:disabled { cursor: not-allowed; }
|
||||
input.switch:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
|
||||
|
||||
/* Settings > Filters: toolbar kept in view while the list scrolls, dense rows. */
|
||||
/* Settings > Pattern: toolbar kept in view while the list scrolls, dense rows. */
|
||||
.tags-section { padding-top: 12px !important; }
|
||||
.tag-toolbar {
|
||||
position: sticky; top: -16px; z-index: 2;
|
||||
@@ -741,15 +756,45 @@ input.switch:focus-visible { outline: 2px solid var(--accent); outline-offset: 2
|
||||
.tag-row .icon-btn svg { width: 15px; height: 15px; }
|
||||
.opt input { display: none; }
|
||||
.opt:has(input:checked) { color: var(--accent); border-color: var(--accent); background: var(--accent-soft); }
|
||||
/* Log files (Settings > Sources) */
|
||||
.file-list { display: flex; flex-direction: column; gap: 4px; }
|
||||
.file-row {
|
||||
display: grid; align-items: center; gap: 6px;
|
||||
grid-template-columns: auto minmax(7rem, 1fr) minmax(10rem, 2fr) minmax(9rem, 1.4fr) minmax(6rem, .8fr) 26px;
|
||||
padding: 6px 6px 6px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--panel);
|
||||
}
|
||||
.file-row.off > input[type="text"], .file-row.off > select { opacity: .6; }
|
||||
.file-row input[type="text"] {
|
||||
height: 28px; padding: 0 8px; min-width: 0;
|
||||
border: 1px solid var(--border); border-radius: 6px; background: var(--panel); font-size: 12.5px; outline: 0;
|
||||
}
|
||||
.file-row input.mono { font-family: var(--mono); }
|
||||
.file-row input[type="text"]:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
|
||||
.file-row select.field { height: 28px; }
|
||||
.file-row .icon-btn { width: 26px; height: 26px; border-radius: 6px; }
|
||||
.file-row .icon-btn svg { width: 15px; height: 15px; }
|
||||
.file-row .fr-regex, .file-row .fr-status, .file-row .fr-sample { grid-column: 2 / -1; }
|
||||
.file-row .fr-sample {
|
||||
margin: 0; font: 11.5px var(--mono); color: var(--muted);
|
||||
white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
|
||||
}
|
||||
.file-row .fr-sample[hidden] { display: none; }
|
||||
.file-row .fr-regex[hidden] { display: none; }
|
||||
.file-row .fr-status { margin: 0 !important; font-size: 12px; }
|
||||
.file-toolbar { margin-top: 10px; }
|
||||
.tag-row .err { grid-column: 1 / -1; color: var(--sev-err); font-size: 12px; }
|
||||
.tag-row .err:empty { display: none; }
|
||||
|
||||
/* ---------- Mobile ---------- */
|
||||
@media (max-width: 760px) {
|
||||
.file-row { grid-template-columns: auto minmax(0, 1fr) 26px; }
|
||||
.file-row [data-del] { grid-column: 3; grid-row: 1; }
|
||||
.file-row > [data-f="path"], .file-row > [data-f="format"], .file-row > [data-f="host"] { grid-column: 2 / -1; }
|
||||
body { font-size: 13.5px; }
|
||||
.topbar { flex-wrap: wrap; padding: 8px 16px; gap: 8px 10px; }
|
||||
.search { order: 3; flex-basis: 100%; max-width: none; }
|
||||
.live .lbl { display: none; }
|
||||
.whoami .who-name { max-width: 12ch; }
|
||||
.filters { padding: 10px 16px 6px; }
|
||||
.filters select { flex: 1 1 calc(50% - 8px); max-width: none; min-width: 0; }
|
||||
.view-mode { flex: 1 1 100%; }
|
||||
@@ -794,7 +839,7 @@ input.switch:focus-visible { outline: 2px solid var(--accent); outline-offset: 2
|
||||
flex-direction: column; justify-content: center; height: auto; padding: 6px 2px; gap: 3px;
|
||||
font-size: 11.5px; text-align: center;
|
||||
}
|
||||
.set-nav button span { max-width: 100%; overflow: hidden; text-overflow: ellipsis; }
|
||||
.set-nav button span { max-width: 100%; white-space: normal; line-height: 1.15; overflow: hidden; text-overflow: ellipsis; }
|
||||
.set-panels { padding: 12px 12px 18px; }
|
||||
.set-panel { grid-template-columns: minmax(0, 1fr); gap: 10px; }
|
||||
.set-panel > .set-section, .set-panel > .set-section + .set-section { padding: 12px; }
|
||||
|
||||
Reference in new issue
Block a user