| // Copyright (C) 2016 The Android Open Source Project |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| package log_test |
| |
| import ( |
| "fmt" |
| |
| "android.googlesource.com/platform/tools/gpu/framework/log" |
| ) |
| |
| func myNestedFunc(ctx log.Context, name string) { |
| ctx = ctx.Enter("nested") |
| ctx.Debug().S("name", name).Log("being questioned") |
| ctx.Info().Tag("answer").S("name", name).S("state", "hungry").Log("") |
| } |
| |
| func myFunc(ctx log.Context) { |
| ctx = ctx.Enter("printer") |
| ctx.Notice().Tag("question").Log("Who are you and what do you want?") |
| myNestedFunc(ctx, "George") |
| ctx.Warning().Fail(fmt.Errorf("Denial of food"), "George is angry") |
| } |
| |
| func Example_Simple() { |
| ctx := log.Background(). |
| PreFilter(log.Limit(log.InfoLevel)). |
| Filter(log.Pass). |
| Handler(log.Std(log.Normal)) |
| myFunc(ctx) |
| // Output: |
| // Notice:question:printer:Who are you and what do you want? |
| // Info:answer:printer->nested:name=George,state=hungry |
| // Warning:printer:Failed:George is angry<Denial of food> |
| } |
| |
| type normal struct{} |
| |
| func (normal) String() string { return "normal" } |
| |
| func Example_Variations() { |
| ctx := log.Background(). |
| PreFilter(log.Limit(log.InfoLevel)). |
| Filter(log.Pass). |
| Handler(log.Stdout(log.Normal)) |
| |
| ctx.At(log.InfoLevel).S("state", "long").Log("") |
| ctx.Info().WithValue("state", normal{}).Log("") |
| ctx.Critical().V("state", "late").Log("") |
| ctx.Debug().S("state", "ignored").Log("") |
| ctx.Notice().I("number", 1).Log("") |
| ctx.Warning().F("pi", 1.142).Log("") |
| ctx.Warning().S("message", `Such a long string, this will get truncated at some point, right?. Wait for it. Wait. |
| Ok not quite yet. Now maybe? |
| No, not quite. |
| How are you? The weather is pretty dull outside. I wonder what happened to summer. |
| My cat's breath smells of cat food. |
| Once a fella met a fella in a field of fitches. Said a fella to a fella, can a fella tell a fella where a fella itches? |
| Probably. |
| A bean, a bean, a half a bean, a bean, a bean and a half. |
| Oh what's that? Could it be the end of this string? |
| Yeah, I think it really might be. Horray!`). |
| Log("") |
| ctx.Warning().S("message", |
| "Such a long string, this will get truncated at some point, right?. Nope this one will just fit, just wait and see. The end."). |
| Log("") |
| ctx.Print("You are simple") |
| ctx.Printf("You are %s", "printf") |
| |
| // Output: |
| // Info:state=long |
| // Info:state=normal |
| // Critical:state=late |
| // Notice:number=1 |
| // Warning:pi=1.142 |
| // Warning:message=Such a long string, this will get truncated at some point, right?. Wait for it. Wait. |
| // Ok not quite yet. Now maybe? |
| // No, not quite. |
| // How are you? The weather is pretty dull outside. I wonder what happened to summer. |
| // My cat's breath smells of cat food. |
| // Once a fella met a fella in a field of fitches. Said a fella to a fella, can a fella tell a fella where a fella itches? |
| // Probably. |
| // A bean, a bean, a half a bean, a bean, a bean and a half. |
| // Oh what's that? Could it be the end of this string? |
| // Yeah, I think it really... |
| // Warning:message=Such a long string, this will get truncated at some point, right?. Nope this one will just fit, just wait and see. The end. |
| // Info:You are simple |
| // Info:You are printf |
| } |
| |
| func Example_Tagged() { |
| filter := func(ctx log.Context, key string, value interface{}) bool { |
| // custom tag filter |
| return key == log.TagKey && value == "ShowThis" |
| } |
| ctx := log.Background(). |
| PreFilter(log.Limit(log.DebugLevel)). |
| Filter(filter). |
| Handler(log.Stdout(log.Normal)) |
| |
| ctx.Debug().Tag("ShowThis").Log("Interesting start") |
| ctx.Info().Tag("NotThis").Log("Boring line") |
| ctx.Info().Tag("ShowThis").Log("Interesting end") |
| |
| // Output: |
| // Debug:ShowThis:Interesting start |
| // Info:ShowThis:Interesting end |
| } |