Skip to content

Instantly share code, notes, and snippets.

@hdonnay
Last active March 27, 2018 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hdonnay/66189d15f91de6dd0bf32a8e376f162e to your computer and use it in GitHub Desktop.
Save hdonnay/66189d15f91de6dd0bf32a8e376f162e to your computer and use it in GitHub Desktop.
logger idea
package tlog
// import (...)
func WithTags(ctx context.Context, pairs ...string) context.Context {
// turn these pairs into a bunch of [][2]string, shove into the Context
}
func Print(ctx context.Context, f string, v ...interface{}) {
tags := fromContext(ctx)
line := strings.Builder{}
// build line...
Output(2, line.String())
}
// etc...
type Logger struct {}
func New(...) *Logger {
// use the functional options pattern because we're breaking compatability with everything, hard
}
func (l *Logger) Print(ctx context.Context, f string, v ...interface{}) {
//...
}
func FromContext(ctx context.Context) *log.Logger {
// close over the provided context and provide a very unconfigurable logger, like the stackdriver package.
// A compatability niceity.
}
package main
func mydumbfunction(ctx context.Context) error {
ctx := tlog.WithTags(ctx, "today", "tuesday")
tlog.Println(ctx, "doing mydumbfunction")
// ...
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment