mirror of
https://github.com/foxcpp/maddy.git
synced 2025-04-06 22:47:37 +03:00
Rework how error inspection and logging is done
Previous error reporting code was inconsistent in terms of what is logged, when and by whom. This caused several problems such as: logs missing important error context, duplicated error messages, too verbose messages, etc. Instead of logging all generated errors, module should log only errors it 'handles' somehow and does not simply pass it to the caller. This removes duplication, however, also it removes context information. To fix this, exterrors package was extended to provide utilities for error wrapping. These utilities provide ability to 'attach' arbitrary key-value ('fields') pairs to any error object while preserving the original value (using to Go 1.13 error handling primitives). In additional to solving problems described above this commit makes logs machine-readable, creating the possibility for automated analysis. Three new functions were added to the Logger object, providing loosely-typed structured logging. However, they do not completely replace plain logging and are used only where they are useful (to allow automated analysis of message processing logs). So, basically, instead of being logged god knows where and when, all context information is attached to the error object and then it is passed up until it is handled somewhere, at this point it is logged together with all context information and then discarded.
This commit is contained in:
parent
8c178f7d76
commit
cf9e81d8a1
24 changed files with 714 additions and 341 deletions
51
exterrors/fields.go
Normal file
51
exterrors/fields.go
Normal file
|
@ -0,0 +1,51 @@
|
|||
package exterrors
|
||||
|
||||
type fieldsErr interface {
|
||||
Fields() map[string]interface{}
|
||||
}
|
||||
|
||||
type unwrapper interface {
|
||||
Unwrap() error
|
||||
}
|
||||
|
||||
type fieldsWrap struct {
|
||||
err error
|
||||
fields map[string]interface{}
|
||||
}
|
||||
|
||||
func (fw fieldsWrap) Error() string {
|
||||
return fw.err.Error()
|
||||
}
|
||||
|
||||
func (fw fieldsWrap) Unwrap() error {
|
||||
return fw.err
|
||||
}
|
||||
|
||||
func (fw fieldsWrap) Fields() map[string]interface{} {
|
||||
return fw.fields
|
||||
}
|
||||
|
||||
func Fields(err error) map[string]interface{} {
|
||||
fields := make(map[string]interface{}, 5)
|
||||
|
||||
for err != nil {
|
||||
errFields, ok := err.(fieldsErr)
|
||||
if ok {
|
||||
for k, v := range errFields.Fields() {
|
||||
fields[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
unwrap, ok := err.(unwrapper)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
err = unwrap.Unwrap()
|
||||
}
|
||||
|
||||
return fields
|
||||
}
|
||||
|
||||
func WithFields(err error, fields map[string]interface{}) error {
|
||||
return fieldsWrap{err: err, fields: fields}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue