github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/logger/logger.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 "github.com/pkg/errors" 19 "github.com/sirupsen/logrus" 20 ) 21 22 type LogOptions struct { 23 // sealer log file path, default log directory is `/var/lib/sealer/log` 24 OutputPath string 25 // Verbose: sealer log level,if it is ture will set debug log mode. 26 Verbose bool 27 // DisableColor if true will disable outputting colors. 28 DisableColor bool 29 RemoteLoggerURL string 30 RemoteLoggerTaskName string 31 // LogToFile flag represent whether write log to disk, default is false. 32 LogToFile bool 33 } 34 35 func Init(options LogOptions) error { 36 if options.Verbose { 37 logrus.SetLevel(logrus.DebugLevel) 38 } else { 39 logrus.SetLevel(logrus.InfoLevel) 40 } 41 42 logrus.SetReportCaller(true) 43 44 logrus.SetFormatter(&Formatter{ 45 DisableColor: options.DisableColor, 46 }) 47 48 if options.LogToFile { 49 fh, err := NewFileHook(options.OutputPath) 50 if err != nil { 51 return errors.Errorf("failed to init log file hook: %v", err) 52 } 53 logrus.AddHook(fh) 54 } 55 56 if options.RemoteLoggerURL != "" { 57 rl, err := NewRemoteLogHook(options.RemoteLoggerURL, options.RemoteLoggerTaskName) 58 if err != nil { 59 return errors.Errorf("failed to init log remote hook: %v", err) 60 } 61 logrus.AddHook(rl) 62 } 63 64 return nil 65 }