github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/config/logging.go (about) 1 package config 2 3 import "github.com/qri-io/jsonschema" 4 5 // Logging encapsulates logging configuration 6 type Logging struct { 7 // Levels is a map of package_name : log_level (one of [info, error, debug, warn]) 8 Levels map[string]string `json:"levels"` 9 } 10 11 // SetArbitrary is an interface implementation of base/fill/struct in order to safely 12 // consume config files that have definitions beyond those specified in the struct. 13 // This simply ignores all additional fields at read time. 14 func (l *Logging) SetArbitrary(key string, val interface{}) error { 15 return nil 16 } 17 18 // DefaultLogging produces a new default logging configuration 19 func DefaultLogging() *Logging { 20 return &Logging{ 21 Levels: map[string]string{ 22 "qriapi": "info", 23 "qrip2p": "info", 24 }, 25 } 26 } 27 28 // Validate validates all fields of logging returning all errors found. 29 func (l Logging) Validate() error { 30 schema := jsonschema.Must(`{ 31 "$schema": "http://json-schema.org/draft-06/schema#", 32 "title": "logging", 33 "description": "Config for setting the level of logging output", 34 "type": "object", 35 "required": ["levels"], 36 "properties": { 37 "levels": { 38 "description": "Levels for logging output for a specific package", 39 "type": "object", 40 "patternProperties": { 41 "^qri": { 42 "type": "string", 43 "enum": [ 44 "info", 45 "error", 46 "debug", 47 "warn" 48 ] 49 } 50 } 51 } 52 } 53 }`) 54 return validate(schema, &l) 55 } 56 57 // Copy returns a deep copy of a Logging struct 58 func (l *Logging) Copy() *Logging { 59 res := &Logging{} 60 if l.Levels != nil { 61 res.Levels = map[string]string{} 62 for key, value := range l.Levels { 63 res.Levels[key] = value 64 } 65 } 66 return res 67 }