github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/logs/context.go (about) 1 /* 2 * Copyright 2023 Wang Min Xiang 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 */ 17 18 package logs 19 20 import ( 21 "fmt" 22 "github.com/aacfactory/errors" 23 "github.com/aacfactory/fns/context" 24 "github.com/aacfactory/logs" 25 ) 26 27 var ( 28 contextKey = []byte("@fns:context:log") 29 ) 30 31 func With(ctx context.Context, v logs.Logger) { 32 ctx.SetLocalValue(contextKey, v) 33 } 34 35 func Load(ctx context.Context) logs.Logger { 36 v := ctx.LocalValue(contextKey) 37 if v == nil { 38 panic(fmt.Sprintf("%+v", errors.Warning("fns: there is no log in context"))) 39 return nil 40 } 41 lg, ok := v.(logs.Logger) 42 if !ok { 43 panic(fmt.Sprintf("%+v", errors.Warning("fns: contextKey in context is not github.com/aacfactory/logs.Logger"))) 44 return nil 45 } 46 return lg 47 }