log

package module
v0.0.0-...-20a49a7 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2022 License: MIT Imports: 7 Imported by: 0

README

Logger

This logger is common logger package designed to be used in any Go project. With this wrapper one can do following easily,

  • Log rotation
  • Sending logs to multiple syncer such as rsyslog
  • Custom service properties such as service name, version etc.

Usage

Follow the instructions to use logger.

Instantiation

To create a logger instance one only needs the application name and version, two params to be supplied to the log.NewLogger() method. In this case all other values will be set to default. To override default configuration just use methods such as logger.WithLogLevel("debug").

	l, err := log.NewLogger("MyService", "1.0.0", logger.WithLogLevel("debug"), logger.WithRotationSize(2))
	if err != nil {
		log.Fatal("failed to get the logger, error: ", err)
	}

Log levels and Logging

Supported log levels are debug, info, warn, error, panic, and fatal.

Logging with different methods,

l, err := log.NewLogger("MyService", "1.0.0")
if err != nil {
    log.Fatal("failed to get the logger, error: ", err)
}
l.Debug("my logs message goes here")
l.Info("log message with addtional fields", "field", "value")

Context based logging,

l, err := log.NewLogger("MyService", "1.0.0")
if err != nil {
    log.Fatal("failed to get the logger, error: ", err)
}
ctxLogger := l.With("myfield", "myvalue")
ctxLogger.Debug("my message goes here")
ctxLogger.Debug("my another message goes here")
// both above logs will have myfield and myvalue been set to the log entry.

Change Log level

l, err := log.NewLogger("MyService", "1.0.0")
if err != nil {
    log.Fatal("failed to get the logger, error: ", err)
}
l.SetLogLevel("warn")
// use any one of the support log level in the argument.

Change log rotation params

l, err := log.NewLogger("MyService", "1.0.0")
if err != nil {
    log.Fatal("failed to get the logger, error: ", err)
}
l.SetRotationSize(20)
// Specify an integer value for size in MB.
l.SetRotationCount(5)
// Specify an integer value for total number files to be kept on disk

Send logs to Syslog

l, err := log.NewLogger(&logger.Config{SyslogPort: "514",})
if err != nil {
    log.Fatal("failed to get the logger, error: ", err)
}

Documentation

Overview

go:build !windows

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetSyslog

func GetSyslog(protocol, conn, tag string) (io.Writer, error)

func NewLogger

func NewLogger(app, version string, opts ...Option) (*logger, error)

NewLogger is a wrapper create around Zap logger

Types

type Logger

type Logger interface {
	Debug(msg string, keyvals ...interface{})

	Info(msg string, keyvals ...interface{})

	Warn(msg string, keyvals ...interface{})

	Error(msg string, keyvals ...interface{})

	Panic(msg string, keyvals ...interface{})

	Fatal(msg string, keyvals ...interface{})

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

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

	With(keyvals ...interface{}) Logger

	// Set log level
	SetLogLevel(level string)
	// Set log rotation size
	SetLogRotationSize(size int)
	// Set log rotation count
	SetLogRotationCount(count int)
}

A common logger interface to be used throughout platfrom components

type Option

type Option func(*logger)

An Option configures a logger.

func WithEncoding

func WithEncoding(ec string) Option

WithEncoding overrides default encoding

func WithInstanceID

func WithInstanceID(instanceID string) Option

WithrotationCount overrides default log rotation file count

func WithLogFile

func WithLogFile(filePath string) Option

WithLogFile overrides default log file path

func WithLogLevel

func WithLogLevel(level string) Option

WithLogLevel overrides the default log level

func WithOutput

func WithOutput(w io.Writer) Option

WithOutput configures the logger to write to stdout also

func WithRotationCount

func WithRotationCount(count int) Option

WithrotationCount overrides default log rotation file count

func WithRotationSize

func WithRotationSize(size int) Option

WithrotationSize overrides default log rotation file size

func WithSyslog

func WithSyslog(conn, protocol string) Option

WithSyslog configures the syslog server for log forwarding

Jump to

Keyboard shortcuts

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