MCPcopy
hub / github.com/op/go-logging / Format

Method Format

format.go:274–334  ·  view source on GitHub ↗
(calldepth int, r *Record, output io.Writer)

Source from the content-addressed store, hash-verified

272}
273
274func (f *stringFormatter) Format(calldepth int, r *Record, output io.Writer) error {
275 for _, part := range f.parts {
276 if part.verb == fmtVerbStatic {
277 output.Write([]byte(part.layout))
278 } else if part.verb == fmtVerbTime {
279 output.Write([]byte(r.Time.Format(part.layout)))
280 } else if part.verb == fmtVerbLevelColor {
281 doFmtVerbLevelColor(part.layout, r.Level, output)
282 } else if part.verb == fmtVerbCallpath {
283 depth, err := strconv.Atoi(part.layout)
284 if err != nil {
285 depth = 0
286 }
287 output.Write([]byte(formatCallpath(calldepth+1, depth)))
288 } else {
289 var v interface{}
290 switch part.verb {
291 case fmtVerbLevel:
292 v = r.Level
293 break
294 case fmtVerbID:
295 v = r.ID
296 break
297 case fmtVerbPid:
298 v = pid
299 break
300 case fmtVerbProgram:
301 v = program
302 break
303 case fmtVerbModule:
304 v = r.Module
305 break
306 case fmtVerbMessage:
307 v = r.Message()
308 break
309 case fmtVerbLongfile, fmtVerbShortfile:
310 _, file, line, ok := runtime.Caller(calldepth + 1)
311 if !ok {
312 file = "???"
313 line = 0
314 } else if part.verb == fmtVerbShortfile {
315 file = filepath.Base(file)
316 }
317 v = fmt.Sprintf("%s:%d", file, line)
318 case fmtVerbLongfunc, fmtVerbShortfunc,
319 fmtVerbLongpkg, fmtVerbShortpkg:
320 // TODO cache pc
321 v = "???"
322 if pc, _, _, ok := runtime.Caller(calldepth + 1); ok {
323 if f := runtime.FuncForPC(pc); f != nil {
324 v = formatFuncName(part.verb, f.Name())
325 }
326 }
327 default:
328 panic("unhandled format part")
329 }
330 fmt.Fprintf(output, part.layout, v)
331 }

Callers

nothing calls this directly

Calls 5

formatCallpathFunction · 0.85
formatFuncNameFunction · 0.85
MessageMethod · 0.80
doFmtVerbLevelColorFunction · 0.70
FormatMethod · 0.65

Tested by

no test coverage detected