Skip to content
/ msg Public
forked from apsdehal/go-logger

multi-format golang logging with πŸ˜­πŸ˜‘πŸ˜€πŸ˜„πŸ˜‹πŸ˜•

License

Notifications You must be signed in to change notification settings

szampardi/msg

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

28 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

msg

Overview

msg is a Golang logging library forked from https://github.com/apsdehal/go-logger. It has the following features I could not find anywhere else (hence the fork):

  • Ability to create new or reconfigure existing log levels
  • Log-level based colored output
  • Custom format for messages
  • Custom time format for messages
  • Multiple pre-configured formats to pick from:
  • 0 external imports.

Installation

go get -v -u github.com/nexus166/msg

Usage

Example below demonstrates some ways to start using msg

package main

import (
	"os"
	"strings"
	"time"

	"github.com/nexus166/msg"
)

var l *msg.Logger

func init() {
	msg.Info("You can immediately use the default Logger, or configure new Loggers")
	var err error
	// Formats is a map[string]Format, it is easy to access the various formats with a simple commandline flag
	l, err = msg.New(msg.Formats["cli"].String(), msg.Formats["rfc822"].String(), "msg-cli", true, os.Stdout, msg.LDebug) //configure the global l *msg.Logger
	if err != nil {
		msg.Fatal(err.Error())
	}
	msg.Info("New Logger configured!")
}

func main() {
	for f, F := range msg.Formats { // lets test them all
		if !strings.Contains(f, "rfc") {
			for _, b := range []bool{false, true} {
				// to use a preconfigured format, you need to pass the format string to msg.New(). For this, String() method is available on preconfigured Formats.
				l, errL := msg.New(F.String(), msg.Formats[msg.DetailedTimeFmt].String(), "msg-"+f, b, os.Stderr, msg.LDebug)
				if errL != nil {
					msg.Error(errL.Error())
				}
				for _, lvl := range msg.Levels {
					l.Log(msg.Lvl(lvl.ID), "Log "+lvl.Str+", format: "+f)
				}
			}
		}
	}
	for _, lvl := range msg.Levels {
		// send a message in this project's original format. You can pass constants directly from time lib to msg as a time format.
		l, _ = msg.New("#%[1]d %[2]s %[4]s:%[5]d β–Ά %.3[6]s %[7]s", time.Kitchen, "custom-formats", true, os.Stdout, msg.LDebug)
		l.Log(msg.Lvl(lvl.ID), "Log "+lvl.Str+" legacy format")
	}
}

Running the above code will demo all available formats (and the customized one):

image

License

The BSD 3-Clause license, the same as the Go language and the original project https://github.com/apsdehal/go-logger

About

multi-format golang logging with πŸ˜­πŸ˜‘πŸ˜€πŸ˜„πŸ˜‹πŸ˜•

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%