github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/gadget-service/logger.go (about) 1 // Copyright 2023 The Inspektor Gadget authors 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 gadgetservice 16 17 import ( 18 "fmt" 19 20 "github.com/inspektor-gadget/inspektor-gadget/pkg/gadget-service/api" 21 "github.com/inspektor-gadget/inspektor-gadget/pkg/logger" 22 ) 23 24 // Logger sends log messages through grpc 25 type Logger struct { 26 send func(*api.GadgetEvent) error 27 level logger.Level 28 fallbackLogger logger.Logger 29 } 30 31 func (l *Logger) SetLevel(level logger.Level) { 32 l.level = level 33 } 34 35 func (l *Logger) GetLevel() logger.Level { 36 return l.level 37 } 38 39 func (l *Logger) Logf(severity logger.Level, format string, params ...any) { 40 if l.level < severity { 41 return 42 } 43 ev := &api.GadgetEvent{ 44 Type: uint32(severity) << api.EventLogShift, 45 Payload: []byte(fmt.Sprintf(format, params...)), 46 } 47 if err := l.send(ev); err != nil { 48 l.fallbackLogger.Logf(severity, format, params...) 49 } 50 } 51 52 func (l *Logger) Log(severity logger.Level, params ...any) { 53 if l.level < severity { 54 return 55 } 56 ev := &api.GadgetEvent{ 57 Type: uint32(severity) << api.EventLogShift, 58 Payload: []byte(fmt.Sprintf("%+v", params...)), 59 } 60 if err := l.send(ev); err != nil { 61 l.fallbackLogger.Log(severity, params...) 62 } 63 }