mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 11:03:34 +00:00
feat(logs): 'uc machine logs' to view logs from systemd services on machines (#283)
* logging: client side Create internal/logs and put abstracted way service and machine logging in there This adds the bits to do machine logging of the services. See #273 and discussion in #158 Signed-off-by: Miek Gieben <miek@miek.nl> * go mod tidy Signed-off-by: Miek Gieben <miek@miek.nl> * fix typo Signed-off-by: Miek Gieben <miek@miek.nl> * Hook it up further Signed-off-by: Miek Gieben <miek@miek.nl> * Check unit validity server side Signed-off-by: Miek Gieben <miek@miek.nl> * Make cli-docs Signed-off-by: Miek Gieben <miek@miek.nl> * Add newline to logs Signed-off-by: Miek Gieben <miek@miek.nl> * docs Signed-off-by: Miek Gieben <miek@miek.nl> * Polish a bit Signed-off-by: Miek Gieben <miek@miek.nl> * comment fix Signed-off-by: Miek Gieben <miek@miek.nl> * There is no groupid Signed-off-by: Miek Gieben <miek@miek.nl> --------- Signed-off-by: Miek Gieben <miek@miek.nl>
This commit is contained in:
@@ -10,11 +10,31 @@ import (
|
||||
"github.com/psviderski/uncloud/pkg/api"
|
||||
)
|
||||
|
||||
const (
|
||||
UnitUncloud = "uncloud"
|
||||
UnitDocker = "docker"
|
||||
UnitCorrosion = "uncloud-corrosion"
|
||||
)
|
||||
|
||||
func ValidUnit(unit string) bool {
|
||||
switch unit {
|
||||
case UnitUncloud:
|
||||
case UnitDocker:
|
||||
case UnitCorrosion:
|
||||
default:
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
const journalctl = "journalctl"
|
||||
|
||||
var commandContext = exec.CommandContext // overidable for the test
|
||||
var commandContext = exec.CommandContext // allow override for test
|
||||
|
||||
func logs(ctx context.Context, unit string, opts api.ServiceLogsOptions) (io.ReadCloser, error) {
|
||||
if !ValidUnit(unit) {
|
||||
return nil, fmt.Errorf("journal logs: invalid unit: %s", unit)
|
||||
}
|
||||
args := []string{"-u", unit, "--no-hostname"}
|
||||
args = append(args, "-n")
|
||||
if opts.Tail > -1 {
|
||||
|
||||
@@ -12,12 +12,7 @@ import (
|
||||
|
||||
// Logs streams logs from a service and returns entries via a channel.
|
||||
func Logs(ctx context.Context, unit string, opts api.ServiceLogsOptions) (<-chan api.LogEntry, error) {
|
||||
// Hard code unit check for now
|
||||
switch unit {
|
||||
case "uncloud":
|
||||
case "uncloud-corrosion":
|
||||
case "docker":
|
||||
default:
|
||||
if !ValidUnit(unit) {
|
||||
return nil, fmt.Errorf("journal logs: invalid unit: %s", unit)
|
||||
}
|
||||
|
||||
@@ -54,7 +49,7 @@ func entry(data []byte) api.LogEntry {
|
||||
|
||||
return api.LogEntry{
|
||||
Timestamp: timestamp,
|
||||
Message: slices.Clone(message), // scanner controls the buffer
|
||||
Message: append(slices.Clone(message), '\n'), // scanner controls the buffer so Clone and re-add newline
|
||||
Stream: api.LogStreamStdout,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user