Provides a zap logger, adjusted for the GCP Cloud Logging format by zapdriver and wrapped by otelzap to provide Opentelemetry tracing/logging support. Meaning that if the incoming context contains a trace then Zap log messages will be recorded as events on the span. The log entries in GCP will also include the "trace_id" as a field to further increase the observability.
About GCP Cloud Logging, the logger is configured to use Error Reporting as described here.
go get github.com/dentech-floss/[email protected]
package example
import (
"github.com/dentech-floss/metadata/pkg/metadata"
"github.com/dentech-floss/logging/pkg/logging"
"github.com/dentech-floss/revision/pkg/revision"
)
func main() {
metadata := metadata.NewMetadata()
logger := logging.NewLogger(
&logging.LoggerConfig{
OnGCP: metadata.OnGCP,
ServiceName: revision.ServiceName,
},
)
defer logger.Sync() // flushes buffer, if any
patientGatewayServiceV1 := service.NewPatientGatewayServiceV1(logger) // inject it
}
package example
import (
"github.com/dentech-floss/logging/pkg/logging"
patient_gateway_service_v1 "go.buf.build/dentechse/go-grpc-gateway-openapiv2/dentechse/patient-api-gateway/api/patient/v1"
)
func (s *PatientGatewayServiceV1) FindAppointments(
ctx context.Context,
request *patient_gateway_service_v1.FindAppointmentsRequest,
) (*patient_gateway_service_v1.FindAppointmentsResponse, error) {
// Ensure trace information + request is part of the log entries
logWithContext := s.logger.WithContext(ctx, logging.ProtoField("request", request))
logWithContext.Info(
"Something something...",
logging.StringField("something", something),
)
startTimeLocal, err := datetime.ISO8601StringToTime(request.StartTime)
if err != nil {
logWithContext.Warn("The start time shall be in ISO 8601 format", logging.ErrorField(err))
return &patient_gateway_service_v1.FindAppointmentsResponse{},
status.Errorf(codes.InvalidArgument, "The start time shall be in ISO 8601 format")
}
}