logger

package module
v0.0.0-...-69fc7af Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2020 License: MIT Imports: 10 Imported by: 0

README

Gologger

General Information

This is a simple implementation of a logger for Go. It is based on zap and lumberjack. It is written in go and has been tested on version 1.14.6 on Mac OS X and Go version 1.13.7 on Windows 10.

I created this becuse the out of the box log package in go did not do everythiing I wanted and I needed to integrate log file rotation.

Sources

This code is based off this blog post and code: https://sunitc.dev/2019/05/27/adding-uber-go-zap-logger-to-golang-project/

To Do:

  • Add logrus option

Installation

go get github.com/pedeveaux/gologger

Use

As currently configured this logger will log to both the console and a file. The console logs will be colorized to easily identify logging levels. The file will be JSON formatted for ease of automated parsing. If the file specified in the config does not exist it will be created.

examples/main.go:

package main

import(
	logger "github.com/pedeveaux/gologger"
)

var(

	log = logger.InitLogger()
)

func main(){
	log.Debugf("This should be Debug logged")
	log.Infof("This should be Info logged")
	log.Warnf("This should be Warninig logged")
	log.Errorf("This should be error logged")
}

examples/config.json:

{
    "logConfig": {
        "EnableConsole": true,
        "ConsoleLevel": "logger.Debug",
        "ConsoleJSONFormat": false,
        "EnableFile": true,
        "FileLevel": "logger.Info",
        "FileJSONFormat": true,
        "FileLocation": "program.log",
        "MaxSize": 10,
        "Compress": true,
        "MaxAge": 90
    }
}

Most of the configuration parameters should be self explanatory. The MaxSize and MaxAge attributes apply to log file rotation. The MaxSize is in megabytes. The MaxAge is in days.

Documentation

Index

Constants

View Source
const (
	//Debug has verbose message
	Debug = "debug"
	//Info is default log level
	Info = "info"
	//Warn is for logging messages about possible issues
	Warn = "warn"
	//Error is for logging errors
	Error = "error"
	//Fatal is for logging fatal messages. The sytem shutsdown after logging the message.
	Fatal = "fatal"
)
View Source
const (
	//InstanceZapLogger is an instance of the zap logger
	InstanceZapLogger int = iota
	//InstanceLogrusLogger is an instance of the logrus logger
	InstanceLogrusLogger int = 2
)

Variables

This section is empty.

Functions

func Debugf

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

Debugf is the debug level logger instance

func Errorf

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

Errorf is the error level logger instance

func Fatalf

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

Fatalf is the fatal level logger instance

func Infof

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

Infof is the info level logger instance

func NewLogger

func NewLogger(config Configuration, loggerInstance int) error

NewLogger returns an instance of logger

func Panicf

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

Panicf is the panic level logger instance

func Warnf

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

Warnf is the warn level logger instance

Types

type Config

type Config struct {
	LogConfig struct {
		EnableConsole     bool   `json:"enableConsole"`
		ConsoleLevel      string `json:"consoleLevel"`
		ConsoleJSONFormat bool   `json:"consoleJSONformat"`
		EnableFile        bool   `json:"enableFile"`
		FileLevel         string `json:"fileLevel"`
		FileJSONFormat    bool   `json:"fileJSONformat"`
		FileLocation      string `json:"fileLocation"`
		MaxSize           int    `json:"maxSize"` //MB
		Compress          bool   `json:"compress"`
		MaxAge            int    `json:"maxAge"` //Days
	} `json:"logconfig"`
}

Config defines the struct for the application configuration

func LoadConfiguration

func LoadConfiguration(filename string) (config Config, err error)

LoadConfiguration loads the configuration for the application

type Configuration

type Configuration struct {
	EnableConsole     bool   `json:"enableConsole"`
	ConsoleLevel      string `json:"consoleLevel"`
	ConsoleJSONFormat bool   `json:"consoleJSONformat"`
	EnableFile        bool   `json:"enableFile"`
	FileLevel         string `json:"fileLevel"`
	FileJSONFormat    bool   `json:"fileJSONformat"`
	FileLocation      string `json:"fileLocation"`
	MaxSize           int    `json:"maxSize"`
	Compress          bool   `json:"compress"`
	MaxAge            int    `json:"maxAge"`
}

Configuration stores the config for the logger For some loggers there can only be one level across writers, for such the level of Console is picked by default

type Fields

type Fields map[string]interface{}

Fields Type to pass when we want to call WithFields for structured logging

type Logger

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

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

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

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

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

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

	WithFields(keyValues Fields) Logger
}

Logger is our contract for the logger

func InitLogger

func InitLogger() Logger

InitLogger creates a zap logger for use in the program

func WithFields

func WithFields(keyValues Fields) Logger

WithFields allows the creation of custom fields as key value pairs.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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