github.com/matrixorigin/matrixone@v1.2.0/pkg/logutil/action.go (about) 1 // Copyright 2022 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package logutil 16 17 import ( 18 "time" 19 20 "go.uber.org/zap" 21 ) 22 23 var ( 24 actionClose = zap.String("action", "close") 25 actionAsyncTask = zap.String("action", "run-async-task") 26 ) 27 28 // LogClose used to log close any components. 29 func LogClose(logger *zap.Logger, components string, fields ...zap.Field) func() { 30 startAt := time.Now() 31 fields = append(fields, 32 actionClose, 33 componentsField(components)) 34 35 logger.Info("start to close components", fields...) 36 return func() { 37 fields = append(fields, costField(startAt)) 38 logger.Info("close components completed", fields...) 39 } 40 } 41 42 // LogAsyncTask used to log any async task, and will write a log if the async task exited. 43 func LogAsyncTask(logger *zap.Logger, task string, fields ...zap.Field) func() { 44 startAt := time.Now() 45 fields = append(fields, 46 actionAsyncTask, 47 taskField(task)) 48 49 logger.Info("start to run async task", fields...) 50 return func() { 51 fields = append(fields, costField(startAt)) 52 logger.Info("async task exited", fields...) 53 } 54 } 55 56 func componentsField(components string) zap.Field { 57 return zap.String("components", components) 58 } 59 60 func taskField(task string) zap.Field { 61 return zap.String("async-task", task) 62 } 63 64 func costField(startAt time.Time) zap.Field { 65 return zap.Duration("cost", time.Duration(time.Since(startAt))) 66 }