Documentation ¶
Overview ¶
Package logger provides a simple logging package for GoCore. It can also log running goRoutines, track time of execution easily and tail files Note: To view running gopher logs, you must set serverSettings.WebConfig.Application.LogGophers to true so it will print out the gopher logs at set interval of serverSettings.WebConfig.Application.LogGopherInterval (set this as well)
Index ¶
- Constants
- Variables
- func GoRoutineLogger(fn func(), routineDesc string)
- func GoRoutineLoggerWithId(fn func(), routineDesc string, Id string)
- func Log(dataValues ...interface{})
- func Message(message string, c Color)
- func Tail(path string, length int64) (data string)
- func TimeTrack(start time.Time, name string) (log string)
- func TimeTrackQuery(start time.Time, name string, collection *mgo.Collection, m bson.M, ...) (log string)
- func ViewRunningGophers()
- type Color
Examples ¶
Constants ¶
const ( // RED is a color constant RED = 1 // GREEN is a color constant GREEN = 2 // YELLOW is a color constant YELLOW = 3 // BLUE is a color constant BLUE = 4 // MAGENTA is a color constant MAGENTA = 5 // CYAN is a color constant CYAN = 6 // WHITE is a color constant WHITE = 7 )
Variables ¶
var GopherTimeRunning map[string]time.Time
GopherTimeRunning is a map of all the gophers currently running and the time they started.
var RunningGophers []string
RunningGophers is a list of all the gophers currently running.
var TotalSystemGoRoutines int32
TotalSystemGoRoutines is a counter of all the go routines running in the system.
var VerboseBornAndDeadGophers bool
VerboseBornAndDeadGophers is a flag to turn on and off the verbose logging of gophers.
Functions ¶
func GoRoutineLogger ¶
func GoRoutineLogger(fn func(), routineDesc string)
GoRoutineLogger is a wrapper for go routines that will log the start and end of the go routine. Pass in a function to be executed in the go routine.
Example ¶
Heres how you can run a go routine and log if its still running every N seconds (set through serverSettings.WebConfig.Application.LogGopherInterval)
package main import ( "log" "time" "github.com/DanielRenne/GoCore/core/logger" "github.com/DanielRenne/GoCore/core/serverSettings" ) func main() { serverSettings.WebConfig.Application.LogGophers = true serverSettings.WebConfig.Application.LogGopherInterval = 5 go logger.GoRoutineLogger(func() { for { log.Println("test") time.Sleep(time.Second * 100) } }, "some long running function") time.Sleep(time.Second * 20) log.Println("Done") }
Output: 2022/10/05 00:20:12 test 2022/10/05 00:20:22 ,_---~~~~~----._ _,,_,*^____ _____ -g--"*, / __/ /' ^. / \ ^@q f [ @f | @)) | | @)) l 0 _/ \ / \~____ / __ \_____/ \ | _l__l_ I } [______] I | | | | | ] ~ ~ | | | | | at 2022-10-05 00:20:22.830692815 +0000 UTC m=+10.010449585 1 Gophers workin up in here! 2022/10/05 00:20:22 #0:Mr.ragjF-> (some long running function) (10.008959946s elapsed) 2022/10/05 00:20:27 ,_---~~~~~----._ _,,_,*^____ _____ -g--"*, / __/ /' ^. / \ ^@q f [ @f | @)) | | @)) l 0 _/ \ / \~____ / __ \_____/ \ | _l__l_ I } [______] I | | | | | ] ~ ~ | | | | | at 2022-10-05 00:20:27.834213444 +0000 UTC m=+15.013970230 1 Gophers workin up in here! 2022/10/05 00:20:27 #0:Mr.ragjF-> (some long running function) (15.012394763s elapsed) 2022/10/05 00:20:32 Done
func GoRoutineLoggerWithId ¶
GoRoutineLoggerWithId is a wrapper for go routines that will log the start and end of the go routine. Pass in a function to be executed in the go routine.
func Log ¶
func Log(dataValues ...interface{})
Log is a wrapper for the standard log package. Pass in unlimited number of parameters.
func TimeTrack ¶
TimeTrack is typically called in your defer function to log the time it took to execute a function. But can be used anywhere.
Example ¶
The simplest use of a TimeTrack caller is to simply call it with a start time and a message.
package main import ( "log" "time" "github.com/DanielRenne/GoCore/core/logger" ) func main() { start := time.Now() defer func() { if r := recover(); r != nil { log.Println("Recovered ", r) return } log.Println(logger.TimeTrack(start, "some long running function")) }() time.Sleep(time.Second * 5) }
Output:
func TimeTrackQuery ¶
func TimeTrackQuery(start time.Time, name string, collection *mgo.Collection, m bson.M, q *mgo.Query) (log string)
TimeTrackQuery is meant to be used in conjunction with the mgo package. It will log the time it took to execute a query and the query itself.
func ViewRunningGophers ¶
func ViewRunningGophers()
ViewRunningGophers prints out all the gophers currently running in the system who have been wrapped in GoRoutineLogger or GoRoutineLoggerWithId