github.com/zntrio/harp/v2@v2.0.9/pkg/sdk/log/builder.go (about) 1 // Licensed to Elasticsearch B.V. under one or more contributor 2 // license agreements. See the NOTICE file distributed with 3 // this work for additional information regarding copyright 4 // ownership. Elasticsearch B.V. licenses this file to you under 5 // the Apache License, Version 2.0 (the "License"); you may 6 // not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, 12 // software distributed under the License is distributed on an 13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 // KIND, either express or implied. See the License for the 15 // specific language governing permissions and limitations 16 // under the License. 17 18 package log 19 20 import ( 21 "context" 22 23 "go.uber.org/zap" 24 "go.uber.org/zap/zapcore" 25 ) 26 27 // ----------------------------------------------------------------------------- 28 29 // Options declares logger options for builder. 30 type Options struct { 31 Debug bool 32 LogLevel string 33 AppName string 34 AppID string 35 Version string 36 Revision string 37 SentryDSN string 38 } 39 40 // ----------------------------------------------------------------------------- 41 42 // DefaultOptions defines default logger options. 43 var DefaultOptions = &Options{ 44 Debug: false, 45 LogLevel: "info", 46 AppName: "changeme", 47 AppID: "changeme", 48 Version: "0.0.1", 49 Revision: "123456789", 50 SentryDSN: "", 51 } 52 53 // ----------------------------------------------------------------------------- 54 55 // Setup the logger. 56 func Setup(ctx context.Context, opts *Options) { 57 // Initialize logs 58 var config zap.Config 59 60 if opts.Debug { 61 opts.LogLevel = "debug" 62 config = zap.NewDevelopmentConfig() 63 config.DisableCaller = true 64 config.DisableStacktrace = true 65 } else { 66 config = zap.NewProductionConfig() 67 config.DisableStacktrace = true 68 config.EncoderConfig.MessageKey = "@message" 69 config.EncoderConfig.TimeKey = "@timestamp" 70 config.EncoderConfig.CallerKey = "@caller" 71 config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder 72 } 73 74 // Parse log level 75 errLogLevel := config.Level.UnmarshalText([]byte(opts.LogLevel)) 76 if errLogLevel != nil { 77 panic(errLogLevel) 78 } 79 80 // Build real logger 81 logger, err := config.Build( 82 zap.AddCallerSkip(2), 83 ) 84 if err != nil { 85 panic(err) 86 } 87 88 // Add prefix to logger 89 logger = logger.With( 90 zap.String("@appName", opts.AppName), 91 zap.String("@version", opts.Version), 92 zap.String("@revision", opts.Revision), 93 zap.String("@appID", opts.AppID), 94 zap.Namespace("@fields"), 95 ) 96 97 // Prepare factory 98 logFactory := NewFactory(logger) 99 100 // Override the global factory 101 SetLoggerFactory(logFactory) 102 103 // Override zap default logger 104 zap.ReplaceGlobals(logger) 105 }