| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package logging | 5 package logging |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "bytes" | 8 "bytes" |
| 9 "fmt" | 9 "fmt" |
| 10 "sort" | 10 "sort" |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 ret[k] = v | 55 ret[k] = v |
| 56 } | 56 } |
| 57 for k, v := range other { | 57 for k, v := range other { |
| 58 ret[k] = v | 58 ret[k] = v |
| 59 } | 59 } |
| 60 return ret | 60 return ret |
| 61 } | 61 } |
| 62 | 62 |
| 63 // SortedEntries processes a Fields object, pruning invisible fields, placing | 63 // SortedEntries processes a Fields object, pruning invisible fields, placing |
| 64 // the ErrorKey field first, and then sorting the remaining fields by key. | 64 // the ErrorKey field first, and then sorting the remaining fields by key. |
| 65 // | 65 func (f Fields) SortedEntries() (s []*FieldEntry) { |
| 66 // If prune is true, internally-used field keys will be pruned from the output | |
| 67 // if present. | |
| 68 func (f Fields) SortedEntries(prune bool) (s []*FieldEntry) { | |
| 69 if len(f) == 0 { | 66 if len(f) == 0 { |
| 70 return nil | 67 return nil |
| 71 } | 68 } |
| 72 s = make([]*FieldEntry, 0, len(f)) | 69 s = make([]*FieldEntry, 0, len(f)) |
| 73 for k, v := range f { | 70 for k, v := range f { |
| 74 » » if !(prune && k == FilterOnKey) { | 71 » » s = append(s, &FieldEntry{k, v}) |
| 75 » » » s = append(s, &FieldEntry{k, v}) | |
| 76 » » } | |
| 77 } | 72 } |
| 78 sort.Sort(fieldEntrySlice(s)) | 73 sort.Sort(fieldEntrySlice(s)) |
| 79 return | 74 return |
| 80 } | 75 } |
| 81 | 76 |
| 82 // FieldString returns a string describing the contents of f in a sorted, | 77 // String returns a string describing the contents of f in a sorted, |
| 83 // dictionary-like format. | 78 // dictionary-like format. |
| 84 // | 79 func (f Fields) String() string { |
| 85 // If prune is true, pruned fields will be omitted from the resulting string. | |
| 86 func (f Fields) FieldString(prune bool) string { | |
| 87 b := bytes.Buffer{} | 80 b := bytes.Buffer{} |
| 88 b.WriteRune('{') | 81 b.WriteRune('{') |
| 89 » for idx, e := range f.SortedEntries(prune) { | 82 » for idx, e := range f.SortedEntries() { |
| 90 if idx > 0 { | 83 if idx > 0 { |
| 91 b.WriteString(", ") | 84 b.WriteString(", ") |
| 92 } | 85 } |
| 93 b.WriteString(e.String()) | 86 b.WriteString(e.String()) |
| 94 } | 87 } |
| 95 b.WriteRune('}') | 88 b.WriteRune('}') |
| 96 return b.String() | 89 return b.String() |
| 97 } | 90 } |
| 98 | 91 |
| 99 // String returns a full string representation of Fields. This should not be | |
| 100 // used for logging otuput, as it doesn't prune fields. | |
| 101 func (f Fields) String() string { | |
| 102 return f.FieldString(false) | |
| 103 } | |
| 104 | |
| 105 // Debugf is a shorthand method to call the current logger's Errorf method. | 92 // Debugf is a shorthand method to call the current logger's Errorf method. |
| 106 func (f Fields) Debugf(c context.Context, fmt string, args ...interface{}) { | 93 func (f Fields) Debugf(c context.Context, fmt string, args ...interface{}) { |
| 107 Get(SetFields(c, f)).LogCall(Debug, 1, fmt, args) | 94 Get(SetFields(c, f)).LogCall(Debug, 1, fmt, args) |
| 108 } | 95 } |
| 109 | 96 |
| 110 // Infof is a shorthand method to call the current logger's Errorf method. | 97 // Infof is a shorthand method to call the current logger's Errorf method. |
| 111 func (f Fields) Infof(c context.Context, fmt string, args ...interface{}) { | 98 func (f Fields) Infof(c context.Context, fmt string, args ...interface{}) { |
| 112 Get(SetFields(c, f)).LogCall(Info, 1, fmt, args) | 99 Get(SetFields(c, f)).LogCall(Info, 1, fmt, args) |
| 113 } | 100 } |
| 114 | 101 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 // GetFields returns the current Fields. | 179 // GetFields returns the current Fields. |
| 193 // | 180 // |
| 194 // This method is used for logger implementations with the understanding that | 181 // This method is used for logger implementations with the understanding that |
| 195 // the returned fields must not be mutated. | 182 // the returned fields must not be mutated. |
| 196 func GetFields(c context.Context) Fields { | 183 func GetFields(c context.Context) Fields { |
| 197 if ret, ok := c.Value(fieldsKey).(Fields); ok { | 184 if ret, ok := c.Value(fieldsKey).(Fields); ok { |
| 198 return ret | 185 return ret |
| 199 } | 186 } |
| 200 return nil | 187 return nil |
| 201 } | 188 } |
| OLD | NEW |