blob: 7e93d24d2e7c568cd7d361a93db5b35672ced69d [file] [log] [blame]
Serge Bazanski5ade7322020-08-27 13:27:51 +02001// Copyright 2020 The Monogon Project Authors.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17package logtree
18
19// LeveledLogger is a generic interface for glog-style logging. There are four hardcoded log severities, in increasing
20// order: INFO, WARNING, ERROR, FATAL. Logging at a certain severity level logs not only to consumers expecting data
21// at that severity level, but also all lower severity levels. For example, an ERROR log will also be passed to
22// consumers looking at INFO or WARNING logs.
23type LeveledLogger interface {
24 // Info logs at the INFO severity. Arguments are handled in the manner of fmt.Print, a terminating newline is added
25 // if missing.
26 Info(args ...interface{})
27 // Infof logs at the INFO severity. Arguments are handled in the manner of fmt.Printf, a terminating newline is
28 // added if missing.
29 Infof(format string, args ...interface{})
30
31 // Warning logs at the WARNING severity. Arguments are handled in the manner of fmt.Print, a terminating newline is
32 // added if missing.
33 Warning(args ...interface{})
34 // Warningf logs at the WARNING severity. Arguments are handled in the manner of fmt.Printf, a terminating newline
35 // is added if missing.
36 Warningf(format string, args ...interface{})
37
38 // Error logs at the ERROR severity. Arguments are handled in the manner of fmt.Print, a terminating newline is
39 // added if missing.
40 Error(args ...interface{})
41 // Errorf logs at the ERROR severity. Arguments are handled in the manner of fmt.Printf, a terminating newline is
42 // added if missing.
43 Errorf(format string, args ...interface{})
44
45 // Fatal logs at the FATAL severity and aborts the current program. Arguments are handled in the manner of
46 // fmt.Print, a terminating newline is added if missing.
47 Fatal(args ...interface{})
48 // Fatalf logs at the FATAL severity and aborts the current program. Arguments are handled in the manner of
49 // fmt.Printf, a terminating newline is added if missing.
50 Fatalf(format string, args ...interface{})
51
52 // V returns a VerboseLeveledLogger at a given verbosity level. These verbosity levels can be dynamically set and
53 // unset on a package-granular level by consumers of the LeveledLogger logs. The returned value represents whether
54 // logging at the given verbosity level was active at that time, and as such should not be a long-lived object
55 // in programs.
56 // This construct is further refered to as 'V-logs'.
57 V(level VerbosityLevel) VerboseLeveledLogger
58}
59
60// VerbosityLevel is a verbosity level defined for V-logs. This can be changed programmatically per Go package. When
61// logging at a given VerbosityLevel V, the current level must be equal or higher to V for the logs to be recorded.
62// Conversely, enabling a V-logging at a VerbosityLevel V also enables all logging at lower levels [Int32Min .. (V-1)].
63type VerbosityLevel int32
64
65type VerboseLeveledLogger interface {
66 // Enabled returns if this level was enabled. If not enabled, all logging into this logger will be discarded
67 // immediately.
68 // Thus, Enabled() can be used to check the verbosity level before performing any logging:
69 // if l.V(3).Enabled() { l.Info("V3 is enabled") }
70 // or, in simple cases, the convenience function .Info can be used:
71 // l.V(3).Info("V3 is enabled")
72 // The second form is shorter and more convenient, but more expensive, as its arguments are always evaluated.
73 Enabled() bool
74 // Info is the equivalent of a LeveledLogger's Info call, guarded by whether this VerboseLeveledLogger is enabled.
75 Info(args ...interface{})
76 // Infof is the equivalent of a LeveledLogger's Infof call, guarded by whether this VerboseLeveledLogger is enabled.
77 Infof(format string, args ...interface{})
78}