logger

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2020 License: MIT Imports: 4 Imported by: 0

README

logger

Simple log to terminal

This simple library prints, based on set LogLevel, nice formatted to terminal

Screenshot

usage
import {
    "github.com/altafino/logger"
    "flag"
}

func main {
    level:=flag.String("log","info","sets log level")
    terminalStyle:=flag.String("style","flat","sets terminal style")
    flag.Parse()
    
    InitLog(*level,*terminalStyle)
}

func InitLog(level string, style string) {

	fmt.Println("level:", level, "style:", style)

	lsettings := logger.Settings{}

	switch level {
	case "debug":
		lsettings = logger.Settings{
			Level: logger.DebugLevel,
		}
	case "info":
		lsettings = logger.Settings{
			Level: logger.InfoLevel,
		}
	case "http":
		lsettings = logger.Settings{
			Level: logger.HttpLevel,
		}
	case "critical":
		lsettings = logger.Settings{
			Level: logger.CriticalLevel,
		}
	case "disabled":
		lsettings = logger.Settings{
			Level: logger.Disabled,
		}
	default:
		lsettings = logger.Settings{
			Level: logger.InfoLevel,
		}
	}

	lsettings.Output = logger.Terminal

	switch style {
	case "flat":
		lsettings.TerminalStyle = logger.FlatStyle
	case "json":
		lsettings.TerminalStyle = logger.JsonStyle
	default:
		lsettings.TerminalStyle = logger.FlatStyle
	}

	logger.InitLogger(lsettings)
	logger.Info("Logger Started", lsettings)

}


use as http middleware
example with go-chi
import {
    "github.com/altafino/logger/middleware"
}
// ....
router.Use(
		render.SetContentType(render.ContentTypeJSON), // Set content-Type headers as application/json
		middleware.Logger,             // Log API request calls
		chiMiddleware.DefaultCompress, // Compress results, mostly gzipping assets and json
		chiMiddleware.RedirectSlashes, // Redirect slashes to no slash URL versions
		chiMiddleware.Recoverer,       // Recover from panics without crashing server
		cors.Handler,
	)
// ....

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Critical

func Critical(interf ...interface{})

func Debug

func Debug(interf ...interface{})

func Error

func Error(interf ...interface{})

func Http

func Http(interf ...interface{})

func Info

func Info(interf ...interface{})

func InitLogger

func InitLogger(settings Settings)

func Only added in v1.0.6

func Only(interf ...interface{})

Types

type Level

type Level int
const (
	InfoLevel     Level = 0
	HttpLevel     Level = 1 // Special Info Level for eg HTTP
	ErrorLevel    Level = 2
	CriticalLevel Level = 3
	DebugLevel    Level = 4
	OnlyLevel     Level = 5
	Disabled      Level = 6
)

func (Level) String

func (level Level) String() string

type Output

type Output int
const (
	Terminal Output = 0
	Json     Output = 1
)

type Settings

type Settings struct {
	Level         Level
	Output        Output
	TerminalStyle TerminalStyle
}
var LoggerSettings Settings

type TerminalStyle

type TerminalStyle int
const (
	FlatStyle TerminalStyle = 0
	JsonStyle TerminalStyle = 1
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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