github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/logger/file_hook.go (about) 1 // Copyright © 2022 Alibaba Group Holding Ltd. 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 logger 16 17 import ( 18 "fmt" 19 "path/filepath" 20 "runtime" 21 "time" 22 23 rotatelogs "github.com/lestrrat-go/file-rotatelogs" 24 "github.com/rifflock/lfshook" 25 "github.com/sirupsen/logrus" 26 27 "github.com/sealerio/sealer/common" 28 ) 29 30 func NewFileHook(filePath string) (logrus.Hook, error) { 31 if filePath == "" { 32 //todo : user could set log dir through sealer config 33 filePath = common.DefaultLogDir 34 } 35 36 path := filepath.Join(filePath, "sealer.log") 37 writer, _ := rotatelogs.New( 38 path+".%Y%m%d", 39 rotatelogs.WithLinkName(path), 40 rotatelogs.WithRotationTime(24*time.Hour), 41 ) 42 43 return lfshook.NewHook(lfshook.WriterMap{ 44 logrus.InfoLevel: writer, 45 logrus.DebugLevel: writer, 46 logrus.WarnLevel: writer, 47 logrus.ErrorLevel: writer, 48 logrus.FatalLevel: writer, 49 logrus.PanicLevel: writer, 50 }, &logrus.TextFormatter{ 51 DisableColors: true, 52 CallerPrettyfier: func(frame *runtime.Frame) (function string, file string) { 53 return "", fmt.Sprintf("%s:%d", filepath.Base(frame.File), frame.Line) 54 }, 55 }), nil 56 }