logger

package module
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2019 License: MIT Imports: 6 Imported by: 70

README

go-logger

Logging library in Go, used for structured logging inside UPP. It is wrapper of logrus library for structrural logging in Go. As wrapper of logrus logger, the UPP logger provides the same functionality and extends it with a few UPP specific methods. It also enforces the aggreed logging format in UPP - JSON formatted logs and each of the log entries include the service name, log level and time if available.

As logrus logger implements the standard Go log interface, the UPP logger implements it as well. So the UPP logger can be used where standard Go log is required.

UPP logger shares the same log levels as logrus - debug, info, warning, error, fatal, panic.

Initialization

When working with this logger library, please use one of the init methods:

  • NewUPPLogger - requires a serviceName and a logLevel as parameters. Also there is additional optional parameter - configuration for the names of the field keys logged by the UPP logger methods.
  • NewUPPInfoLogger - requires only the serviceName as a parameter. Initializes logger with log level info. Also there is additional optional parameter - configuration for the names of the field keys logged by the UPP logger methods.
  • NewUnstructuredLogger - returns UPP logger but without enforced structured logging format.

Please note that using package level logger by only importing the library (supported in v1 of this library) is no longer available.

Logging with the UPP logger

UPP logger supports structured logging as logrus supports it. Please take a look at logging fields as logrus method for structured logging. UPP logrus also implements WithField and WithFields methods. As logrus UPPLogger also supports chaining of the methods that add logging fields.

For producing actual log, use the default log methods, like Info, Warn, Error and others.

Adding additional methods to the Entry and logger

Beside the With... fields offered by the original logrus Entry and logger, 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).
Logging events

The library includes methods which help facilitate the monitoring of key application events.

  • You can add a monitoring event to a log entry by using the following method: WithMonitoringEvent - with transaction ID, event name and content type 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.
  • You can add an event with category and message by using: WithCategorisedEvent - with event name, event category and event message as parameters. Using this method we are also able to produce log with particular structure easy to be picked up and parsed by a monitoring tool.
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

View Source
const (
	DefaultKeyLogLevel = logrus.FieldKeyLevel
	DefaultKeyMsg      = logrus.FieldKeyMsg
	DefaultKeyError    = "error"
	DefaultKeyTime     = "time"

	DefaultKeyServiceName   = "service_name"
	DefaultKeyTransactionID = "transaction_id"
	DefaultKeyUUID          = "uuid"
	DefaultKeyIsValid       = "isValid"

	DefaultKeyEventName       = "event"
	DefaultKeyMonitoringEvent = "monitoring_event"
	DefaultKeyContentType     = "content_type"
	DefaultKeyEventCategory   = "event_category"
	DefaultKeyEventMsg        = "event_msg"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type KeyNamesConfig

type KeyNamesConfig struct {
	KeyLogLevel string
	KeyMsg      string
	KeyError    string
	KeyTime     string

	KeyServiceName   string
	KeyTransactionID string
	KeyUUID          string
	KeyIsValid       string

	KeyEventName       string
	KeyMonitoringEvent string
	KeyContentType     string
	KeyEventCategory   string
	KeyEventMsg        string
}

func GetDefaultKeyNamesConfig

func GetDefaultKeyNamesConfig() *KeyNamesConfig

func GetFullKeyNameConfig

func GetFullKeyNameConfig(conf KeyNamesConfig) *KeyNamesConfig

GetFullKeyNameConfig returns KeyNamesConfig that has all key names from the input conf and if there are key names missing from the input conf, the default key names are used.

type LogEntry

type LogEntry struct {
	*logrus.Entry
	// contains filtered or unexported fields
}

LogEntry is wrapper around logrus.Entry with a few methods adding UPP specific keys to the log entries. LogEntry is the final or intermediate logging entry. It's finally logged when Debug, Info, Warn, Error, Fatal or Panic is called on it.

func (*LogEntry) WithCategorisedEvent

func (entry *LogEntry) WithCategorisedEvent(eventName, eventCategory, eventMsg, tid string) *LogEntry

WithCategorisedEvent returns new LogEntry with categorised event fields in it. The categorised event fields are event name, event category, event message.

func (*LogEntry) WithError

func (entry *LogEntry) WithError(err error) *LogEntry

WithError returns new LogEntry with error field in it.

func (*LogEntry) WithField

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

WithField for LogEntry is wrapper around WithField of logrus.Entry that let us return our object when chaining the calls to WithField.

func (*LogEntry) WithFields

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

WithFields for LogEntry is wrapper around WithFields of logrus.Entry that let us return our object when chaining the calls to WithFields.

func (*LogEntry) WithMonitoringEvent

func (entry *LogEntry) WithMonitoringEvent(eventName, tid, contentType string) *LogEntry

WithMonitoringEvent returns new LogEntry with monitoring event fields in it. The monitoring event fields are boolean valued monitoring event, event name and content type.

func (*LogEntry) WithTime

func (entry *LogEntry) WithTime(time time.Time) *LogEntry

WithTime returns new LogEntry with time field in it.

func (*LogEntry) WithTransactionID

func (entry *LogEntry) WithTransactionID(tid string) *LogEntry

WithTransactionID returns new LogEntry with transaction id field in it.

func (*LogEntry) WithUUID

func (entry *LogEntry) WithUUID(uuid string) *LogEntry

WithUUID returns new LogEntry with uuid field in it.

func (*LogEntry) WithValidFlag

func (entry *LogEntry) WithValidFlag(isValid bool) *LogEntry

WithValidFlag returns new LogEntry with "is valid" field in it.

type UPPLogger

type UPPLogger struct {
	*logrus.Logger
	// contains filtered or unexported fields
}

UPPLogger wraps logrus logger providing the same functionality as logrus with a few UPP specifics.

func NewUPPInfoLogger

func NewUPPInfoLogger(serviceName string, kconf ...KeyNamesConfig) *UPPLogger

NewUPPInfoLogger initializes UPPLogger with log level INFO.

func NewUPPLogger

func NewUPPLogger(serviceName string, logLevel string, kconf ...KeyNamesConfig) *UPPLogger

NewUPPLogger initializes UPP logger with structured logging format.

func NewUnstructuredLogger

func NewUnstructuredLogger() *UPPLogger

NewUnstructuredLogger initializes plain logrus log without taking into account UPP log formatting.

func (*UPPLogger) LogServiceStartedEvent

func (ulog *UPPLogger) LogServiceStartedEvent(port int)

LogServiceStartedEvent logs service started event with level INFO.

func (*UPPLogger) WithCategorisedEvent

func (ulog *UPPLogger) WithCategorisedEvent(eventName, eventCategory, eventMsg, tid string) *LogEntry

WithCategorisedEvent creates an entry from the standard logger and adds categorised event fields to it. The added fields are "event", "event_category" and "event_msg".

func (*UPPLogger) WithError

func (ulog *UPPLogger) WithError(err error) *LogEntry

WithError creates an entry from the standard logger and adds an error field to it.

func (*UPPLogger) WithField

func (ulog *UPPLogger) 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 (*UPPLogger) WithFields

func (ulog *UPPLogger) 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 (*UPPLogger) WithMonitoringEvent

func (ulog *UPPLogger) WithMonitoringEvent(eventName, tid, contentType string) *LogEntry

WithMonitoringEvent creates an entry from the standard logger and adds monitoring event fields to it. The monitoring event fields are "monitoring_event", "event" and "content_type".

func (*UPPLogger) WithTime

func (ulog *UPPLogger) WithTime(time time.Time) *LogEntry

WithTime creates an entry from the standard logger and adds an time field to it.

func (*UPPLogger) WithTransactionID

func (ulog *UPPLogger) WithTransactionID(tid string) *LogEntry

WithTransactionID creates an entry from the standard logger and adds transaction_id field to it.

func (*UPPLogger) WithUUID

func (ulog *UPPLogger) WithUUID(uuid string) *LogEntry

WithUUID creates an entry from the standard logger and adds an uuid field to it.

func (*UPPLogger) WithValidFlag

func (ulog *UPPLogger) WithValidFlag(isValid bool) *LogEntry

WithValidFlag creates an entry from the standard logger and adds an "is valid" field to it.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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