logger

package module
v0.0.0-...-febee65 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2018 License: MIT Imports: 6 Imported by: 35

README

go-logger

Logging library in GO, used for structural logging inside UPP (mostly required for monitoring publish events). It is based on the Logrus implementation.

Initialization

When working with this logger library, please make sure you use one of the init methods first (otherwise the logging will fail):

  • InitLogger - requires a serviceName and a logLevel
  • InitDefaultLogger - requires only the serviceName as a parameter

Note: You can still create your own standard logger by using the NewLogger function (check Logrus for more details).

Logging a Monitoring Event

The library is Logrus compatible, but it includes a few default fields, which help facilitate the monitoring of key application events (@time, transaction_id and service_name). You can add a monitoring event to a log entry by using the following method:

  • WithMonitoringEvent - with transaction_id, eventName and contentType as parameters. A monitoring_event=true field will also be added to the entry. This message will be picked up by the monitoring services and dashboards.
Adding additional fields to the Entry

Beside the With... fields offered by the original Logrus Entry, the following methods can be used:

  • WithTransactionID, to add a transaction ID to the log entry;
  • WithUUID, to add a UUID to the log entry;
  • WithTime, to set a custom time of the logging entry (this can be used to influence Splunk log time);
  • WithValidFlag to mark if a message received by an application is valid or not. Invalid messages will be ignored by some of the monitoring statistics (SLAs).
Actual Logging

Use Logrus' default log methods, like Info, Warn, Error and others.

Examples

A monitoring log for a successful publish, with validation flag, can look like this:

logger.WithMonitoringEvent("Map", tid, "Annotations")
      .WithUUID(uuid)
      .WithValidFlag(true)
      .Info("Successfully mapped")

A monitoring log for a failed publish would log it as an error:

logger.WithMonitoringEvent("Map", tid, "Annotations")
      .WithUUID(uuid)
      .WithValidFlag(true)
      .WithError(err)
      .Error("Error decoding body")
Test Package

The test package has been introduced to check through unit tests that the application is logging relevant events properly. The example below shows how to check that an application is logging a specific monitoring event:

import (
    ...
    logTest "github.com/Financial-Times/go-logger/test"
    ...
)

func TestSomething(t *testing.T) {
    hook := logTest.NewTestHook("serviceName")
    ...
    Something()
    ...
    entry := hook.LastEntry()
    test.Assert(t, entry).HasMonitoringEvent("Map", "tid_test", "annotations").HasValidFlag(true)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(args ...interface{})

Debug logs a message at level Debug on the standard logger.

func Debugf

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

Debugf logs a message at level Debug on the standard logger.

func Debugln

func Debugln(args ...interface{})

Debugln logs a message at level Debug on the standard logger.

func Error

func Error(args ...interface{})

Error logs a message at level Error on the standard logger.

func Errorf

func Errorf(format string, args ...interface{})

Errorf logs a message at level Error on the standard logger.

func Errorln

func Errorln(args ...interface{})

Errorln logs a message at level Error on the standard logger.

func Fatal

func Fatal(args ...interface{})

Fatal logs a message at level Fatal on the standard logger.

func Fatalf

func Fatalf(format string, args ...interface{})

Fatalf logs a message at level Fatal on the standard logger.

func Fatalln

func Fatalln(args ...interface{})

Fatalln logs a message at level Fatal on the standard logger.

func Info

func Info(args ...interface{})

Info logs a message at level Info on the standard logger.

func Infof

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

Infof logs a message at level Info on the standard logger.

func Infoln

func Infoln(args ...interface{})

Infoln logs a message at level Info on the standard logger.

func InitDefaultLogger

func InitDefaultLogger(serviceName string)

func InitLogger

func InitLogger(serviceName string, logLevel string)

func Logger

func Logger() *logrus.Logger

func Panic

func Panic(args ...interface{})

Panic logs a message at level Panic on the standard logger.

func Panicf

func Panicf(format string, args ...interface{})

Panicf logs a message at level Panic on the standard logger.

func Panicln

func Panicln(args ...interface{})

Panicln logs a message at level Panic on the standard logger.

func Print

func Print(args ...interface{})

Print logs a message at level Info on the standard logger.

func Printf

func Printf(format string, args ...interface{})

Printf logs a message at level Info on the standard logger.

func Println

func Println(args ...interface{})

Println logs a message at level Info on the standard logger.

func ServiceStartedEvent

func ServiceStartedEvent(port int)

func Warn

func Warn(args ...interface{})

Warn logs a message at level Warn on the standard logger.

func Warnf

func Warnf(format string, args ...interface{})

Warnf logs a message at level Warn on the standard logger.

func Warning

func Warning(args ...interface{})

Warning logs a message at level Warn on the standard logger.

func Warningf

func Warningf(format string, args ...interface{})

Warningf logs a message at level Warn on the standard logger.

func Warningln

func Warningln(args ...interface{})

Warningln logs a message at level Warn on the standard logger.

func Warnln

func Warnln(args ...interface{})

Warnln logs a message at level Warn on the standard logger.

Types

type LogEntry

type LogEntry interface {
	logrus.FieldLogger
	WithUUID(uuid string) LogEntry
	WithValidFlag(isValid bool) LogEntry
	WithTime(time time.Time) LogEntry
	WithTransactionID(tid string) LogEntry
}

func WithError

func WithError(err error) LogEntry

WithError creates an entry from the standard logger and adds an error to it, using the value defined in ErrorKey as key.

func WithField

func WithField(key string, value interface{}) LogEntry

WithField creates an entry from the standard logger and adds a field to it. If you want multiple fields, use `WithFields`.

Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal or Panic on the Entry it returns.

func WithFields

func WithFields(fields map[string]interface{}) LogEntry

WithFields creates an entry from the standard logger and adds multiple fields to it. This is simply a helper for `WithField`, invoking it once for each field.

Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal or Panic on the Entry it returns.

func WithMonitoringEvent

func WithMonitoringEvent(eventName, tid, contentType string) LogEntry

func WithTransactionID

func WithTransactionID(tid string) LogEntry

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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