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

Unified Diff: impl/prod/logger.go

Issue 1498963003: Move gaelogging from luci/luci-go to luci/gae (Closed) Base URL: https://github.com/luci/gae.git@master
Patch Set: rebase Created 5 years 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « impl/prod/everything_test.go ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: impl/prod/logger.go
diff --git a/impl/prod/logger.go b/impl/prod/logger.go
new file mode 100644
index 0000000000000000000000000000000000000000..2b8d9712fdf2cc2fbe35811a3ffea27883ac66a2
--- /dev/null
+++ b/impl/prod/logger.go
@@ -0,0 +1,67 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package prod
+
+import (
+ "fmt"
+
+ "github.com/luci/luci-go/common/logging"
+ "golang.org/x/net/context"
+ "google.golang.org/appengine/log"
+)
+
+// useLogging adds a logging.Logger implementation to the context which logs to
+// appengine's log handler.
+func useLogging(c context.Context) context.Context {
+ return logging.SetFactory(c, func(ic context.Context) logging.Logger {
+ return &loggerImpl{AEContext(ic)}
+ })
+}
+
+type loggerImpl struct {
+ aeCtx context.Context
+}
+
+func (gl *loggerImpl) Debugf(format string, args ...interface{}) {
+ gl.LogCall(logging.Debug, 1, format, args)
+}
+func (gl *loggerImpl) Infof(format string, args ...interface{}) {
+ gl.LogCall(logging.Info, 1, format, args)
+}
+func (gl *loggerImpl) Warningf(format string, args ...interface{}) {
+ gl.LogCall(logging.Warning, 1, format, args)
+}
+func (gl *loggerImpl) Errorf(format string, args ...interface{}) {
+ gl.LogCall(logging.Error, 1, format, args)
+}
+
+// TODO(riannucci): prefix with caller's code location.
+func (gl *loggerImpl) LogCall(l logging.Level, calldepth int, format string, args []interface{}) {
+ if gl.aeCtx == nil || !logging.IsLogging(gl.aeCtx, l) {
+ return
+ }
+
+ var logf func(context.Context, string, ...interface{})
+ switch l {
+ case logging.Debug:
+ logf = log.Debugf
+ case logging.Info:
+ logf = log.Infof
+ case logging.Warning:
+ logf = log.Warningf
+
+ case logging.Error:
+ fallthrough
+ default:
+ logf = log.Errorf
+ }
+
+ fields := logging.GetFields(gl.aeCtx)
+ if len(fields) > 0 {
+ logf(gl.aeCtx, "%s :: %s", fmt.Sprintf(format, args...), fields.FieldString(true))
+ } else {
+ logf(gl.aeCtx, format, args...)
+ }
+}
« no previous file with comments | « impl/prod/everything_test.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698