logging

package
v5.3.3 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2021 License: Apache-2.0 Imports: 10 Imported by: 0

README

logging Package

The logging package provides objects and methods for logging output to the standard output console. It allows consumers to output in various formats, including text and JSON. It also supports log levels and color.

Package Methods

Below is a reference of exported methods in this package.

LogFactory(logFormat LogFormat, applicationName string, minimumLogLevel LogType) ILogger

LogFactory should be your primary point of entry. This is a factory method that, when told what format, log level, and application name is, will return the correct logger object. For example, to get a simple logger with a minimum log level output of INFO:

var logger logging.ILogger
logger = logging.LogFactory(logging.LOG\_FORMAT\_SIMPLE, "Test Application", logging.INFO)
NewJSONLogger(applicationName string, minimumLogLevel LogType) *JSONLogger

NewJSONLogger returns an instance of a new logger that outputs in JSON format. Logs written in JSON format output an object to the console. Here is an example of what that looks like. Note that an actual log entry would be a single line.

{
	"applicationName": "Test Application",
	"type": "INFO",
	"message": "Database connection established"
}
NewSimpleLogger(applicationName string, minimumLogLevel LogType) *SimpleLogger

NewSimpleLogger returns an instance of a new logger that outputs plain text. Here is an example of what that looks like.

Test Application: INFO - Database connection established
StringToLogFormat(logFormatName string) LogFormat

StringToLogFormat takes a string and attempts to convert it to a LogFormat type. A log format represents the format the logger will output. Currently valid values are

  • SIMPLE
  • JSON
StringToLogType(logTypeName string) LogType

StringToLogType takes a string and attempts to convert it to a LogType. A log type specifies the types of log entries, such as an error, or information entry. The following are the supported log types.

  • DEBUG
  • INFO
  • WARN
  • ERROR
  • FATAL

Documentation

Overview

* Copyright (c) 2020. App Nerds LLC. All rights reserved

* Copyright (c) 2020. App Nerds LLC. All rights reserved

* Copyright (c) 2020. App Nerds LLC. All rights reserved

* Copyright (c) 2020. App Nerds LLC. All rights reserved

* Copyright (c) 2020. App Nerds LLC. All rights reserved

* Copyright (c) 2020. App Nerds LLC. All rights reserved

* Copyright (c) 2020. App Nerds LLC. All rights reserved

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Hook

func Hook() echo.MiddlewareFunc

func HookWithExisting

func HookWithExisting(existingLogger *logrus.Entry) echo.MiddlewareFunc

func HookWithLogger

func HookWithLogger(existingLogger *logrus.Logger) echo.MiddlewareFunc

Types

type ILogger

type ILogger interface {
	Debugf(message string, args ...interface{})
	DisableColors()
	EnableColors()
	Errorf(message string, args ...interface{})
	Infof(message string, args ...interface{})
}

ILogger is an interface that describes how a logger should behave

func LogFactory

func LogFactory(logFormat LogFormat, applicationName string, minimumLogLevel LogType) ILogger

LogFactory returns a logger in the required format

type JSONLogger

type JSONLogger struct {
	Logger
}

JSONLogger is a console logger that has a format of `{"applicationName": "{ApplicationName}", "type": "{Type}"", "message": "{Message}"}`

func NewJSONLogger

func NewJSONLogger(applicationName string, minimumLogLevel LogType) *JSONLogger

NewJSONLogger returns an instance of an ILogger interface set to the JSON logger format

func (*JSONLogger) Debugf

func (logger *JSONLogger) Debugf(message string, args ...interface{})

Debugf writes a formatted debug entry to the log

func (*JSONLogger) DisableColors

func (logger *JSONLogger) DisableColors()

DisableColors turns of console coloring

func (*JSONLogger) EnableColors

func (logger *JSONLogger) EnableColors()

EnableColors turns on console coloring

func (*JSONLogger) Errorf

func (logger *JSONLogger) Errorf(message string, args ...interface{})

Errorf writes a formatted error entry to the log

func (*JSONLogger) Infof

func (logger *JSONLogger) Infof(message string, args ...interface{})

Infof writes a formatted info entry to the log

type LogFormat

type LogFormat int

LogFormat describes how to format log messages

const (
	LOG_FORMAT_SIMPLE LogFormat = iota
	LOG_FORMAT_JSON
)

Constants for the available log formats

func StringToLogFormat

func StringToLogFormat(logFormatName string) LogFormat

StringToLogFormat converts a specified string to a LogFormat. If the string does not match a specific log type the SIMPLE is returned.

func (LogFormat) String

func (format LogFormat) String() string

String returns the friendly name of a specified log format

type LogType

type LogType int

LogType represents a type and level of logging

const (
	NONE LogType = iota
	DEBUG
	INFO
	WARN
	ERROR
	FATAL
)

Constants for the type and levels of logging

func StringToLogType

func StringToLogType(logTypeName string) LogType

StringToLogType converts a specified string to a LogType. If the string does not match a specific log type the NONE is returned.

func (LogType) Color

func (logType LogType) Color() color.Attribute

Color returns the color attribute for this log type

func (LogType) String

func (logType LogType) String() string

String returns the friendly name of a specified log type/level

type Logger

type Logger struct {
	ApplicationName string
	LogLevel        LogType
	LogFormat       LogFormat
	// contains filtered or unexported fields
}

Logger represents the basic instance of a logging object. Other, more specific loggers will use this

type LogrusMiddleware

type LogrusMiddleware struct {
	// contains filtered or unexported fields
}

type MiddlewareLogger

type MiddlewareLogger struct {
	*logrus.Logger
}

func (MiddlewareLogger) Debugj

func (l MiddlewareLogger) Debugj(j log.JSON)

func (MiddlewareLogger) Errorj

func (l MiddlewareLogger) Errorj(j log.JSON)

func (MiddlewareLogger) Fatalj

func (l MiddlewareLogger) Fatalj(j log.JSON)

func (MiddlewareLogger) Infoj

func (l MiddlewareLogger) Infoj(j log.JSON)

func (MiddlewareLogger) Level

func (l MiddlewareLogger) Level() log.Lvl

func (MiddlewareLogger) Output

func (l MiddlewareLogger) Output() io.Writer

func (MiddlewareLogger) Panicj

func (l MiddlewareLogger) Panicj(j log.JSON)

func (MiddlewareLogger) Prefix

func (l MiddlewareLogger) Prefix() string

func (MiddlewareLogger) Printj

func (l MiddlewareLogger) Printj(j log.JSON)

func (MiddlewareLogger) SetLevel

func (l MiddlewareLogger) SetLevel(lvl log.Lvl)

func (MiddlewareLogger) SetOutput

func (l MiddlewareLogger) SetOutput(w io.Writer)

func (MiddlewareLogger) SetPrefix

func (l MiddlewareLogger) SetPrefix(s string)

func (MiddlewareLogger) Warnj

func (l MiddlewareLogger) Warnj(j log.JSON)

type SimpleLogger

type SimpleLogger struct {
	Logger
}

SimpleLogger is a basic console logger that has a format of `{ApplicationName}: {Type} - {Message}`

func NewSimpleLogger

func NewSimpleLogger(applicationName string, minimumLogLevel LogType) *SimpleLogger

NewSimpleLogger returns an instance of an ILogger interface set to the simple logger format

func (*SimpleLogger) Debugf

func (logger *SimpleLogger) Debugf(message string, args ...interface{})

Debugf writes a formatted debug entry to the log

func (*SimpleLogger) DisableColors

func (logger *SimpleLogger) DisableColors()

DisableColors turns of console coloring

func (*SimpleLogger) EnableColors

func (logger *SimpleLogger) EnableColors()

EnableColors turns on console coloring

func (*SimpleLogger) Errorf

func (logger *SimpleLogger) Errorf(message string, args ...interface{})

Errorf writes a formatted error entry to the log

func (*SimpleLogger) Infof

func (logger *SimpleLogger) Infof(message string, args ...interface{})

Infof writes a formatted info entry to the log

type StringLogger

type StringLogger struct {
	Logger

	Messages []string
}

StringLogger is a logger that stores log entries in an array that has a format of `{ApplicationName}: {Type} - {Message}`. This is useful for things like unit tests

func NewStringLogger

func NewStringLogger(applicationName string, minimumLogLevel LogType) *StringLogger

NewStringLogger returns an instance of an ILogger interface set to the simple logger format

func (*StringLogger) Debugf

func (logger *StringLogger) Debugf(message string, args ...interface{})

Debugf writes a formatted debug entry to the log

func (*StringLogger) DisableColors

func (logger *StringLogger) DisableColors()

DisableColors turns of console coloring

func (*StringLogger) EnableColors

func (logger *StringLogger) EnableColors()

EnableColors turns on console coloring

func (*StringLogger) Errorf

func (logger *StringLogger) Errorf(message string, args ...interface{})

Errorf writes a formatted error entry to the log

func (*StringLogger) Infof

func (logger *StringLogger) Infof(message string, args ...interface{})

Infof writes a formatted info entry to the log

Jump to

Keyboard shortcuts

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