/
logs.go
47 lines (40 loc) · 1.3 KB
/
logs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// Package logging contains utility functions for ECS logging.
package logging
import (
"fmt"
"io"
"github.com/aws/copilot-cli/internal/pkg/aws/cloudwatchlogs"
)
// HumanJSONStringer can output in both human-readable and JSON format.
type HumanJSONStringer interface {
HumanString() string
JSONString() (string, error)
}
// WriteJSONLogs outputs CloudWatch logs in JSON format.
func WriteJSONLogs(w io.Writer, logStringers []HumanJSONStringer) error {
for _, logStringer := range logStringers {
data, err := logStringer.JSONString()
if err != nil {
return fmt.Errorf("get log string in JSON: %w", err)
}
fmt.Fprint(w, data)
}
return nil
}
// WriteHumanLogs outputs CloudWatch logs in human-readable format.
func WriteHumanLogs(w io.Writer, logStringers []HumanJSONStringer) error {
for _, logStringer := range logStringers {
fmt.Fprint(w, logStringer.HumanString())
}
return nil
}
func cwEventsToHumanJSONStringers(events []*cloudwatchlogs.Event) []HumanJSONStringer {
// golang limitation: https://golang.org/doc/faq#convert_slice_of_interface
logStringers := make([]HumanJSONStringer, len(events))
for ind, event := range events {
logStringers[ind] = event
}
return logStringers
}