예제 #1
0
파일: sync_test.go 프로젝트: basvanbeek/kit
func TestSyncLoggerConcurrency(t *testing.T) {
	var w io.Writer
	w = &bytes.Buffer{}
	logger := log.NewLogfmtLogger(w)
	logger = log.NewSyncLogger(logger)
	testConcurrency(t, logger, 10000)
}
예제 #2
0
파일: main.go 프로젝트: wolfeidau/sensord
func main() {

	// setup a logger
	logger = log.NewLogfmtLogger(os.Stderr)
	logger = log.NewContext(logger).With("svc", "sensor").With("caller", log.DefaultCaller)

	// setup rethinkdb session
	session := getSession()

	// build the context
	ctx := context.Background()

	// new service
	svc := NewSensorService(session)

	// wrap it in the logging middleware
	svc = loggingMiddleware(logger)(svc)

	// bind the service to HTTP with the context
	// with it's matching encoder/decoder
	recordHandler := httptransport.NewServer(
		ctx,
		makeRecordEndpoint(svc),
		decodeRecordRequest,
		encodeResponse,
	)

	// assign an endpoint route
	http.Handle("/sensor/record", recordHandler)

	// bind the listener
	logger.Log("msg", "HTTP", "addr", ":5000")
	logger.Log("err", http.ListenAndServe(":5000", nil))
}
예제 #3
0
func Example() {

	// make a normal, go-kit Logger
	logger := kitlog.NewLogfmtLogger(os.Stdout)
	// wrap it with serial awesome sauce
	logger = NewSerialLogger(logger, 50)

	var wg sync.WaitGroup
	wg.Add(threadCount)

	// spawn threadCount goroutines to log concurrently
	for i := 0; i < threadCount; i++ {
		go func() {
			defer wg.Done()

			slice := KVs()
			// log my data printCount times
			for j := 0; j < printCount; j++ {
				logger.Log(slice...)
			}
		}()
	}

	wg.Wait()

	logger.(SerialLogger).Flush()
}
예제 #4
0
func main() {
	// `package log` domain
	var logger kitlog.Logger
	logger = kitlog.NewLogfmtLogger(os.Stderr)
	logger = kitlog.NewContext(logger).With("ts", kitlog.DefaultTimestampUTC)
	stdlog.SetOutput(kitlog.NewStdlibAdapter(logger)) // redirect stdlib logging to us
	stdlog.SetFlags(0)                                // flags are handled in our logger

	// read configuration from environment
	c, err := config.LoadConfiguration()
	if err != nil {
		logger.Log("fatal", err.Error())
		return
	}

	// Mechanical stuff
	rand.Seed(time.Now().UnixNano())
	root := context.Background()
	errc := make(chan error)

	go func() {
		errc <- interrupt()
	}()

	// Start bindings
	binding.StartApplicationSQSConsumer(logger, root, errc, c)
	binding.StartHealthCheckHTTPListener(logger, root, errc, c)

	logger.Log("fatal", <-errc)
}
예제 #5
0
파일: log_test.go 프로젝트: ryancox/kit
func TestContext(t *testing.T) {
	t.Parallel()
	buf := &bytes.Buffer{}
	logger := log.NewLogfmtLogger(buf)

	kvs := []interface{}{"a", 123}
	lc := log.NewContext(logger).With(kvs...)
	kvs[1] = 0 // With should copy its key values

	lc = lc.With("b", "c") // With should stack
	if err := lc.Log("msg", "message"); err != nil {
		t.Fatal(err)
	}
	if want, have := "a=123 b=c msg=message\n", buf.String(); want != have {
		t.Errorf("\nwant: %shave: %s", want, have)
	}

	buf.Reset()
	lc = lc.WithPrefix("p", "first")
	if err := lc.Log("msg", "message"); err != nil {
		t.Fatal(err)
	}
	if want, have := "p=first a=123 b=c msg=message\n", buf.String(); want != have {
		t.Errorf("\nwant: %shave: %s", want, have)
	}
}
예제 #6
0
func Example_context() {
	logger := log.NewLogfmtLogger(os.Stdout)

	type Task struct {
		ID  int
		Cmd string
	}

	taskHelper := func(cmd string, logger log.Logger) {
		// execute(cmd)
		logger.Log("cmd", cmd, "dur", 42*time.Millisecond)
	}

	RunTask := func(task Task, logger log.Logger) {
		logger = log.NewContext(logger).With("taskID", task.ID)
		logger.Log("event", "starting task")

		taskHelper(task.Cmd, logger)

		logger.Log("event", "task complete")
	}

	RunTask(Task{ID: 1, Cmd: "echo Hello, world!"}, logger)

	// Output:
	// taskID=1 event="starting task"
	// taskID=1 cmd="echo Hello, world!" dur=42ms
	// taskID=1 event="task complete"
}
예제 #7
0
func TestLogfmtLogger(t *testing.T) {
	t.Parallel()
	buf := &bytes.Buffer{}
	logger := log.NewLogfmtLogger(buf)

	if err := logger.Log("hello", "world"); err != nil {
		t.Fatal(err)
	}
	if want, have := "hello=world\n", buf.String(); want != have {
		t.Errorf("want %#v, have %#v", want, have)
	}

	buf.Reset()
	if err := logger.Log("a", 1, "err", errors.New("error")); err != nil {
		t.Fatal(err)
	}
	if want, have := "a=1 err=error\n", buf.String(); want != have {
		t.Errorf("want %#v, have %#v", want, have)
	}

	buf.Reset()
	if err := logger.Log("std_map", map[int]int{1: 2}, "my_map", mymap{0: 0}); err != nil {
		t.Fatal(err)
	}
	if want, have := "std_map=\""+logfmt.ErrUnsupportedValueType.Error()+"\" my_map=special_behavior\n", buf.String(); want != have {
		t.Errorf("want %#v, have %#v", want, have)
	}
}
예제 #8
0
파일: levels_test.go 프로젝트: cnicolov/kit
func TestDefaultLevels(t *testing.T) {
	buf := bytes.Buffer{}
	logger := levels.New(log.NewLogfmtLogger(&buf))

	logger.Debug().Log("msg", "résumé") // of course you'd want to do this
	if want, have := "level=debug msg=résumé\n", buf.String(); want != have {
		t.Errorf("want %#v, have %#v", want, have)
	}

	buf.Reset()
	logger.Info().Log("msg", "Åhus")
	if want, have := "level=info msg=Åhus\n", buf.String(); want != have {
		t.Errorf("want %#v, have %#v", want, have)
	}

	buf.Reset()
	logger.Error().Log("msg", "© violation")
	if want, have := "level=error msg=\"© violation\"\n", buf.String(); want != have {
		t.Errorf("want %#v, have %#v", want, have)
	}

	buf.Reset()
	logger.Crit().Log("msg", "	")
	if want, have := "level=crit msg=\"\\t\"\n", buf.String(); want != have {
		t.Errorf("want %#v, have %#v", want, have)
	}
}
예제 #9
0
파일: main.go 프로젝트: andrewl/bikefinder
func main() {
	logger = log.NewLogfmtLogger(os.Stderr)
	logger = log.NewContext(logger).With("ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller)
	logger.Log("msg", "Starting bikefinder")
	var err error

	db_url := os.Getenv("DATABASE_URL")
	DB, err = crud.Connect("mysql", db_url)

	if err != nil {
		logger.Log("msg", "Failed to connect to db at "+db_url, "error", err)
		panic(err)
	}

	err = DB.CreateTables(DockingStationStatus{})
	if err != nil {
		logger.Log("msg", "Failed to create db table", "error", err)
		panic(err)
	}

	http.HandleFunc("/schemes", getSchemes)
	http.HandleFunc("/stations", getStationsInside)
	http.HandleFunc("/bikes-near", getBikesNear)
	http.HandleFunc("/freedocks-near", getFreeDocksNear)
	http.HandleFunc("/map", getMap)
	http.HandleFunc("/ingest", ingest)
	http.Handle("/", http.FileServer(http.Dir("./static")))
	bind := fmt.Sprintf("%s:%s", os.Getenv("OPENSHIFT_GO_IP"), os.Getenv("OPENSHIFT_GO_PORT"))
	logger.Log("msg", "Attempting to listen on "+bind)
	err = http.ListenAndServe(bind, nil)
	if err != nil {
		logger.Log("msg", "Failed to listen", "error", err)
		panic(err)
	}
}
예제 #10
0
func main() {
	logger := log.NewLogfmtLogger(os.Stdout)

	ctx := context.Background()

	c := &countService{}

	var svc endpoint.Endpoint
	svc = makeAddEndpoint(c)

	limit := ratelimit.NewBucket(2*time.Second, 1)
	svc = kitratelimit.NewTokenBucketLimiter(limit)(svc)

	requestCount := expvar.NewCounter("request.count")
	svc = metricsMiddleware(requestCount)(svc)
	svc = loggingMiddlware(logger)(svc)

	addHandler := httptransport.NewServer(
		ctx,
		svc,
		decodeAddRequest,
		encodeResponse,
		httptransport.ServerBefore(beforeIDExtractor, beforePATHExtractor),
	)

	http.Handle("/add", addHandler)

	port := os.Getenv("PORT")
	logger.Log("listening on", port)
	if err := http.ListenAndServe(":"+port, nil); err != nil {
		logger.Log("listen.error", err)
	}
}
예제 #11
0
func TestStringifyLogger(t *testing.T) {
	buf := &bytes.Buffer{}
	logger := log.NewLogfmtLogger(buf)
	logger = kithlp.StringifyLogger{logger}

	if err := logger.Log("hello", "world"); err != nil {
		t.Fatal(err)
	}
	if want, have := "hello=world\n", buf.String(); want != have {
		t.Errorf("want %#v, have %#v", want, have)
	}

	buf.Reset()
	if err := logger.Log("a", 1, "err", errors.New("error")); err != nil {
		t.Fatal(err)
	}
	if want, have := "a=1 err=error\n", buf.String(); want != have {
		t.Errorf("want %#v, have %#v", want, have)
	}

	buf.Reset()
	if err := logger.Log("std_map", map[int]int{1: 2}, "my_map", mymap{0: 0}); err != nil {
		t.Fatal(err)
	}
	if want, have := "std_map=map[1:2] my_map=special_behavior\n", buf.String(); want != have {
		t.Errorf("want %#v, have %#v", want, have)
	}
}
예제 #12
0
func main() {
	logger := log.NewLogfmtLogger(os.Stderr)
	logger = log.NewContext(logger).With("ts", log.DefaultTimestampUTC)

	err := godotenv.Load()
	if err != nil {
		logger.Log("level", "fatal", "err", err)
	}

	config := &Config{
		Addr: os.Getenv("ADDR"),
		DSN:  os.Getenv("DSN"),
	}

	db, err := gorm.Open("postgres", config.DSN)
	if err != nil {
		panic(err)
	}

	app := cli.NewApp()

	app.Commands = []cli.Command{{
		Name:   "serve",
		Action: serve(logger, config, db),
	}}

	if err := app.Run(os.Args); err != nil {
		logger.Log("level", "fatal", "err", err)
	}

}
예제 #13
0
func TestCatalogueServiceTags(t *testing.T) {
	logger = log.NewLogfmtLogger(os.Stderr)
	db, mock, err := sqlmock.New()
	if err != nil {
		t.Fatalf("an error '%s' was not expected when opening stub database connection", err)
	}
	defer db.Close()
	sqlxDB := sqlx.NewDb(db, "sqlmock")

	var cols []string = []string{"name"}

	mock.ExpectQuery("SELECT name FROM tag").WillReturnRows(sqlmock.NewRows(cols).
		AddRow(tags[0]).
		AddRow(tags[1]).
		AddRow(tags[2]))

	s := NewCatalogueService(sqlxDB, logger)

	have, err := s.Tags()
	if err != nil {
		t.Errorf("Tags(): %v", err)
	}
	if !reflect.DeepEqual(tags, have) {
		t.Errorf("Tags(): want %v, have %v", tags, have)
	}
}
예제 #14
0
func TestTrackTrackCargo(t *testing.T) {
	var (
		cargos         = repository.NewInMemcargo()
		handlingEvents = repository.NewInMemHandlingEvent()
		service        = NewService(cargos, handlingEvents)
	)

	c := cargo.New("TEST", cargo.RouteSpecification{
		Origin:          "SESTO",
		Destination:     "FIHEL",
		ArrivalDeadline: time.Date(2005, 12, 4, 0, 0, 0, 0, time.UTC),
	})
	cargos.Store(c)

	ctx := context.Background()

	logger := log.NewLogfmtLogger(ioutil.Discard)

	h := MakeHandler(ctx, service, logger)

	req, _ := http.NewRequest("GET", "http://example.com/tracking/v1/cargos/TEST", nil)
	rec := httptest.NewRecorder()

	h.ServeHTTP(rec, req)

	if rec.Code != http.StatusOK {
		t.Errorf("rec.Code = %d; want = %d", rec.Code, http.StatusOK)
	}

	if content := rec.Header().Get("Content-Type"); content != "application/json; charset=utf-8" {
		t.Errorf("Content-Type = %q; want = %q", content, "application/json; charset=utf-8")
	}

	var response trackCargoResponse
	if err := json.NewDecoder(rec.Body).Decode(&response); err != nil {
		t.Error(err)
	}

	if response.Err != nil {
		t.Errorf("response.Err = %q", response.Err)
	}

	var eta time.Time

	want := Cargo{
		TrackingID:           "TEST",
		Origin:               "SESTO",
		Destination:          "FIHEL",
		ArrivalDeadline:      time.Date(2005, 12, 4, 0, 0, 0, 0, time.UTC),
		ETA:                  eta.In(time.UTC),
		StatusText:           "Not received",
		NextExpectedActivity: "There are currently no expected activities for this cargo.",
		Events:               nil,
	}

	if !reflect.DeepEqual(want, *response.Cargo) {
		t.Errorf("response.Cargo = %#v; want = %#v", response.Cargo, want)
	}
}
예제 #15
0
func main() {
	var svc stringsvc.StringService = StringService{}
	l := log.NewLogfmtLogger(os.Stderr)
	svc = logging.Middleware(l, svc)(svc)

	trans.ServersForEndpoints(svc)
	http.ListenAndServe(":9000", nil)
}
예제 #16
0
파일: requests.go 프로젝트: homemade/justin
// StructuredLogger provide a minimal structured Logger implementation with the provided io.Writer
// Uses go-kit log to write in Logfmt
// See https://github.com/go-kit/kit/tree/master/log
func StructuredLogger(w io.Writer) Logger {
	l := gokitlog.NewLogfmtLogger(w)
	var logger LoggerFunc
	logger = func(c Call) {
		l.Log("msg", "calling api", "origin_id", c.OriginID, "duration", c.TimeTaken, "method", c.CalleeID, "request", fmt.Sprintf("%#v", c.Req), "request_body", c.ReqBody, "response", fmt.Sprintf("%#v", c.Res), "response_body", c.ResBody, "error", c.Err)
	}
	return logger
}
예제 #17
0
파일: main.go 프로젝트: cnicolov/kit
func main() {
	var (
		listen = flag.String("listen", ":8080", "HTTP listen address")
		proxy  = flag.String("proxy", "", "Optional comma-separated list of URLs to proxy uppercase requests")
	)
	flag.Parse()

	var logger log.Logger
	logger = log.NewLogfmtLogger(os.Stderr)
	logger = log.NewContext(logger).With("listen", *listen).With("caller", log.DefaultCaller)

	ctx := context.Background()

	fieldKeys := []string{"method", "error"}
	requestCount := kitprometheus.NewCounter(stdprometheus.CounterOpts{
		Namespace: "my_group",
		Subsystem: "string_service",
		Name:      "request_count",
		Help:      "Number of requests received.",
	}, fieldKeys)
	requestLatency := metrics.NewTimeHistogram(time.Microsecond, kitprometheus.NewSummary(stdprometheus.SummaryOpts{
		Namespace: "my_group",
		Subsystem: "string_service",
		Name:      "request_latency_microseconds",
		Help:      "Total duration of requests in microseconds.",
	}, fieldKeys))
	countResult := kitprometheus.NewSummary(stdprometheus.SummaryOpts{
		Namespace: "my_group",
		Subsystem: "string_service",
		Name:      "count_result",
		Help:      "The result of each count method.",
	}, []string{})

	var svc StringService
	svc = stringService{}
	svc = proxyingMiddleware(*proxy, ctx, logger)(svc)
	svc = loggingMiddleware(logger)(svc)
	svc = instrumentingMiddleware(requestCount, requestLatency, countResult)(svc)

	uppercaseHandler := httptransport.NewServer(
		ctx,
		makeUppercaseEndpoint(svc),
		decodeUppercaseRequest,
		encodeResponse,
	)
	countHandler := httptransport.NewServer(
		ctx,
		makeCountEndpoint(svc),
		decodeCountRequest,
		encodeResponse,
	)

	http.Handle("/uppercase", uppercaseHandler)
	http.Handle("/count", countHandler)
	http.Handle("/metrics", stdprometheus.Handler())
	logger.Log("msg", "HTTP", "addr", *listen)
	logger.Log("err", http.ListenAndServe(*listen, nil))
}
예제 #18
0
// New returns a new Weavebox object
func New() *Weavebox {
	return &Weavebox{
		router:          httprouter.New(),
		Output:          os.Stderr,
		ErrorHandler:    defaultErrorHandler,
		EnableAccessLog: false,
		logger:          kitlog.NewLogfmtLogger(os.Stderr),
	}
}
예제 #19
0
파일: levels_test.go 프로젝트: apsanz/kit
func ExampleLevels() {
	logger := levels.New(log.NewLogfmtLogger(os.Stdout))
	logger.Debug("msg", "hello")
	logger.With("context", "foo").Warn("err", "error")

	// Output:
	// level=debug msg=hello
	// level=warn context=foo err=error
}
예제 #20
0
func main() {
	ctx := context.Background()

	logger := log.NewLogfmtLogger(os.Stderr)

	fieldKeys := []string{"method", "error"}
	requestCount := kitprometheus.NewCounter(stdprometheus.CounterOpts{
		Namespace: "my_group",
		Subsystem: "string_service",
		Name:      "request_count",
		Help:      "Number of requests received.",
	}, fieldKeys)
	requestLatency := metrics.NewTimeHistogram(time.Microsecond, kitprometheus.NewSummary(stdprometheus.SummaryOpts{
		Namespace: "my_group",
		Subsystem: "string_service",
		Name:      "request_latency_microseconds",
		Help:      "Total duration of requests in microseconds.",
	}, fieldKeys))
	countResult := kitprometheus.NewSummary(stdprometheus.SummaryOpts{
		Namespace: "my_group",
		Subsystem: "string_service",
		Name:      "count_result",
		Help:      "The result of each count method.",
	}, []string{}) // no fields here

	var svc StringService
	svc = stringService{}
	svc = loggingMiddleware{logger, svc}
	svc = instrumentingMiddleware{requestCount, requestLatency, countResult, svc}

	var uppercase endpoint.Endpoint
	uppercase = makeUppercaseEndpoint(svc)

	var count endpoint.Endpoint
	count = makeCountEndpoint(svc)

	uppercaseHandler := httptransport.Server{
		Context:            ctx,
		Endpoint:           uppercase,
		DecodeRequestFunc:  decodeUppercaseRequest,
		EncodeResponseFunc: encodeResponse,
	}

	countHandler := httptransport.Server{
		Context:            ctx,
		Endpoint:           count,
		DecodeRequestFunc:  decodeCountRequest,
		EncodeResponseFunc: encodeResponse,
	}

	http.Handle("/uppercase", uppercaseHandler)
	http.Handle("/count", countHandler)
	http.Handle("/metrics", stdprometheus.Handler())

	stdlog.Fatal(http.ListenAndServe(":8080", nil))
}
예제 #21
0
func main() {
	ctx := context.Background()
	logger := log.NewLogfmtLogger(os.Stderr)

	//Define metrics to be collected  for this service
	fieldKeys := []string{"method", "error"}
	requestCount := kitprometheus.NewCounter(stdprometheus.CounterOpts{
		Namespace: "my_group",
		Subsystem: "string_service",
		Name:      "request_count",
		Help:      "Number of requests received.",
	}, fieldKeys)

	requestLatency := metrics.NewTimeHistogram(time.Microsecond, kitprometheus.NewSummary(stdprometheus.SummaryOpts{
		Namespace: "my_group",
		Subsystem: "string_service",
		Name:      "request_latency_microseconds",
		Help:      "Total duration of requests in Microseconds.",
	}, fieldKeys))

	countResult := kitprometheus.NewSummary(stdprometheus.SummaryOpts{
		Namespace: "my_group",
		Subsystem: "string_service",
		Name:      "count_result",
		Help:      "The result of eac count method",
	}, []string{})

	//Declare the string service
	var svc StringService
	svc = stringService{}
	svc = loggingMiddleware{logger, svc}
	svc = instrumentingMiddleware{requestCount, requestLatency, countResult, svc}

	//Declare and define a http server that exposes the string service to clients via httptransport
	uppercaseHandler := httptransport.NewServer(
		ctx,
		makeUppercaseEndpoint(svc),
		decodeUppercaseRequest,
		encodeResponse,
	)

	countHandler := httptransport.NewServer(
		ctx,
		makeCountEndpoint(svc),
		decodeCountRequest,
		encodeResponse,
	)

	//Define the content routes
	http.Handle("/uppercase", uppercaseHandler)
	http.Handle("/count", countHandler)
	http.Handle("/metrics", stdprometheus.Handler())
	logger.Log("msg", "HTTP", "addr", ":8080")
	logger.Log("err", http.ListenAndServe(":8080", nil))
}
예제 #22
0
func ExampleLog() {
	logger := leveledLogger.New(log.NewLogfmtLogger(os.Stdout))
	logger.Debug("msg", "hello")
	logger = logger.With("context", "foo")
	logger.Error("error", "error message")
	logger.Error("error", "another error message")

	// Output:
	// level=debug msg=hello
	// context=foo level=error error="error message"
	// context=foo level=error error="another error message"
}
예제 #23
0
func TestDoLog(t *testing.T) {
	var (
		buf      = new(bytes.Buffer)
		logger   = leveledLogger.New(log.NewLogfmtLogger(buf))
		expected = "level=info msg=\"message string\" fileline=file:line error=\"some error string\"\n"
		got      string
	)
	logger.Info("msg", "message string", "fileline", "file:line", "error", "some error string")
	got = buf.String()
	if got != expected {
		t.Errorf("Got incorrect log message:\nexpected %q\n but got %q\n", expected, got)
	}
}
예제 #24
0
func newColorLogger(t testing.TB, w io.Writer) log.Logger {
	return term.NewColorLogger(log.NewLogfmtLogger(w),
		"a", term.ColorOption{Value: func(_ interface{}) term.FgBgColor {
			return term.FgBgColor{Fg: term.Green, Bg: term.Default}
		}},
		"err", term.ColorOption{Value: func(err interface{}) term.FgBgColor {
			if err == nil {
				return term.FgBgColor{}
			}
			return term.FgBgColor{Fg: term.White, Bg: term.Red}
		}},
	)
}
예제 #25
0
파일: main.go 프로젝트: basvanbeek/kit
func main() {
	ctx := context.Background()
	logger := log.NewLogfmtLogger(os.Stderr)

	fieldKeys := []string{"method", "error"}
	requestCount := kitprometheus.NewCounterFrom(stdprometheus.CounterOpts{
		Namespace: "my_group",
		Subsystem: "string_service",
		Name:      "request_count",
		Help:      "Number of requests received.",
	}, fieldKeys)
	requestLatency := kitprometheus.NewSummaryFrom(stdprometheus.SummaryOpts{
		Namespace: "my_group",
		Subsystem: "string_service",
		Name:      "request_latency_microseconds",
		Help:      "Total duration of requests in microseconds.",
	}, fieldKeys)
	countResult := kitprometheus.NewSummaryFrom(stdprometheus.SummaryOpts{
		Namespace: "my_group",
		Subsystem: "string_service",
		Name:      "count_result",
		Help:      "The result of each count method.",
	}, []string{}) // no fields here

	var svc StringService
	svc = stringService{}
	svc = loggingMiddleware{logger, svc}
	svc = instrumentingMiddleware{requestCount, requestLatency, countResult, svc}

	uppercaseHandler := httptransport.NewServer(
		ctx,
		makeUppercaseEndpoint(svc),
		decodeUppercaseRequest,
		encodeResponse,
	)

	countHandler := httptransport.NewServer(
		ctx,
		makeCountEndpoint(svc),
		decodeCountRequest,
		encodeResponse,
	)

	http.Handle("/uppercase", uppercaseHandler)
	http.Handle("/count", countHandler)
	http.Handle("/metrics", stdprometheus.Handler())
	logger.Log("msg", "HTTP", "addr", ":8080")
	logger.Log("err", http.ListenAndServe(":8080", nil))
}
예제 #26
0
func TestLevelContext(t *testing.T) {
	var buf bytes.Buffer

	// Wrapping the level logger with a context allows users to use
	// log.DefaultCaller as per normal.
	var logger log.Logger
	logger = log.NewLogfmtLogger(&buf)
	logger = level.New(logger, level.Config{Allowed: level.AllowAll()})
	logger = log.NewContext(logger).With("caller", log.DefaultCaller)

	level.Info(logger).Log("foo", "bar")
	if want, have := `caller=level_test.go:134 level=info foo=bar`, strings.TrimSpace(buf.String()); want != have {
		t.Errorf("want %q, have %q", want, have)
	}
}
예제 #27
0
func TestContextLevel(t *testing.T) {
	var buf bytes.Buffer

	// Wrapping a context with the level logger still works, but requires users
	// to specify a higher callstack depth value.
	var logger log.Logger
	logger = log.NewLogfmtLogger(&buf)
	logger = log.NewContext(logger).With("caller", log.Caller(5))
	logger = level.New(logger, level.Config{Allowed: level.AllowAll()})

	level.Info(logger).Log("foo", "bar")
	if want, have := `caller=level_test.go:150 level=info foo=bar`, strings.TrimSpace(buf.String()); want != have {
		t.Errorf("want %q, have %q", want, have)
	}
}
예제 #28
0
func ExampleContext() {
	logger := log.NewLogfmtLogger(os.Stdout)
	logger.Log("foo", 123)
	ctx := log.NewContext(logger).With("level", "info")
	ctx.Log()
	ctx = ctx.With("msg", "hello")
	ctx.Log()
	ctx.With("a", 1).Log("b", 2)

	// Output:
	// foo=123
	// level=info
	// level=info msg=hello
	// level=info msg=hello a=1 b=2
}
예제 #29
0
파일: log_test.go 프로젝트: seastar-io/log
func TestLogOnce(t *testing.T) {
	var buf []byte
	out := bytes.NewBuffer(buf)
	logger := kitlog.NewLogfmtLogger(out)
	logger = NewSerialLogger(logger, 50)

	logger.Log("a", 1, "b", "c")

	logger.(SerialLogger).Flush()

	expected := "a=1 b=c\n"
	got := out.String()
	if got != expected {
		t.Errorf("Expected log statement [%v] but got [%v]", expected, got)
	}
}
예제 #30
0
func TestDeregister(t *testing.T) {
	for _, tc := range deregisterTestTable {
		c := Client(&testClient{tc.deregisterRes})
		buf := &bytes.Buffer{}
		logger := log.NewLogfmtLogger(buf)
		r := NewRegistrar(
			c,
			testService,
			logger,
		)
		r.Deregister()
		if want, have := tc.log, buf.String(); want != have {
			t.Fatalf("want %v, have %v", want, have)
		}
	}
}