Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(FAQ): add zapfilter #939

Merged
merged 1 commit into from May 18, 2021
Merged

chore(FAQ): add zapfilter #939

merged 1 commit into from May 18, 2021

Commits on May 10, 2021

  1. Add zapfilter

    This utility allows creating a logger with "advanced" filters like in the following example.
    It is particularly convenient to use with a CLI flag package or using environment variables.
    
    	core := zap.NewExample().Core()
    	// *=myns             => any level, myns namespace
        // info,warn:myns.*   => info or warn level, any namespace matching myns.*
    	// error=*            => everything with error level
    	logger := zap.New(zapfilter.NewFilteringCore(core, zapfilter.MustParseRules("*:myns info,warn:myns.* error:*")))
    	defer logger.Sync()
    
    	logger.Debug("top debug")                                 // no match
    	logger.Named("myns").Debug("myns debug")                  // matches *:myns
    	logger.Named("bar").Debug("bar debug")                    // no match
    	logger.Named("myns").Named("foo").Debug("myns.foo debug") // no match
    
    	logger.Info("top info")                                 // no match
    	logger.Named("myns").Info("myns info")                  // matches *:myns
    	logger.Named("bar").Info("bar info")                    // no match
    	logger.Named("myns").Named("foo").Info("myns.foo info") // matches info,warn:myns.*
    
    	logger.Warn("top warn")                                 // no match
    	logger.Named("myns").Warn("myns warn")                  // matches *:myns
    	logger.Named("bar").Warn("bar warn")                    // no match
    	logger.Named("myns").Named("foo").Warn("myns.foo warn") // matches info,warn:myns.*
    
    	logger.Error("top error")                                 // matches error:*
    	logger.Named("myns").Error("myns error")                  // matches *:myns and error:*
    	logger.Named("bar").Error("bar error")                    // matches error:*
    	logger.Named("myns").Named("foo").Error("myns.foo error") // matches error:*
    
    	// Output:
    	// {"level":"debug","logger":"myns","msg":"myns debug"}
    	// {"level":"info","logger":"myns","msg":"myns info"}
    	// {"level":"info","logger":"myns.foo","msg":"myns.foo info"}
    	// {"level":"warn","logger":"myns","msg":"myns warn"}
    	// {"level":"warn","logger":"myns.foo","msg":"myns.foo warn"}
    	// {"level":"error","msg":"top error"}
    	// {"level":"error","logger":"myns","msg":"myns error"}
    	// {"level":"error","logger":"bar","msg":"bar error"}
    	// {"level":"error","logger":"myns.foo","msg":"myns.foo error"}
    moul committed May 10, 2021
    Copy the full SHA
    d9e56bb View commit details
    Browse the repository at this point in the history