Codebase list golang-github-go-logr-logr / 26dc218
Rename WithTags to WithValues This renames the WithTags method to WithValues, and removes the use of the term "tag" from the documentation. It's replaced with the phrase "named values", and noted that "key" is used to refer to the name of a particular value. Solly Ross 5 years ago
5 changed file(s) with 31 addition(s) and 27 deletion(s). Raw diff Collapse all Expand all
1111 // It's terribly inefficient, and is *only* a basic example.
1212 type TabLogger struct{
1313 name string
14 tags map[string]interface{}
14 keyValues map[string]interface{}
1515
1616 writer *tabwriter.Writer
1717 }
2020
2121 func (l *TabLogger) Info(msg string, kvs ...interface{}) {
2222 fmt.Fprintf(l.writer, "%s\t%s\t", l.name, msg)
23 for k, v := range l.tags {
23 for k, v := range l.keyValues {
2424 fmt.Fprintf(l.writer, "%s: %+v ", k, v)
2525 }
2626 for i := 0; i < len(kvs); i += 2 {
4646 func (l *TabLogger) WithName(name string) logr.Logger {
4747 return &TabLogger{
4848 name: l.name+"."+name,
49 tags: l.tags,
49 keyValues: l.keyValues,
5050 writer: l.writer,
5151 }
5252 }
5353
54 func (l *TabLogger) WithTags(kvs ...interface{}) logr.Logger {
55 newMap := make(map[string]interface{}, len(l.tags)+len(kvs)/2)
56 for k, v := range l.tags {
54 func (l *TabLogger) WithValues(kvs ...interface{}) logr.Logger {
55 newMap := make(map[string]interface{}, len(l.keyValues)+len(kvs)/2)
56 for k, v := range l.keyValues {
5757 newMap[k] = v
5858 }
5959 for i := 0; i < len(kvs); i += 2 {
6161 }
6262 return &TabLogger{
6363 name: l.name,
64 tags: newMap,
64 keyValues: newMap,
6565 writer: l.writer,
6666 }
6767 }
9191 c.log.Info("starting reconciliation")
9292
9393 for key := c.client.WatchNext(); key != ""; key = c.client.WatchNext() {
94 // we can make more specific loggers if we always want to attach a particular tag
95 log := c.log.WithTags("key", key)
94 // we can make more specific loggers if we always want to attach a particular named value
95 log := c.log.WithValues("key", key)
9696
9797 // fetch our object
9898 obj, err := c.client.Get(key)
108108 }
109109
110110 // always log the object with log messages
111 log = log.WithTags("object", obj)
111 log = log.WithValues("object", obj)
112112 log.V(1).Info("reconciling object for key")
113113
114114 // Do some complicated updates updates
1212 //
1313 // Usage
1414 //
15 // Logging is done using a Logger. Loggers can have name prefixes and tags attached,
16 // so that all log messages logged with that Logger have some base context associated.
15 // Logging is done using a Logger. Loggers can have name prefixes and named values
16 // attached, so that all log messages logged with that Logger have some base context
17 // associated.
18 //
19 // The term "key" is used to refer to the name associated with a particular value, to
20 // disambiguate it from the general Logger name.
1721 //
1822 // For instance, suppose we're trying to reconcile the state of an object, and we want
1923 // to log that we've made some decision.
2529 //
2630 // With logr's structured logging, we'd write
2731 // // elsewhere in the file, set up the logger to log with the prefix of "reconcilers",
28 // // and the tag target-type=Foo, for extra context.
32 // // and the named value target-type=Foo, for extra context.
2933 // log := mainLogger.WithName("reconcilers").WithTag("target-type", "Foo")
3034 //
3135 // // later on...
4650 // This functions similarly to:
4751 // log.Info("unable to reconcile object", "error", err, "object", object)
4852 //
49 // However, it ensures that a standard tag ("error") is used across all error logging. Furthermore,
50 // certain implementations may choose to attach additional information (such as stack traces) on
51 // calls to Error, so it's preferred to use Error to log errors.
53 // However, it ensures that a standard key for the error value ("error") is used across all
54 // error logging. Furthermore, certain implementations may choose to attach additional
55 // information (such as stack traces) on calls to Error, so it's preferred to use Error
56 // to log errors.
5257 //
5358 // Parts of a log line
5459 //
5560 // Each log message from a Logger has four types of context:
56 // logger name, log verbosity, log message, and key-value pairs.
61 // logger name, log verbosity, log message, and the named values.
5762 //
58 // The Logger name is constists of a series of name "segments" added by successive calls to WithName.
63 // The Logger name constists of a series of name "segments" added by successive calls to WithName.
5964 // These name segments will be joined in some way by the underlying implementation. It is strongly
6065 // reccomended that name segements contain simple identifiers (letters, digits, and hyphen), and do
6166 // not contain characters that could muddle the log output or confuse the joining operation (e.g.
6873 // The log message consists of a constant message attached to the the log line. This
6974 // should generally be a simple description of what's occuring, and should never be a format string.
7075 //
71 // Variable information can then be attached using key/value pairs. Keys are arbitrary strings,
72 // and values may be any Go value.
76 // Variable information can then be attached using named values (key/value pairs). Keys are arbitrary
77 // strings, while values may be any Go value.
7378 package logr
7479
7580 // TODO: consider adding back in format strings if they're really needed
100105 InfoLogger
101106
102107 // Error logs an error, with the given message and key/value pairs as context.
103 // It functions similarly to calling Info with the "error" tag, but may have
104 // unique behavior, and should be preferred for logging errors (see the package
108 // It functions similarly to calling Info with the "error" named value, but may
105109 // documentations for more information).
106 //
110 // package documentations for more information).
107111 // The msg field should be used to add context to any underlying error,
108112 // while the err field should be used to attach the actual error that
109113 // triggered this log line, if present.
113117 // verbosity level means a log message is less important.
114118 V(level int) InfoLogger
115119
116 // WithTags adds some key-value pairs of context to a logger.
120 // WithValues adds some key-value pairs of context to a logger.
117121 // See Info for documentation on how key/value pairs work.
118 WithTags(keysAndValues ...interface{}) Logger
122 WithValues(keysAndValues ...interface{}) Logger
119123
120124 // WithName adds a new element to the logger's name.
121125 // Successive calls with WithName continue to append
2626 return log
2727 }
2828
29 func (log NullLogger) WithTags(_ ...interface{}) logr.Logger {
29 func (log NullLogger) WithValues(_ ...interface{}) logr.Logger {
3030 return log
3131 }
3333 return log
3434 }
3535
36 func (log TestLogger) WithTags(_ ...interface{}) logr.Logger {
36 func (log TestLogger) WithValues(_ ...interface{}) logr.Logger {
3737 return log
3838 }