github.com/zntrio/harp/v2@v2.0.9/pkg/sdk/log/package.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 "io" 23 24 "github.com/pkg/errors" 25 "go.uber.org/zap" 26 "go.uber.org/zap/zapcore" 27 ) 28 29 var defaultFactory LoggerFactory 30 31 // ----------------------------------------------------------------------------- 32 33 func init() { 34 SetLoggerFactory(NewFactory(zap.L())) 35 } 36 37 // SetLoggerFactory defines the default package logger. 38 func SetLoggerFactory(instance LoggerFactory) { 39 if defaultFactory != nil { 40 defaultFactory.Bg().Debug("Replacing logger factory", zap.String("old", defaultFactory.Name()), zap.String("new", instance.Name())) 41 } else { 42 instance.Bg().Debug("Initializing logger factory", zap.String("factory", instance.Name())) 43 } 44 defaultFactory = instance 45 } 46 47 // ----------------------------------------------------------------------------- 48 49 // Bg delegates a no-context logger. 50 func Bg() Logger { 51 return checkFactory(defaultFactory).Bg() 52 } 53 54 // For delegates a context logger. 55 func For(ctx context.Context) Logger { 56 return checkFactory(defaultFactory).For(ctx) 57 } 58 59 // Default returns the logger factory. 60 func Default() LoggerFactory { 61 return checkFactory(defaultFactory) 62 } 63 64 // CheckErr handles error correctly. 65 func CheckErr(msg string, err error, fields ...zapcore.Field) { 66 if err != nil { 67 fields = append(fields, zap.Error(err)) 68 Default().Bg().Error(msg, fields...) 69 } 70 } 71 72 // CheckErrCtx handles error correctly. 73 func CheckErrCtx(ctx context.Context, msg string, err error, fields ...zapcore.Field) { 74 if err != nil { 75 fields = append(fields, zap.Error(errors.WithStack(err))) 76 Default().For(ctx).Error(msg, fields...) 77 } 78 } 79 80 // SafeClose handles the closer error. 81 func SafeClose(c io.Closer, msg string, fields ...zapcore.Field) { 82 if cerr := c.Close(); cerr != nil { 83 fields = append(fields, zap.Error(errors.WithStack(cerr))) 84 Default().Bg().Error(msg, fields...) 85 } 86 } 87 88 // SafeCloseCtx handles the closer error. 89 func SafeCloseCtx(ctx context.Context, c io.Closer, msg string, fields ...zapcore.Field) { 90 if cerr := c.Close(); cerr != nil { 91 fields = append(fields, zap.Error(errors.WithStack(cerr))) 92 Default().For(ctx).Error(msg, fields...) 93 } 94 } 95 96 // ----------------------------------------------------------------------------- 97 98 func checkFactory(defaultFactory LoggerFactory) LoggerFactory { 99 if defaultFactory == nil { 100 panic("Unable to create logger instance, you have to register an adapter first.") 101 } 102 return defaultFactory 103 }