log

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 23, 2020 License: BSD-3-Clause Imports: 8 Imported by: 48

Documentation

Overview

Package log implements the Mute logging framework.

See https://github.com/cihub/seelog/wiki/Log-levels for an introduction to the different logging levels.

We want to log all error conditions in Mute, but want to avoid logging them multiple times. Therefore, we log them once as early as possible: When calling external packages that create an error, we wrap that error in a log.Error() call. If we create our own errors, we use log.Error[f]() to do that. If we call panic() we create the error for that with log.Critical[f](). In server packages we might wrap and create errors with log.Warn[f]() instead of log.Error[f](), because the server doesn't handle the error himself and passes it on to a client. On the client the error is wrapped with log.Error(), because it comes from an external source.

Example (Critical)

This example shows when and how to use the critical log level.

package main

import (
	"github.com/mutecomm/mute/log"
)

func main() {
	alwaysFalseCondition := false
	// ...
	if alwaysFalseCondition {
		panic(log.Critical("package name: this condition should never be true"))
	}
}
Output:

Example (Error)

This example shows when and how to use the error log level.

package main

import (
	"os"

	"github.com/mutecomm/mute/log"
)

func main() {
	conditionWhichShouldBeTrue := true
	// ...

	// create own error
	if !conditionWhichShouldBeTrue {
		log.Error("package name: condition should be true")
	}

	// calling external package which can produce an error
	_, err := os.Create("filename")
	if err != nil {
		log.Error(err)
	}
}
Output:

Example (Info)

This example shows when and how to use the info log level.

package main

import (
	"github.com/mutecomm/mute/log"
)

func main() {
	// server receives message
	log.Info("server: message received")
}
Output:

Example (Warn)

This example shows when and how to use the warn log level.

package main

import (
	"github.com/mutecomm/mute/log"
)

func main() {
	expiryCondition := true
	// ...

	// check condition in server package, error is not handled on the server
	if !expiryCondition {
		log.Warnf("server: token has expired")
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Critical

func Critical(v ...interface{}) error

Critical formats message using the default formats for its operands and writes to default logger with log level = Critical.

func Criticalf

func Criticalf(format string, params ...interface{}) error

Criticalf formats message according to format specifier and writes to default logger with log level = Critical.

func Debug

func Debug(v ...interface{})

Debug formats message using the default formats for its operands and writes to default logger with log level = Debug.

func Debugf

func Debugf(format string, params ...interface{})

Debugf formats message according to format specifier and writes to default logger with log level = Debug.

func Error

func Error(v ...interface{}) error

Error formats message using the default formats for its operands and writes to default logger with log level = Error.

func Errorf

func Errorf(format string, params ...interface{}) error

Errorf formats message according to format specifier and writes to default logger with log level = Error.

func Flush

func Flush()

Flush flushes all the messages in the logger.

func Info

func Info(v ...interface{})

Info formats message using the default formats for its operands and writes to default logger with log level = Info.

func Infof

func Infof(format string, params ...interface{})

Infof formats message according to format specifier and writes to default logger with log level = Info.

func Init

func Init(logLevel, cmdPrefix, logDir string, logToConsole bool) error

Init initializes the Mute logging framework to the given logging level. If logDir is not nil logging is done to a logfile in the directory. If logToConsole is true the console logging is activated. cmdPrefix must be a 5 character long command prefix. If the given level is invalid or the initialization fails, an error is returned.

func SetLogWriter

func SetLogWriter(writer io.Writer) error

SetLogWriter uses a specified io.Writer to output library log. Use this func if you are not using Seelog logging system in your app.

func Trace

func Trace(v ...interface{})

Trace formats message using the default formats for its operands and writes to default logger with log level = Trace.

func Tracef

func Tracef(format string, params ...interface{})

Tracef formats message according to format specifier and writes to default logger with log level = Trace.

func UseLogger

func UseLogger(newLogger seelog.LoggerInterface)

UseLogger uses a specified seelog.LoggerInterface to output library log. Use this func if you are using Seelog logging system in your app.

func Warn

func Warn(v ...interface{}) error

Warn formats message using the default formats for its operands and writes to default logger with log level = Warn.

func Warnf

func Warnf(format string, params ...interface{}) error

Warnf formats message according to format specifier and writes to default logger with log level = Warn.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL