Documentation ¶
Overview ¶
Package golog implements logging functions that log errors to stderr and debug messages to stdout.
Trace logs go to stdout as well, but they are only written if a linker flag or an environment variable is set as such:
- The following linker flag is set
-X github.com/getlantern/golog.linkerFlagEnableTraceThroughLinker=true - Optionally, you can also set a comma-separated list of prefixes to trace through the following linker flag -X github.com/getlantern/golog.linkerFlagTracePrefixes=prefix1,prefix2 - Or, alternatively, trace logging can also be enable if "TRACE=true" environment variable is set - Optionally, you can also set a comma-separated list of prefixes to trace through the "TRACE" environment variable like this: "TRACE=prefix1,prefix2"
A stack dump will be printed after the message if "PRINT_STACK=true".
Index ¶
- Constants
- func DefaultOnFatal()
- func GetPrepender() func(io.Writer)
- func OnFatal(fn func(err error))
- func RegisterReporter(reporter ErrorReporter)
- func ResetOutputs()deprecated
- func ResetPrepender()
- func SetOutput(out Output) (reset func())
- func SetOutputs(errorOut io.Writer, debugOut io.Writer) (reset func())
- func SetPrepender(p func(io.Writer))
- type ErrorReporter
- type Event
- type Logger
- type MultiLine
- type Output
- type Severity
Constants ¶
const ( // ERROR is an error Severity ERROR = 500 // FATAL is an error Severity FATAL = 600 )
Variables ¶
This section is empty.
Functions ¶
func GetPrepender ¶
func OnFatal ¶
func OnFatal(fn func(err error))
OnFatal configures golog to call the given function on any FATAL error. By default, golog calls os.Exit(1) on any FATAL error.
func RegisterReporter ¶
func RegisterReporter(reporter ErrorReporter)
RegisterReporter registers the given ErrorReporter. All logged Errors are sent to this reporter.
func ResetOutputs
deprecated
func ResetOutputs()
Deprecated: instead of calling ResetOutputs, use the reset function returned by SetOutputs.
func ResetPrepender ¶
func ResetPrepender()
func SetOutput ¶
func SetOutput(out Output) (reset func())
SetOutput sets the Output to use for errors and debug messages
func SetOutputs ¶
SetOutputs sets the outputs for error and debug logs to use the given Outputs. Returns a function that resets outputs to their original values prior to calling SetOutputs. If env variable PRINT_JSON is set, use JSON output instead of plain text
func SetPrepender ¶
SetPrepender sets a function to write something, e.g., the timestamp, before each line of the log.
Types ¶
type ErrorReporter ¶
ErrorReporter is a function to which the logger will report errors. It the given error and corresponding message along with associated ops context. This should return quickly as it executes on the critical code path. The recommended approach is to buffer as much as possible and discard new reports if the buffer becomes saturated.
type Logger ¶
type Logger interface { // Debug logs to stdout Debug(arg interface{}) // Debugf logs to stdout Debugf(message string, args ...interface{}) // Error logs to stderr Error(arg interface{}) error // Errorf logs to stderr. It returns the first argument that's an error, or // a new error built using fmt.Errorf if none of the arguments are errors. Errorf(message string, args ...interface{}) error // Fatal logs to stderr and then exits with status 1 Fatal(arg interface{}) // Fatalf logs to stderr and then exits with status 1 Fatalf(message string, args ...interface{}) // Trace logs to stderr only if TRACE=true Trace(arg interface{}) // Tracef logs to stderr only if TRACE=true Tracef(message string, args ...interface{}) // TraceOut provides access to an io.Writer to which trace information can // be streamed. If running with environment variable "TRACE=true", TraceOut // will point to os.Stderr, otherwise it will point to a ioutil.Discared. // Each line of trace information will be prefixed with this Logger's // prefix. TraceOut() io.Writer // IsTraceEnabled() indicates whether or not tracing is enabled for this // logger. IsTraceEnabled() bool // AsDebugLogger returns an standard logger that writes Debug messages AsDebugLogger() *log.Logger // AsStdLogger returns an standard logger that writes Errors AsStdLogger() *log.Logger // AsErrorLogger returns an standard logger that writes Errors AsErrorLogger() *log.Logger }
type MultiLine ¶
type MultiLine interface { // MultiLinePrinter returns a function that can be used to print the // multi-line output. The returned function writes one line to the buffer and // returns true if there are more lines to write. This function does not need // to take care of trailing carriage returns, golog handles that // automatically. MultiLinePrinter() func(buf *bytes.Buffer) bool }
MultiLine is an interface for arguments that support multi-line output.
type Output ¶
type Output interface { // Write debug messages Debug(prefix string, skipFrames int, printStack bool, severity string, arg interface{}, values map[string]interface{}) // Write error messages Error(prefix string, skipFrames int, printStack bool, severity string, arg interface{}, values map[string]interface{}) }
Output is a log output that can optionally support structured logging
func JsonOutput ¶
JsonOutput creates an output that writes JSON structured log to different io.Writers for errors and debug
func TextOutput ¶
TextOutput creates an output that writes text to different io.Writers for errors and debug