forked from GoogleCloudPlatform/nodejs-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for structured logs (GoogleCloudPlatform#66)
This is a port of GoogleCloudPlatform/cloud-sql-proxy#1246
- Loading branch information
Showing
9 changed files
with
225 additions
and
71 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
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,98 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package log | ||
|
||
import ( | ||
"io" | ||
llog "log" | ||
"os" | ||
|
||
"github.com/GoogleCloudPlatform/alloydb-auth-proxy/alloydb" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
// StdLogger is the standard logger that distinguishes between info and error | ||
// logs. | ||
type StdLogger struct { | ||
infoLog *llog.Logger | ||
errLog *llog.Logger | ||
} | ||
|
||
// NewStdLogger create a Logger that uses out and err for informational and | ||
// error messages. | ||
func NewStdLogger(out, err io.Writer) alloydb.Logger { | ||
return &StdLogger{ | ||
infoLog: llog.New(out, "", llog.LstdFlags), | ||
errLog: llog.New(err, "", llog.LstdFlags), | ||
} | ||
} | ||
|
||
func (l *StdLogger) Infof(format string, v ...interface{}) { | ||
l.infoLog.Printf(format, v...) | ||
} | ||
|
||
func (l *StdLogger) Errorf(format string, v ...interface{}) { | ||
l.errLog.Printf(format, v...) | ||
} | ||
|
||
func (l *StdLogger) Debugf(format string, v ...interface{}) { | ||
l.infoLog.Printf(format, v...) | ||
} | ||
|
||
// StructuredLogger writes log messages in JSON. | ||
type StructuredLogger struct { | ||
logger *zap.SugaredLogger | ||
} | ||
|
||
func (l *StructuredLogger) Infof(format string, v ...interface{}) { | ||
l.logger.Infof(format, v...) | ||
} | ||
|
||
func (l *StructuredLogger) Errorf(format string, v ...interface{}) { | ||
l.logger.Errorf(format, v...) | ||
} | ||
|
||
func (l *StructuredLogger) Debugf(format string, v ...interface{}) { | ||
l.logger.Infof(format, v...) | ||
} | ||
|
||
// NewStructuredLogger creates a Logger that logs messages using JSON. | ||
func NewStructuredLogger() (alloydb.Logger, func() error) { | ||
// Configure structured logs to adhere to LogEntry format | ||
// https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry | ||
c := zap.NewProductionEncoderConfig() | ||
c.LevelKey = "severity" | ||
c.MessageKey = "message" | ||
c.TimeKey = "timestamp" | ||
c.EncodeLevel = zapcore.CapitalLevelEncoder | ||
c.EncodeTime = zapcore.ISO8601TimeEncoder | ||
|
||
enc := zapcore.NewJSONEncoder(c) | ||
core := zapcore.NewTee( | ||
zapcore.NewCore(enc, zapcore.Lock(os.Stdout), zap.LevelEnablerFunc(func(l zapcore.Level) bool { | ||
// Anything below error, goes to the info log. | ||
return l < zapcore.ErrorLevel | ||
})), | ||
zapcore.NewCore(enc, zapcore.Lock(os.Stderr), zap.LevelEnablerFunc(func(l zapcore.Level) bool { | ||
// Anything at error or higher goes to the error log. | ||
return l >= zapcore.ErrorLevel | ||
})), | ||
) | ||
l := &StructuredLogger{ | ||
logger: zap.New(core).Sugar(), | ||
} | ||
return l, l.logger.Sync | ||
} |
Oops, something went wrong.