Exemplo n.º 1
0
func Example() {
	// everything logged needs a context
	ctx := context.Background()

	log.Info(ctx, "program started")
	if err := doFirstThing(ctx, 5, 4); err != nil {
		// ... error processing here ...
	}

	// YYYY-MM-DDTHH:MM:SS.FFFFFF info msg="program started"
	// YYYY-MM-DDTHH:MM:SS.FFFFFF error msg="cannot do third thing" error="error message goes here" a=5 b=4
}
Exemplo n.º 2
0
func ExampleNewWriter(ctx context.Context) {
	// Creates a HTTP server whose error log will write to the
	// default slog.Logger. Any panics that are recovered will
	// have the details logged via slog.
	httpServer := &http.Server{
		Addr:     ":8080",
		ErrorLog: log.New(slog.NewWriter(ctx), "http", 0),
	}

	slog.Info(ctx, "web server started", slog.WithValue("addr", httpServer.Addr))
	if err := httpServer.ListenAndServe(); err != nil {
		slog.Error(ctx, "web server failed", slog.WithError(err))
	}

	// 2009-11-10T12:34:56.789 info msg="web server started" addr=:8080
	// 2009-11-10T12:35:57:987 error msg="http: panic serving 123.1:2.3:36145 runtime error: invalid memory address or nil pointer dereference"
}