logr.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. Copyright 2019 The logr Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // This design derives from Dave Cheney's blog:
  14. // http://dave.cheney.net/2015/11/05/lets-talk-about-logging
  15. // Package logr defines a general-purpose logging API and abstract interfaces
  16. // to back that API. Packages in the Go ecosystem can depend on this package,
  17. // while callers can implement logging with whatever backend is appropriate.
  18. //
  19. // Usage
  20. //
  21. // Logging is done using a Logger instance. Logger is a concrete type with
  22. // methods, which defers the actual logging to a LogSink interface. The main
  23. // methods of Logger are Info() and Error(). Arguments to Info() and Error()
  24. // are key/value pairs rather than printf-style formatted strings, emphasizing
  25. // "structured logging".
  26. //
  27. // With Go's standard log package, we might write:
  28. // log.Printf("setting target value %s", targetValue)
  29. //
  30. // With logr's structured logging, we'd write:
  31. // logger.Info("setting target", "value", targetValue)
  32. //
  33. // Errors are much the same. Instead of:
  34. // log.Printf("failed to open the pod bay door for user %s: %v", user, err)
  35. //
  36. // We'd write:
  37. // logger.Error(err, "failed to open the pod bay door", "user", user)
  38. //
  39. // Info() and Error() are very similar, but they are separate methods so that
  40. // LogSink implementations can choose to do things like attach additional
  41. // information (such as stack traces) on calls to Error().
  42. //
  43. // Verbosity
  44. //
  45. // Often we want to log information only when the application in "verbose
  46. // mode". To write log lines that are more verbose, Logger has a V() method.
  47. // The higher the V-level of a log line, the less critical it is considered.
  48. // Log-lines with V-levels that are not enabled (as per the LogSink) will not
  49. // be written. Level V(0) is the default, and logger.V(0).Info() has the same
  50. // meaning as logger.Info(). Negative V-levels have the same meaning as V(0).
  51. //
  52. // Where we might have written:
  53. // if flVerbose >= 2 {
  54. // log.Printf("an unusual thing happened")
  55. // }
  56. //
  57. // We can write:
  58. // logger.V(2).Info("an unusual thing happened")
  59. //
  60. // Logger Names
  61. //
  62. // Logger instances can have name strings so that all messages logged through
  63. // that instance have additional context. For example, you might want to add
  64. // a subsystem name:
  65. //
  66. // logger.WithName("compactor").Info("started", "time", time.Now())
  67. //
  68. // The WithName() method returns a new Logger, which can be passed to
  69. // constructors or other functions for further use. Repeated use of WithName()
  70. // will accumulate name "segments". These name segments will be joined in some
  71. // way by the LogSink implementation. It is strongly recommended that name
  72. // segments contain simple identifiers (letters, digits, and hyphen), and do
  73. // not contain characters that could muddle the log output or confuse the
  74. // joining operation (e.g. whitespace, commas, periods, slashes, brackets,
  75. // quotes, etc).
  76. //
  77. // Saved Values
  78. //
  79. // Logger instances can store any number of key/value pairs, which will be
  80. // logged alongside all messages logged through that instance. For example,
  81. // you might want to create a Logger instance per managed object:
  82. //
  83. // With the standard log package, we might write:
  84. // log.Printf("decided to set field foo to value %q for object %s/%s",
  85. // targetValue, object.Namespace, object.Name)
  86. //
  87. // With logr we'd write:
  88. // // Elsewhere: set up the logger to log the object name.
  89. // obj.logger = mainLogger.WithValues(
  90. // "name", obj.name, "namespace", obj.namespace)
  91. //
  92. // // later on...
  93. // obj.logger.Info("setting foo", "value", targetValue)
  94. //
  95. // Best Practices
  96. //
  97. // Logger has very few hard rules, with the goal that LogSink implementations
  98. // might have a lot of freedom to differentiate. There are, however, some
  99. // things to consider.
  100. //
  101. // The log message consists of a constant message attached to the log line.
  102. // This should generally be a simple description of what's occurring, and should
  103. // never be a format string. Variable information can then be attached using
  104. // named values.
  105. //
  106. // Keys are arbitrary strings, but should generally be constant values. Values
  107. // may be any Go value, but how the value is formatted is determined by the
  108. // LogSink implementation.
  109. //
  110. // Key Naming Conventions
  111. //
  112. // Keys are not strictly required to conform to any specification or regex, but
  113. // it is recommended that they:
  114. // * be human-readable and meaningful (not auto-generated or simple ordinals)
  115. // * be constant (not dependent on input data)
  116. // * contain only printable characters
  117. // * not contain whitespace or punctuation
  118. // * use lower case for simple keys and lowerCamelCase for more complex ones
  119. //
  120. // These guidelines help ensure that log data is processed properly regardless
  121. // of the log implementation. For example, log implementations will try to
  122. // output JSON data or will store data for later database (e.g. SQL) queries.
  123. //
  124. // While users are generally free to use key names of their choice, it's
  125. // generally best to avoid using the following keys, as they're frequently used
  126. // by implementations:
  127. // * "caller": the calling information (file/line) of a particular log line
  128. // * "error": the underlying error value in the `Error` method
  129. // * "level": the log level
  130. // * "logger": the name of the associated logger
  131. // * "msg": the log message
  132. // * "stacktrace": the stack trace associated with a particular log line or
  133. // error (often from the `Error` message)
  134. // * "ts": the timestamp for a log line
  135. //
  136. // Implementations are encouraged to make use of these keys to represent the
  137. // above concepts, when necessary (for example, in a pure-JSON output form, it
  138. // would be necessary to represent at least message and timestamp as ordinary
  139. // named values).
  140. //
  141. // Break Glass
  142. //
  143. // Implementations may choose to give callers access to the underlying
  144. // logging implementation. The recommended pattern for this is:
  145. // // Underlier exposes access to the underlying logging implementation.
  146. // // Since callers only have a logr.Logger, they have to know which
  147. // // implementation is in use, so this interface is less of an abstraction
  148. // // and more of way to test type conversion.
  149. // type Underlier interface {
  150. // GetUnderlying() <underlying-type>
  151. // }
  152. //
  153. // Logger grants access to the sink to enable type assertions like this:
  154. // func DoSomethingWithImpl(log logr.Logger) {
  155. // if underlier, ok := log.GetSink()(impl.Underlier) {
  156. // implLogger := underlier.GetUnderlying()
  157. // ...
  158. // }
  159. // }
  160. //
  161. // Custom `With*` functions can be implemented by copying the complete
  162. // Logger struct and replacing the sink in the copy:
  163. // // WithFooBar changes the foobar parameter in the log sink and returns a
  164. // // new logger with that modified sink. It does nothing for loggers where
  165. // // the sink doesn't support that parameter.
  166. // func WithFoobar(log logr.Logger, foobar int) logr.Logger {
  167. // if foobarLogSink, ok := log.GetSink()(FoobarSink); ok {
  168. // log = log.WithSink(foobarLogSink.WithFooBar(foobar))
  169. // }
  170. // return log
  171. // }
  172. //
  173. // Don't use New to construct a new Logger with a LogSink retrieved from an
  174. // existing Logger. Source code attribution might not work correctly and
  175. // unexported fields in Logger get lost.
  176. //
  177. // Beware that the same LogSink instance may be shared by different logger
  178. // instances. Calling functions that modify the LogSink will affect all of
  179. // those.
  180. package logr
  181. import (
  182. "context"
  183. )
  184. // New returns a new Logger instance. This is primarily used by libraries
  185. // implementing LogSink, rather than end users.
  186. func New(sink LogSink) Logger {
  187. logger := Logger{}
  188. logger.setSink(sink)
  189. sink.Init(runtimeInfo)
  190. return logger
  191. }
  192. // setSink stores the sink and updates any related fields. It mutates the
  193. // logger and thus is only safe to use for loggers that are not currently being
  194. // used concurrently.
  195. func (l *Logger) setSink(sink LogSink) {
  196. l.sink = sink
  197. }
  198. // GetSink returns the stored sink.
  199. func (l Logger) GetSink() LogSink {
  200. return l.sink
  201. }
  202. // WithSink returns a copy of the logger with the new sink.
  203. func (l Logger) WithSink(sink LogSink) Logger {
  204. l.setSink(sink)
  205. return l
  206. }
  207. // Logger is an interface to an abstract logging implementation. This is a
  208. // concrete type for performance reasons, but all the real work is passed on to
  209. // a LogSink. Implementations of LogSink should provide their own constructors
  210. // that return Logger, not LogSink.
  211. //
  212. // The underlying sink can be accessed through GetSink and be modified through
  213. // WithSink. This enables the implementation of custom extensions (see "Break
  214. // Glass" in the package documentation). Normally the sink should be used only
  215. // indirectly.
  216. type Logger struct {
  217. sink LogSink
  218. level int
  219. }
  220. // Enabled tests whether this Logger is enabled. For example, commandline
  221. // flags might be used to set the logging verbosity and disable some info logs.
  222. func (l Logger) Enabled() bool {
  223. return l.sink.Enabled(l.level)
  224. }
  225. // Info logs a non-error message with the given key/value pairs as context.
  226. //
  227. // The msg argument should be used to add some constant description to the log
  228. // line. The key/value pairs can then be used to add additional variable
  229. // information. The key/value pairs must alternate string keys and arbitrary
  230. // values.
  231. func (l Logger) Info(msg string, keysAndValues ...interface{}) {
  232. if l.Enabled() {
  233. if withHelper, ok := l.sink.(CallStackHelperLogSink); ok {
  234. withHelper.GetCallStackHelper()()
  235. }
  236. l.sink.Info(l.level, msg, keysAndValues...)
  237. }
  238. }
  239. // Error logs an error, with the given message and key/value pairs as context.
  240. // It functions similarly to Info, but may have unique behavior, and should be
  241. // preferred for logging errors (see the package documentations for more
  242. // information).
  243. //
  244. // The msg argument should be used to add context to any underlying error,
  245. // while the err argument should be used to attach the actual error that
  246. // triggered this log line, if present.
  247. func (l Logger) Error(err error, msg string, keysAndValues ...interface{}) {
  248. if withHelper, ok := l.sink.(CallStackHelperLogSink); ok {
  249. withHelper.GetCallStackHelper()()
  250. }
  251. l.sink.Error(err, msg, keysAndValues...)
  252. }
  253. // V returns a new Logger instance for a specific verbosity level, relative to
  254. // this Logger. In other words, V-levels are additive. A higher verbosity
  255. // level means a log message is less important. Negative V-levels are treated
  256. // as 0.
  257. func (l Logger) V(level int) Logger {
  258. if level < 0 {
  259. level = 0
  260. }
  261. l.level += level
  262. return l
  263. }
  264. // WithValues returns a new Logger instance with additional key/value pairs.
  265. // See Info for documentation on how key/value pairs work.
  266. func (l Logger) WithValues(keysAndValues ...interface{}) Logger {
  267. l.setSink(l.sink.WithValues(keysAndValues...))
  268. return l
  269. }
  270. // WithName returns a new Logger instance with the specified name element added
  271. // to the Logger's name. Successive calls with WithName append additional
  272. // suffixes to the Logger's name. It's strongly recommended that name segments
  273. // contain only letters, digits, and hyphens (see the package documentation for
  274. // more information).
  275. func (l Logger) WithName(name string) Logger {
  276. l.setSink(l.sink.WithName(name))
  277. return l
  278. }
  279. // WithCallDepth returns a Logger instance that offsets the call stack by the
  280. // specified number of frames when logging call site information, if possible.
  281. // This is useful for users who have helper functions between the "real" call
  282. // site and the actual calls to Logger methods. If depth is 0 the attribution
  283. // should be to the direct caller of this function. If depth is 1 the
  284. // attribution should skip 1 call frame, and so on. Successive calls to this
  285. // are additive.
  286. //
  287. // If the underlying log implementation supports a WithCallDepth(int) method,
  288. // it will be called and the result returned. If the implementation does not
  289. // support CallDepthLogSink, the original Logger will be returned.
  290. //
  291. // To skip one level, WithCallStackHelper() should be used instead of
  292. // WithCallDepth(1) because it works with implementions that support the
  293. // CallDepthLogSink and/or CallStackHelperLogSink interfaces.
  294. func (l Logger) WithCallDepth(depth int) Logger {
  295. if withCallDepth, ok := l.sink.(CallDepthLogSink); ok {
  296. l.setSink(withCallDepth.WithCallDepth(depth))
  297. }
  298. return l
  299. }
  300. // WithCallStackHelper returns a new Logger instance that skips the direct
  301. // caller when logging call site information, if possible. This is useful for
  302. // users who have helper functions between the "real" call site and the actual
  303. // calls to Logger methods and want to support loggers which depend on marking
  304. // each individual helper function, like loggers based on testing.T.
  305. //
  306. // In addition to using that new logger instance, callers also must call the
  307. // returned function.
  308. //
  309. // If the underlying log implementation supports a WithCallDepth(int) method,
  310. // WithCallDepth(1) will be called to produce a new logger. If it supports a
  311. // WithCallStackHelper() method, that will be also called. If the
  312. // implementation does not support either of these, the original Logger will be
  313. // returned.
  314. func (l Logger) WithCallStackHelper() (func(), Logger) {
  315. var helper func()
  316. if withCallDepth, ok := l.sink.(CallDepthLogSink); ok {
  317. l.setSink(withCallDepth.WithCallDepth(1))
  318. }
  319. if withHelper, ok := l.sink.(CallStackHelperLogSink); ok {
  320. helper = withHelper.GetCallStackHelper()
  321. } else {
  322. helper = func() {}
  323. }
  324. return helper, l
  325. }
  326. // contextKey is how we find Loggers in a context.Context.
  327. type contextKey struct{}
  328. // FromContext returns a Logger from ctx or an error if no Logger is found.
  329. func FromContext(ctx context.Context) (Logger, error) {
  330. if v, ok := ctx.Value(contextKey{}).(Logger); ok {
  331. return v, nil
  332. }
  333. return Logger{}, notFoundError{}
  334. }
  335. // notFoundError exists to carry an IsNotFound method.
  336. type notFoundError struct{}
  337. func (notFoundError) Error() string {
  338. return "no logr.Logger was present"
  339. }
  340. func (notFoundError) IsNotFound() bool {
  341. return true
  342. }
  343. // FromContextOrDiscard returns a Logger from ctx. If no Logger is found, this
  344. // returns a Logger that discards all log messages.
  345. func FromContextOrDiscard(ctx context.Context) Logger {
  346. if v, ok := ctx.Value(contextKey{}).(Logger); ok {
  347. return v
  348. }
  349. return Discard()
  350. }
  351. // NewContext returns a new Context, derived from ctx, which carries the
  352. // provided Logger.
  353. func NewContext(ctx context.Context, logger Logger) context.Context {
  354. return context.WithValue(ctx, contextKey{}, logger)
  355. }
  356. // RuntimeInfo holds information that the logr "core" library knows which
  357. // LogSinks might want to know.
  358. type RuntimeInfo struct {
  359. // CallDepth is the number of call frames the logr library adds between the
  360. // end-user and the LogSink. LogSink implementations which choose to print
  361. // the original logging site (e.g. file & line) should climb this many
  362. // additional frames to find it.
  363. CallDepth int
  364. }
  365. // runtimeInfo is a static global. It must not be changed at run time.
  366. var runtimeInfo = RuntimeInfo{
  367. CallDepth: 1,
  368. }
  369. // LogSink represents a logging implementation. End-users will generally not
  370. // interact with this type.
  371. type LogSink interface {
  372. // Init receives optional information about the logr library for LogSink
  373. // implementations that need it.
  374. Init(info RuntimeInfo)
  375. // Enabled tests whether this LogSink is enabled at the specified V-level.
  376. // For example, commandline flags might be used to set the logging
  377. // verbosity and disable some info logs.
  378. Enabled(level int) bool
  379. // Info logs a non-error message with the given key/value pairs as context.
  380. // The level argument is provided for optional logging. This method will
  381. // only be called when Enabled(level) is true. See Logger.Info for more
  382. // details.
  383. Info(level int, msg string, keysAndValues ...interface{})
  384. // Error logs an error, with the given message and key/value pairs as
  385. // context. See Logger.Error for more details.
  386. Error(err error, msg string, keysAndValues ...interface{})
  387. // WithValues returns a new LogSink with additional key/value pairs. See
  388. // Logger.WithValues for more details.
  389. WithValues(keysAndValues ...interface{}) LogSink
  390. // WithName returns a new LogSink with the specified name appended. See
  391. // Logger.WithName for more details.
  392. WithName(name string) LogSink
  393. }
  394. // CallDepthLogSink represents a Logger that knows how to climb the call stack
  395. // to identify the original call site and can offset the depth by a specified
  396. // number of frames. This is useful for users who have helper functions
  397. // between the "real" call site and the actual calls to Logger methods.
  398. // Implementations that log information about the call site (such as file,
  399. // function, or line) would otherwise log information about the intermediate
  400. // helper functions.
  401. //
  402. // This is an optional interface and implementations are not required to
  403. // support it.
  404. type CallDepthLogSink interface {
  405. // WithCallDepth returns a LogSink that will offset the call
  406. // stack by the specified number of frames when logging call
  407. // site information.
  408. //
  409. // If depth is 0, the LogSink should skip exactly the number
  410. // of call frames defined in RuntimeInfo.CallDepth when Info
  411. // or Error are called, i.e. the attribution should be to the
  412. // direct caller of Logger.Info or Logger.Error.
  413. //
  414. // If depth is 1 the attribution should skip 1 call frame, and so on.
  415. // Successive calls to this are additive.
  416. WithCallDepth(depth int) LogSink
  417. }
  418. // CallStackHelperLogSink represents a Logger that knows how to climb
  419. // the call stack to identify the original call site and can skip
  420. // intermediate helper functions if they mark themselves as
  421. // helper. Go's testing package uses that approach.
  422. //
  423. // This is useful for users who have helper functions between the
  424. // "real" call site and the actual calls to Logger methods.
  425. // Implementations that log information about the call site (such as
  426. // file, function, or line) would otherwise log information about the
  427. // intermediate helper functions.
  428. //
  429. // This is an optional interface and implementations are not required
  430. // to support it. Implementations that choose to support this must not
  431. // simply implement it as WithCallDepth(1), because
  432. // Logger.WithCallStackHelper will call both methods if they are
  433. // present. This should only be implemented for LogSinks that actually
  434. // need it, as with testing.T.
  435. type CallStackHelperLogSink interface {
  436. // GetCallStackHelper returns a function that must be called
  437. // to mark the direct caller as helper function when logging
  438. // call site information.
  439. GetCallStackHelper() func()
  440. }
  441. // Marshaler is an optional interface that logged values may choose to
  442. // implement. Loggers with structured output, such as JSON, should
  443. // log the object return by the MarshalLog method instead of the
  444. // original value.
  445. type Marshaler interface {
  446. // MarshalLog can be used to:
  447. // - ensure that structs are not logged as strings when the original
  448. // value has a String method: return a different type without a
  449. // String method
  450. // - select which fields of a complex type should get logged:
  451. // return a simpler struct with fewer fields
  452. // - log unexported fields: return a different struct
  453. // with exported fields
  454. //
  455. // It may return any value of any type.
  456. MarshalLog() interface{}
  457. }