aws lambda variable with execution scope in golang

131 views Asked by At

I have a variable for logger in a package main:

This is the folder tree directory:

cmd:
  lambda:
      main.go
  service:
     xxxx.go
     yyyy.go
  repository:
     aa_repository.go

In my main lambda I have the following code:

var (
    log *zap.Logger
)

func main() {
    log = InitLoggerWithUUID()
    lambda.Start(handler)
}

I am adding an UUID for my execution in my lambda, to have the trazability of all execution.

But in my xxxx.go service and in my yyyyy.service and in my aa_repository I can't import the main to use the log variable created in lambda package (main.go) because i obtain "import cycle not allowed"

Because my main call the services, and the services the repositories.

And I don't want to receive the logger as a parameter in each method, because it seems very complicated only for keep the UUID for execution.

Other Idea that i had was:

create other package logger with logger.go and here

var (
    log *zap.Logger
)

func init() {
    log = InitLoggerWithUUID()
}

and overwrite the methods Info, Warning, etc:

func Info(message string, fields ...zap.Field) {
    log.Info(message, fields...)
}

func Warn(message string, fields ...zap.Field) {
    log.Warn(message, fields...)
}

and import this new package from my main, services and repositories:

import log "my-lambda/cmd/logger"

But in this way I have a log global variable for my lambda, and no a log variable for each execution with his own UUID.

ANd I don't have other Idea to solve this.

0

There are 0 answers