Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(76)

Side by Side Diff: go/src/infra/libs/logging/gologger/gologger.go

Issue 1153883002: go: infra/libs/* now live in luci-go. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: move the rest too Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 }
OLDNEW
« no previous file with comments | « go/src/infra/libs/logging/gaelogger/gaelogger.infra_testing ('k') | go/src/infra/libs/logging/gologger/gologger.infra_testing » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698