OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 package gologger | |
6 | |
7 import ( | |
8 "golang.org/x/net/context" | |
9 "os" | |
10 | |
11 gol "github.com/op/go-logging" | |
12 | |
13 "infra/libs/logging" | |
14 ) | |
15 | |
16 const fmt string = "%{color}" + | |
17 "[P%{pid} %{time:15:04:05.000} %{shortfile} %{level:.4s} %{id:03x}]" + | |
18 "%{color:reset} %{message}" | |
19 | |
20 type loggerImpl struct { | |
21 l *gol.Logger | |
22 } | |
23 | |
24 func (l *loggerImpl) Infof(format string, args ...interface{}) { l.l.Info(for
mat, args...) } | |
25 func (l *loggerImpl) Warningf(format string, args ...interface{}) { l.l.Warning(
format, args...) } | |
26 func (l *loggerImpl) Errorf(format string, args ...interface{}) { l.l.Error(fo
rmat, args...) } | |
27 | |
28 // UseFile adds a go-logging logger to the context which writes to the provided | |
29 // file. Caller is still responsible for closing the file when no longer needed. | |
30 func UseFile(c context.Context, f *os.File) context.Context { | |
31 backend := gol.NewLogBackend(f, "", 0) | |
32 formatted := gol.NewBackendFormatter(backend, gol.MustStringFormatter(fm
t)) | |
33 gol.SetBackend(formatted) | |
34 log := &loggerImpl{gol.MustGetLogger("")} | |
35 log.l.ExtraCalldepth = 1 // one layer of wrapping in loggerImpl struct a
bove | |
36 return logging.Set(c, func(context.Context) logging.Logger { return log
}) | |
37 } | |
38 | |
39 // Use adds a go-logging logger to the context which writes to os.Stdout. | |
40 func Use(c context.Context) context.Context { | |
41 return UseFile(c, os.Stdout) | |
42 } | |
43 | |
44 // Get returns default go-logging based logger. | |
45 func Get() logging.Logger { | |
46 return logging.Get(Use(context.Background())) | |
47 } | |
OLD | NEW |