gologger

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2020 License: Apache-2.0 Imports: 6 Imported by: 1

README

Gologger

Go report card Build Status GoDoc Maintenance License GitHub release GitHub issues PRs Welcome

Gologger is a concurrent thread safe logging system which writes to the filesystem. Gologger utilises a service worker model with buffered channels/queues. No need to worry about leaking file descriptors as Gologger only opens one descriptor per file.

  • Non blocking, uses a queue whith a service worker to proccess logs.
  • Thread/Concurrent safe.
  • Supports any data type! Supports interface{} type.
  • Use logger just like fmt, provide as many arguments as you need.
  • Advanced features such as naming conventions e.g. logs-Jul-2020.txt

Install

go get github.com/syrinsecurity/gologger

Basic Examples

Custom loggers

func main() {

	/*
	"./file.log" is where the logs will be written to. If this file does not exist it will be created.

	"200" is the buffer size of the channel
	*/
	logger, err := gologger.New("./file.log", 200)
	if err != nil {
		panic(err)
	}

	logger.WriteString("log any data you want.")
	logger.WriteString("This is a interface so you can use any type you like.", "UserID:", 9039832898, "Timestamp:", time.Now().Unix())
}
package main

import "github.com/syrinsecurity/gologger"

var (
	logger, _ = gologger.New("./file.log", 200)
)

func main() {
	logger.Write("data", 123, 345)
}

Use the built in loggers for fast setup

package main

import "github.com/syrinsecurity/gologger"

func main() {

	//Start the service worker
	//Leave the path blank for loggers you do not want to use
	go gologger.Service("./error.log", "", "")

	gologger.Write(gologger.LogError, "error data")

	//Quick logger syntax
	gologger.Error.Write(errors.New("example error"))
}

Advanced Usage

package main

import (
	"github.com/syrinsecurity/gologger"
)

type structExample struct {
	Feild1 string
	Feild2 string
}

func main() {
	//Create a new custom logger
	log := gologger.NewCustomLogger("./logs-", ".txt", 0)

	//This will make the filename update every month for example: logs-Jul-2020.txt
	gologger.SetNameConventionMonthYear(log)

	//Start the logger service on another goroutine
	go log.Service()

	//Make sure to close the logger
	defer log.Close()

	//write multipule values to the log with any data type
	log.Write("test", 1, 2, 2)

	//Convert any object to JSON and write it to the log
	log.WriteJSON(structExample{
		Feild1: "value1",
		Feild2: "value2",
	})

	//Byte arrays are written directly to the file, meaning no additional formating like fmt would
	log.Write([]byte{0, 3, 86, 32})

}
Naming conventions
  • SetNameConventionMonthYear() Jan-2006
  • SetNameConventionYear() 2006
  • SetNameConventionDayMonthYear() Monday-Jan-2006

You can create your own naming conventions by setting logger.NameingConvention to equal a func() string of your choosing. Note By default the naming convention returns a empty string. If you do not want to use a naming convention you can just not modify the default.

Settings
  • LineTerminator - Appends a suffix to the end of the log, default is "\n"
  • ValueSeperator - Is the value inbetween each value insert when you use multipule values on the write method, default is " "
  • ConventionUpdate - Determins how often the filename should be checked for new changes
  • Extention - This is your file extention what is appened after the name convention
  • Path - This is where you want to store the log file, it also includes the start of the filename
Methods & Callbacks
  • Close() - This will shutdown the service worker
  • ConventionUpdated(oldFile string, newFile string) - This call back is returned when the naming convention has changed thus a new log file has been created.
    • You could use this callback to backup the "old" log file.

Get the size of all basic queues:

gologger.QueueSize()

For more examples look at https://github.com/syrinsecurity/gologger/tree/master/examples

Documentation

Index

Constants

View Source
const (
	//LogError is used to log any sort of error, both internal and external
	LogError = 0

	//LogCritical is used to log any large issues/errors
	LogCritical = 1

	//LogTraffic logs web traffic
	LogTraffic = 2

	//SystemLogIfCreateFail will log to stdout and LogError if the creation of the Logger fails
	SystemLogIfCreateFail = 0

	//PanicIfFileError will panic the program if a error is encountered
	PanicIfFileError = 1
)

Variables

View Source
var (
	//Error is used to log any sort of error, both internal and external
	Error = LogType{0}

	//Critical is used to log any large issues/errors
	Critical = LogType{1}

	//Traffic logs web traffic
	Traffic = LogType{2}
)

Functions

func QueueSize

func QueueSize() int

QueueSize returns the current size of all queues

func Service

func Service(errorLog string, trafficLog string, criticalLog string)

Service handles the log events and writes them to disk. Leave errorLog blank if you do not want to use it, same applies to any other log

func SetNameConventionDayMonthYear

func SetNameConventionDayMonthYear(logger *CustomLogger)

SetNameConventionDayMonthYear will automatically configure the logger to use this convention, it will set the convention function and the update period

func SetNameConventionMonthYear

func SetNameConventionMonthYear(logger *CustomLogger)

SetNameConventionMonthYear will automatically configure the logger to use this convention, it will set the convention function and the update period

func SetNameConventionYear

func SetNameConventionYear(logger *CustomLogger)

SetNameConventionYear will automatically configure the logger to use this convention, it will set the convention function and the update period

func Write

func Write(logType int, data ...interface{})

Write will sort the event into the correct queue then procress the action as write to disk

func WritePrint

func WritePrint(logType int, data ...interface{})

WritePrint will print the data and send it to Systemlog.Write()

Types

type CustomLogger

type CustomLogger struct {
	Path string
	//Extention is the file extention; .txt, .json, .log
	Extention string
	//NameingConvention returns a string which will be used as the filename
	NameingConvention func() string

	//LineTerminator usually is "\n"
	LineTerminator string

	//ValueSeperator is inserted after each value in the data array of a write
	ValueSeperator string

	//ConventionUpdate is how long the logger should wait until it checks the current naming convention and if its time to change the file handle will be changed
	ConventionUpdate time.Duration

	//ConventionUpdated can be used to backup the old log file after the convention has change
	ConventionUpdated func(oldFile string, newFile string)
	// contains filtered or unexported fields
}

CustomLogger is heavily customisable logger which allows for complex file naming conventions and backups

func NewCustomLogger

func NewCustomLogger(path string, extention string, bufferSize int) *CustomLogger

NewCustomLogger creates a new custom logger

func (*CustomLogger) Close

func (l *CustomLogger) Close()

Close will shutdown the service worker

func (*CustomLogger) Service

func (l *CustomLogger) Service() error

Service will start the logging service

func (*CustomLogger) Write

func (l *CustomLogger) Write(data ...interface{})

Write will add the data to the queue then write to disk

func (*CustomLogger) WriteJSON

func (l *CustomLogger) WriteJSON(object interface{})

WriteJSON will take in a object and serialise it as JSON

func (*CustomLogger) WritePrint

func (l *CustomLogger) WritePrint(data ...interface{})

WritePrint will write the data to the file but also print it to the screen

type LogType

type LogType struct {
	// contains filtered or unexported fields
}

LogType is a datatype for determining what logger to use

func (LogType) Write

func (l LogType) Write(data ...interface{})

Write will sort the event into the correct queue then procress the action as write to disk

func (LogType) WritePrint

func (l LogType) WritePrint(data ...interface{})

WritePrint will print the data and send it to Systemlog.Write()

type Logger

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

Logger is used to log to a file

func New

func New(filename string, bufferSize int, options ...int) (Logger, error)

New will create a new logger and service

func (*Logger) Write

func (l *Logger) Write(data ...interface{})

Write will queue the data to be written to disk

func (*Logger) WriteJSON

func (l *Logger) WriteJSON(data interface{}) error

WriteJSON will encode the data as json and write it to disk

func (*Logger) WritePrint

func (l *Logger) WritePrint(data ...interface{})

WritePrint will print the data and send it to *Logger.Write()

func (*Logger) WriteString

func (l *Logger) WriteString(data ...interface{})

WriteString will queue the data to be written to disk

Directories

Path Synopsis
examples
ErrorLogger command
customLogger command
default command

Jump to

Keyboard shortcuts

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