log

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2024 License: MIT Imports: 8 Imported by: 0

README

tiny-go-log

A tiny conveniently setup go rolling logger powered by zerolog and lumberjack based on panta's gist

Usage

package main

import (
    "github.com/gugabfigueiredo/tiny-go-log"
)

func main() {
    
    // create new logger from local config.go struct
    logger := log.New(&log.Config{
        // Context is the application name. 
        // It is also used to compose the file path if file logging is enabled
        Context: "my-app",
        // Level is the log level the logger should log at
        Level: "info",
        // Enable console logging, defaults to true
        ConsoleLoggingEnabled: true,
        // EncodeLogsAsJson makes the log framework log JSON
        EncodeLogsAsJson: true,
        // FileLoggingEnabled makes the framework log to a file
        // the fields below can be skipped if this value is false!
        FileLoggingEnabled: true,
        // Directory to log to when file logging is enabled
        Directory: "/var/log",
        // Filename is the name of the logfile which will be placed inside the directory
        Filename: "my-app",
        // MaxSize the max size in MB of the logfile before it's rolled
        MaxSize: 10,
        // MaxBackups the max number of rolled files to keep
        MaxBackups: 3,
        // MaxAge the max age in days to keep a logfile
        MaxAge: 2,
    })
    
    logger.Info("Hello World")
}

expected output:

{"level":"info","time":"2020-05-01T00:00:00Z","message":"Hello World","context":"my-app"}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTagsOddCount     = errors.New("odd logger tag count")
	ErrTagsKeyNotString = errors.New("tag key is not a string")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	// Level is the log level to use
	Level zerolog.Level `default:"1"`
	// Context is the application name
	Context string `required:"true"`
	// Enable console logging
	ConsoleLoggingEnabled bool `default:"true"`
	// EncodeLogsAsJson makes the log framework log JSON
	EncodeLogsAsJson bool `default:"true"`
	// FileLoggingEnabled makes the framework log to a file
	// the fields below can be skipped if this value is false!
	FileLoggingEnabled bool `default:"false"`
	// Directory to log to when file logging is enabled
	Directory string `default:"/var/log"`
	// Filename is the name of the logfile which will be placed inside the directory
	Filename string `default:"my-app"`
	// MaxSize the max size in MB of the logfile before it's rolled
	MaxSize int `default:"10"`
	// MaxBackups the max number of rolled files to keep
	MaxBackups int `default:"10"`
	// MaxAge the max age in days to keep a logfile
	MaxAge int `default:"2"`
}

Config is the configuration for the logger, supported by envconfig: https://github.com/kelseyhightower/envconfig

type ILogger

type ILogger interface {
	With(tags ...any) ILogger
	Debug(message string, tags ...any)
	Fatal(message string, tags ...any)
	Error(message string, tags ...any)
	Warn(message string, tags ...any)
	Info(message string, tags ...any)
	Log(message string, tags ...any)
}

type Logger

type Logger struct {
	zerolog.Logger
	// contains filtered or unexported fields
}

func New

func New(config *Config) *Logger

New sets up the logging framework

func (*Logger) Debug

func (l *Logger) Debug(message string, tags ...any)

func (*Logger) Error

func (l *Logger) Error(message string, tags ...any)

func (*Logger) Fatal

func (l *Logger) Fatal(message string, tags ...any)

func (*Logger) Info

func (l *Logger) Info(message string, tags ...any)

func (*Logger) Log

func (l *Logger) Log(message string, tags ...any)

func (*Logger) Warn

func (l *Logger) Warn(message string, tags ...any)

func (*Logger) With

func (l *Logger) With(tags ...any) ILogger

Jump to

Keyboard shortcuts

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