tus / tusd

Reference server implementation in Go of tus: the open protocol for resumable file uploads
https://tus.github.io/tusd
MIT License
2.93k stars 465 forks source link

cli: Option log-format=json is ignored for plugin logs #1099

Closed quality-leftovers closed 2 months ago

quality-leftovers commented 3 months ago

Describe the bug Using -log-format=json does not apply to logging of (hc)plugin

To Reproduce Steps to reproduce the behavior:

  1. Start tusd using -log-format=json and (hc)plugin hook
  2. Check logs
  3. The logs of the main process are formatted as json, the plugin logs are formatted as plain text

Expected behavior All logs are formatted as json

Setup details I tried using both "log" and hc-log in the plugin.

Initializing the logger in the plugin using

hclog.New(&hclog.LoggerOptions{
        Level:      logLevel,
        JSONFormat: true,
    })

does not produce the desired results.

Instead I had to add JSONFormat: true, to https://github.com/tus/tusd/blob/50b9ff5623c26c856ec97e2e0a9a44c0f6baaceb/pkg/hooks/plugin/plugin.go#L36-L40

It would be nice to either pass the Json flag to the hclogger instance or if something like an hclog->slog adapter was use d (like https://github.com/evanphx/go-hclog-slog, but in the opposite direction. Not sure if such a package exists / is possible)

Log output "as is"

{"time":"2024-03-19T14:29:53.249651675Z","level":"INFO","msg":"Using '/srv/tusd-data/data' as directory storage."}
{"time":"2024-03-19T14:29:53.250772425Z","level":"INFO","msg":"Using 0.00MB as maximum size."}
{"time":"2024-03-19T14:29:53.250811027Z","level":"INFO","msg":"Using '/usr/local/bin/tusd-hook-plugin' to load plugin for hooks"}
2024/03/19 02:29:53.251028 [DEBUG] plugin: starting plugin: path=/usr/local/bin/tusd-hook-plugin args=["/usr/local/bin/tusd-hook-plugin"]
2024/03/19 02:29:53.251524 [DEBUG] plugin: plugin started: path=/usr/local/bin/tusd-hook-plugin pid=15
2024/03/19 02:29:53.251638 [DEBUG] plugin: waiting for RPC address: plugin=/usr/local/bin/tusd-hook-plugin
2024/03/19 02:29:53.259521 [DEBUG] plugin.tusd-hook-plugin: plugin address: network=unix address=/tmp/plugin1135885035 timestamp=2024-03-19T14:29:53.259Z
2024/03/19 02:29:53.262943 [INFO]  plugin.tusd-hook-plugin: Setup: timestamp=2024-03-19T14:29:53.26
...

Log output using: https://github.com/quality-leftovers/tusd/tree/plugin-log-json

{"time":"2024-03-19T14:13:11.704897099Z","level":"INFO","msg":"Using '/srv/tusd-data/data' as directory storage."}
{"time":"2024-03-19T14:13:11.705671906Z","level":"INFO","msg":"Using 0.00MB as maximum size."}
{"time":"2024-03-19T14:13:11.705711607Z","level":"INFO","msg":"Using '/usr/local/bin/tusd-hook-plugin' to load plugin for hooks"}
{"@level":"debug","@message":"starting plugin","@module":"plugin","@timestamp":"2024/03/19 02:13:11.705805","args":["/usr/local/bin/tusd-hook-plugin"],"path":"/usr/local/bin/tusd-hook-plugin"}
{"@level":"debug","@message":"plugin started","@module":"plugin","@timestamp":"2024/03/19 02:13:11.706353","path":"/usr/local/bin/tusd-hook-plugin","pid":13}
{"@level":"debug","@message":"waiting for RPC address","@module":"plugin","@timestamp":"2024/03/19 02:13:11.706474","plugin":"/usr/local/bin/tusd-hook-plugin"}
{"@level":"debug","@message":"using plugin","@module":"plugin","@timestamp":"2024/03/19 02:13:11.722045","version":1}
{"@level":"debug","@message":"plugin address","@module":"plugin.tusd-hook-plugin","@timestamp":"2024/03/19 02:13:11.722055","address":"/tmp/plugin3216457465","network":"unix","timestamp":"2024-03-19T14:13:11.721Z"}
{"@level":"info","@message":"Setup","@module":"plugin.tusd-hook-plugin","@timestamp":"2024/03/19 02:13:11.728717","timestamp":"2024-03-19T14:13:11.728Z"}
Acconut commented 3 months ago

Thank you for reporting this. The easiest solution would be to set hclog.LoggerOptions.JSONFormat = true if the JSON format is specified in the flags. This could be passed in using a new field in the PluginHook struct. Would you be willing to open a PR for this?