github.com/pavlo67/common@v0.5.3/common/logger/operator.go (about) 1 package logger 2 3 import ( 4 "image" 5 "path/filepath" 6 "regexp" 7 "strings" 8 9 "github.com/pavlo67/common/common" 10 11 "github.com/pavlo67/common/common/errors" 12 "github.com/pavlo67/common/common/filelib" 13 14 "github.com/pavlo67/common/common/joiner" 15 ) 16 17 const InterfaceKey joiner.InterfaceKey = "logger" 18 19 type Level int 20 21 type Config struct { 22 Key string 23 LogLevel Level 24 BasePath string 25 OutputPaths []string 26 ErrorPaths []string 27 Encoding string 28 SaveFiles bool 29 } 30 31 const TraceLevel Level = -2 32 const DebugLevel Level = -1 33 const InfoLevel Level = 0 34 const WarnLevel Level = 1 35 const ErrorLevel Level = 2 36 const FatalLevel Level = 4 37 38 type GetImage interface { 39 Image(opts common.Map) (image.Image, string, error) 40 Bounds() image.Rectangle 41 } 42 43 type Operator interface { 44 Debug(args ...interface{}) 45 Debugf(template string, args ...interface{}) 46 47 Info(args ...interface{}) 48 Infof(template string, args ...interface{}) 49 50 Warn(args ...interface{}) 51 Warnf(template string, args ...interface{}) 52 53 Error(args ...interface{}) 54 Errorf(template string, args ...interface{}) 55 56 Fatal(args ...interface{}) 57 Fatalf(template string, args ...interface{}) 58 59 Comment(text string) 60 61 SetKey(key string) 62 Key() string 63 64 SetPath(basePath string) 65 File(path string, data []byte) 66 Image(path string, getImage GetImage, opts common.Map) 67 } 68 69 // TODO!!! be careful in windows 70 71 var reRootPath = regexp.MustCompile(`^/`) 72 73 func ModifyPaths(paths []string, basePath string) ([]string, error) { 74 if basePath = strings.TrimSpace(basePath); basePath == "" { 75 return paths, nil 76 } 77 78 var err error 79 if basePath, err = filelib.Dir(basePath); err != nil { 80 return nil, errors.Wrapf(err, "on logger.ModifyPaths()") 81 } 82 83 modifiedPaths := make([]string, len(paths)) 84 85 for i, path := range paths { 86 if path == "stdin" || path == "stdout" || path == "stderr" || reRootPath.MatchString(path) { 87 modifiedPaths[i] = path 88 } else { 89 modifiedPaths[i] = filepath.Join(basePath, path) 90 } 91 } 92 93 return modifiedPaths, nil 94 }