🌱 cron: structured logging (#3167)

* Structured logging for cron

Signed-off-by: Raghav Kaul <raghavkaul@google.com>

* also update scorecard worker logger

Signed-off-by: Raghav Kaul <raghavkaul@google.com>

* address pr comments

* set json fields
* docs link

---------

Signed-off-by: Raghav Kaul <raghavkaul@google.com>
This commit is contained in:
Raghav Kaul 2023-06-23 12:53:31 -04:00 committed by GitHub
parent 2aa9887f13
commit eb941e3964
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 2 deletions

View File

@ -118,7 +118,7 @@ func newScorecardWorker() (*ScorecardWorker, error) {
}
sw.ctx = context.Background()
sw.logger = log.NewLogger(log.InfoLevel)
sw.logger = log.NewCronLogger(log.InfoLevel)
sw.githubClient = githubrepo.CreateGithubRepoClient(sw.ctx, sw.logger)
// TODO(raghavkaul): Read GitLab auth token from environment
if sw.gitlabClient, err = gitlabrepo.CreateGitlabClient(sw.ctx, "https://gitlab.com"); err != nil {

View File

@ -74,7 +74,7 @@ func (wl *WorkLoop) Run() error {
return fmt.Errorf("config.GetResultDataBucketURL: %w", err)
}
logger := log.NewLogger(log.InfoLevel)
logger := log.NewCronLogger(log.InfoLevel)
for {
req, err := subscriber.SynchronousPull()

View File

@ -41,6 +41,23 @@ func NewLogger(logLevel Level) *Logger {
return NewLogrusLogger(logrusLog)
}
// NewCronLogger creates an instance of *Logger.
func NewCronLogger(logLevel Level) *Logger {
logrusLog := logrus.New()
// for stackdriver, see: https://cloud.google.com/logging/docs/structured-logging#special-payload-fields
logrusLog.SetFormatter(&logrus.JSONFormatter{FieldMap: logrus.FieldMap{
logrus.FieldKeyLevel: "severity",
logrus.FieldKeyMsg: "message",
}})
// Set log level from logrus
logrusLevel := parseLogrusLevel(logLevel)
logrusLog.SetLevel(logrusLevel)
return NewLogrusLogger(logrusLog)
}
// NewLogrusLogger creates an instance of *Logger backed by the supplied
// logrusLog instance.
func NewLogrusLogger(logrusLog *logrus.Logger) *Logger {