Skip to content

Commit

Permalink
Merge pull request #194 from listx/fix-auditor
Browse files Browse the repository at this point in the history
address review comments from #193
  • Loading branch information
k8s-ci-robot authored Mar 10, 2020
2 parents 022877c + 6c19056 commit 0f4af34
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
6 changes: 5 additions & 1 deletion lib/audit/auditor.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ func initServerContext(
}

erc := initErrorReportingClient(gcpProjectID)
loggingFacility := logclient.NewGcpLoggingFacility(gcpProjectID, LogName)
loggingFacility, err := logclient.NewGcpLoggingFacility(
gcpProjectID, LogName)
if err != nil {
return nil, err
}

serverContext := ServerContext{
ID: uuid,
Expand Down
6 changes: 3 additions & 3 deletions lib/logclient/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ func (fakeLogClient *FakeLogClient) Close() error { return nil }
// all local (stdout), with a FakeLogClient.
func NewFakeLoggingFacility() *LoggingFacility {

logInfo := log.New(os.Stdout, "FAKE-INFO", log.LstdFlags)
logError := log.New(os.Stdout, "FAKE-ERROR", log.LstdFlags)
logAlert := log.New(os.Stdout, "FAKE-ALERT", log.LstdFlags)
logInfo := log.New(os.Stderr, "FAKE-INFO", log.LstdFlags)
logError := log.New(os.Stderr, "FAKE-ERROR", log.LstdFlags)
logAlert := log.New(os.Stderr, "FAKE-ALERT", log.LstdFlags)

return New(logInfo, logError, logAlert, &FakeLogClient{})
}
22 changes: 11 additions & 11 deletions lib/logclient/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,35 @@ import (
"context"

"cloud.google.com/go/logging"
"k8s.io/klog"
)

// NewGcpLoggingFacility returns a new LoggingFacility that logs to GCP
// resources. As such, it requires the GCP projectID as well as the logName to
// log to.
func NewGcpLoggingFacility(projectID, logName string) *LoggingFacility {
gcpLogClient := initGcpLogClient(projectID)
func NewGcpLoggingFacility(
projectID, logName string,
) (*LoggingFacility, error) {

gcpLogClient, err := initGcpLogClient(projectID)
if err != nil {
return nil, err
}

logInfo := gcpLogClient.Logger(logName).StandardLogger(logging.Info)
logError := gcpLogClient.Logger(logName).StandardLogger(logging.Error)
logAlert := gcpLogClient.Logger(logName).StandardLogger(logging.Alert)

return New(logInfo, logError, logAlert, gcpLogClient)
return New(logInfo, logError, logAlert, gcpLogClient), nil
}

// initGcpLogClient creates a logging client that performs better logging than
// the default behavior on GCP Stackdriver. For instance, logs sent with this
// client are not split up over newlines, and also the severity levels are
// actually understood by Stackdriver.
func initGcpLogClient(projectID string) *logging.Client {
func initGcpLogClient(projectID string) (*logging.Client, error) {

ctx := context.Background()

// Creates a client.
client, err := logging.NewClient(ctx, projectID)
if err != nil {
klog.Fatalf("Failed to create client: %v", err)
}

return client
return logging.NewClient(ctx, projectID)
}

0 comments on commit 0f4af34

Please sign in to comment.