| 32 | } |
| 33 | |
| 34 | func Example_sprintf() { |
| 35 | logger := logging.NewLogger("json") |
| 36 | // Set log level to info |
| 37 | logger = level.NewFilter(logger, level.AllowInfo()) |
| 38 | levelLogger := logging.WithLevel(logger) |
| 39 | |
| 40 | // Let's try to log some debug messages. They are filtered by log level, so you should see no output. |
| 41 | // The cost of fmt.Sprintf is paid event if the log is filtered out. This sometimes can be a huge performance downside. |
| 42 | levelLogger.Debugw("record some data", "data", fmt.Sprintf("%+v", []int{1, 2, 3})) |
| 43 | // Or better, we can use logging.Sprintf to avoid the cost if the log is not actually written to the output. |
| 44 | levelLogger.Debugw("record some data", "data", logging.Sprintf("%+v", []int{1, 2, 3})) |
| 45 | |
| 46 | // Output: |
| 47 | // |
| 48 | } |
| 49 | |
| 50 | func ExampleWithContext() { |
| 51 | bag, ctx := ctxmeta.Inject(context.Background()) |