-
Notifications
You must be signed in to change notification settings - Fork 458
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial support for JSON logging
Hook into logging machinery. Signed-off-by: Alexey Palazhchenko <alexey.palazhchenko@talos-systems.com>
- Loading branch information
Showing
20 changed files
with
341 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package logging | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/talos-systems/talos/internal/app/machined/pkg/runtime" | ||
) | ||
|
||
type jsonSender struct { | ||
l *log.Logger | ||
} | ||
|
||
// NewJSON returns log sender that would eventually send logs in JSON. | ||
// FIXME(aleksi): update comment. | ||
func NewJSON() runtime.LogSender { | ||
return &jsonSender{ | ||
l: log.New(os.Stdout, "JSON: ", 0), | ||
} | ||
} | ||
|
||
func (j *jsonSender) Send(ctx context.Context, e *runtime.LogEvent) error { | ||
m := make(map[string]interface{}, len(e.Fields)+3) | ||
m["msg"] = e.Msg | ||
m["time"] = e.Time.Unix() | ||
m["level"] = e.Level.String() | ||
|
||
for k, v := range e.Fields { | ||
m[k] = v | ||
} | ||
|
||
b, err := json.Marshal(m) | ||
if err != nil { | ||
return fmt.Errorf("%w: %s", runtime.ErrDontRetry, err) | ||
} | ||
|
||
j.l.Printf("%s\n", b) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package logging | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/talos-systems/talos/internal/app/machined/pkg/runtime" | ||
) | ||
|
||
type nullSender struct{} | ||
|
||
// NewNull returns log sender that does nothing. | ||
func NewNull() runtime.LogSender { | ||
return nullSender{} | ||
} | ||
|
||
func (nullSender) Send(context.Context, *runtime.LogEvent) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.