Logger

package module
v0.0.0-...-3fc21e2 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2019 License: Apache-2.0 Imports: 4 Imported by: 0

README

go-logger Golang Logger

Logger on steroids.

Get it

go get github.com/bestmethod/go-logger

What's new

Async option

There is now support for async logging. To avoid slowdown of main code when logging to stdout, one can run the logger in Async mode - which in turn uses goroutines. This allows for the main code execution without delay. To use, set logger.Async to true AFTER running the logger.Init().

Warning: Use Async at your own peril. If your logging falls behind the main code, you risk exhausting handles, memory or goroutine space and crashing. This is no designed as a queuing mechanism for logging, but marely to allow for more real-time of main code execution.
logger := new(Logger.Logger)
logger.Init(header, serviceName, stdoutLevels, stderrLevels, devlogLevels)
logger.Async = true

Usage

Create object and Init
logger := new(Logger.Logger)
err := logger.Init(header, serviceName, stdoutLevels, stderrLevels, devlogLevels)
Functions
func (l *Logger) Init(header string,serviceName string,stdoutLevel int,stderrLevel int,devlogLevel int) error {}
func (l *Logger) Debug(format string, args ...interface{}) {}
func (l *Logger) Info(format string, args ...interface{}) {}
func (l *Logger) Warn(format string, args ...interface{}) {}
func (l *Logger) Error(format string, args ...interface{}) {}
func (l *Logger) Critical(format string, args ...interface{}) {}
func (l *Logger) Fatal(format string, args ...interface{}) {}
func (l *Logger) Fatalf(exitCode int,format string, args ...interface{}) {}
func (l *Logger) Destroy() error {}
func (l *Logger) TimeFormat(newFormat string) {}
Destroy objects before forgetting them

This will destroy the devlog handler and the connection to devlog that it holds in the background (open handle). If you are crazy enough to Init and destroy 100s of loggers in your code, you may want to use this.

Note that this will not stop the logger from working if you use it afterwards. This just disables and closes the devlog type logger as a cleanup procedure. If you do not use devlog logger, don't worry about this at all.

err := logger.Destroy()

Example

import "github.com/bestmethod/logger"
import "fmt"
import "os"

logger := new(Logger.Logger)
err := logger.Init("SUBNAME", "SERVICENAME", Logger.LEVEL_DEBUG | Logger.LEVEL_INFO | Logger.LEVEL_WARN, Logger.LEVEL_ERROR | Logger.LEVEL_CRITICAL, Logger.LEVEL_NONE)
if err != nil {
  fmt.Fprintf(os.Stderr, "CRITICAL Could not initialize logger. Quitting. Details: %s\n", err)
  os.Exit(1)
}

// standard logger messages  
logger.Info("This is info message")
logger.Debug("This is debug message")
logger.Error("This is error message")
logger.Warn("This is warning message")
logger.Critical("This is critical message")

// logger messages, like Printf (auto-discovery of printf happens, so same functions are used)
logger.Info("%s %v","This is info message",10)
logger.Debug("%s %v","This is debug message",10)
logger.Error("%s %v","This is error message",10)
logger.Warn("%s %v","This is warning message",10)
logger.Critical("%s %v","This is critical message",10)

// standard fatal exit with custom exit code
code := 1
logger.Fatal("This is a critical message that terminates the program with os.exit(code)", code)

// fatal with printf has a separate fatalf
code = 1
logger.Fatalf(code,"%s","This is a critical message that terminates the program with os.exit(code)")

Documentation

Index

Constants

View Source
const (
	LEVEL_DEBUG    = 16
	LEVEL_INFO     = 8
	LEVEL_WARN     = 4
	LEVEL_ERROR    = 2
	LEVEL_CRITICAL = 1
	LEVEL_NONE     = 0
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Logger

type Logger struct {
	Header      string
	ServiceName string

	Stdout      *os.File
	StdoutLevel int
	Stderr      *os.File
	StderrLevel int
	Devlog      *syslog.Writer
	DevlogLevel int

	Async  bool
	Format string
	// contains filtered or unexported fields
}

func (*Logger) Critical

func (l *Logger) Critical(format string, args ...interface{})

func (*Logger) Debug

func (l *Logger) Debug(format string, args ...interface{})

func (*Logger) Destroy

func (l *Logger) Destroy() error

func (*Logger) DevlogInit

func (l *Logger) DevlogInit() error

func (*Logger) DevlogOsCheck

func (l *Logger) DevlogOsCheck() error

func (*Logger) Error

func (l *Logger) Error(format string, args ...interface{})

func (*Logger) Fatal

func (l *Logger) Fatal(m string, exitCode int)

func (*Logger) Fatalf

func (l *Logger) Fatalf(exitCode int, format string, args ...interface{})

func (*Logger) Info

func (l *Logger) Info(format string, args ...interface{})

func (*Logger) Init

func (l *Logger) Init(header string, serviceName string, stdoutLevel int, stderrLevel int, devlogLevel int) error

func (*Logger) TimeFormat

func (l *Logger) TimeFormat(newFormat string)

func (*Logger) Warn

func (l *Logger) Warn(format string, args ...interface{})

Jump to

Keyboard shortcuts

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