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 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 func (s fieldEntrySlice) Swap(i, j int) { | 156 func (s fieldEntrySlice) Swap(i, j int) { |
157 s[i], s[j] = s[j], s[i] | 157 s[i], s[j] = s[j], s[i] |
158 } | 158 } |
159 | 159 |
160 func (s fieldEntrySlice) Len() int { | 160 func (s fieldEntrySlice) Len() int { |
161 return len(s) | 161 return len(s) |
162 } | 162 } |
163 | 163 |
164 // SetFields adds the additional fields as context for the current Logger. The | 164 // SetFields adds the additional fields as context for the current Logger. The |
165 // display of these fields depends on the implementation of the Logger. The | 165 // display of these fields depends on the implementation of the Logger. The |
166 // new Logger will contain the combination of its current Fields, updated with | 166 // new context will contain the combination of its current Fields, updated with |
167 // the new ones (see Fields.Copy). Specifying the new fields as nil will | 167 // the new ones (see Fields.Copy). Specifying the new fields as nil will |
168 // clear the currently set fields. | 168 // clear the currently set fields. |
169 func SetFields(c context.Context, fields Fields) context.Context { | 169 func SetFields(c context.Context, fields Fields) context.Context { |
170 return context.WithValue(c, fieldsKey, GetFields(c).Copy(fields)) | 170 return context.WithValue(c, fieldsKey, GetFields(c).Copy(fields)) |
171 } | 171 } |
172 | 172 |
173 // SetField is a convenience method for SetFields for a single key/value | 173 // SetField is a convenience method for SetFields for a single key/value |
174 // pair. | 174 // pair. |
175 func SetField(c context.Context, key string, value interface{}) context.Context
{ | 175 func SetField(c context.Context, key string, value interface{}) context.Context
{ |
176 return SetFields(c, Fields{key: value}) | 176 return SetFields(c, Fields{key: value}) |
177 } | 177 } |
178 | 178 |
179 // GetFields returns the current Fields. | 179 // GetFields returns the current Fields. |
180 // | 180 // |
181 // This method is used for logger implementations with the understanding that | 181 // This method is used for logger implementations with the understanding that |
182 // the returned fields must not be mutated. | 182 // the returned fields must not be mutated. |
183 func GetFields(c context.Context) Fields { | 183 func GetFields(c context.Context) Fields { |
184 if ret, ok := c.Value(fieldsKey).(Fields); ok { | 184 if ret, ok := c.Value(fieldsKey).(Fields); ok { |
185 return ret | 185 return ret |
186 } | 186 } |
187 return nil | 187 return nil |
188 } | 188 } |
OLD | NEW |