Skip to content

Latest commit

History

21 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

# logger (A logger package for Go)

This package is for logging messages to file. The log format can be in json or can be normal.

Its prettly simple to use.

Create the logger Instance - Use 'CreateLogger' function
There are 5 methods available to log, all of these linked to a log level.

  • Debug(format string, a ...interface{})
  • Info(format string, a ...interface{})
  • Warn(format string, a ...interface{})
  • Error(format string, a ...interface{})
  • Fatal(format string, a ...interface{})

Each of the above functions are assoiated to 5 Loglevels.

  • LDebug
  • LInfo
  • LWarn
  • LError
  • LFatal

Any of the log level functions can be called from any goroutine and the logging would not be affected.

# How to install

go get github.com/r-pai/logger

# How to use

import the package

import"github.com/r-pai/logger"

Create Logger in main or in init.

log:=logger.CreateLogger("./", "Myapp", logger.LDebug)

Log the messages - 5 Types of LogLevels

log.Debug("Starting Hello LDEBUG")
log.Info("Starting Hello LINFO")
log.Warn("Starting Hello LWARN")
log.Error("Starting Hello LERROR")
log.Fatal("Starting Hello LFATAL")

# Sample Code 1

package main
import ( "github.com/r-pai/logger"
)
//Create the logger instancevarlog=logger.CreateLogger("./", "Myapp", logger.LDebug)
funcmain() {
log.Debug("Starting Hello LDEBUG")
log.Info("Starting Hello LINFO")
log.Warn("Starting Hello LWARN")
log.Error("Starting Hello LERROR")
log.Fatal("Starting Hello LFATAL")
}

# Sample Code 2 - Log messages in go routines

package main
import (
"github.com/r-pai/logger""time"
)
funcnumber() int {
num:=15*5returnnum
}
funcgo1() {
fori:=0; i<1000; i++ {
log.Info("%s %d", "go1 Log message", i)
time.Sleep(400*time.Millisecond)
}
}
funcgo2() {
fori:=1000; i<3000; i++ {
log.Info("%s %d", "go2 Log message", i)
time.Sleep(100*time.Millisecond)
}
}
//Create the loggervarlog*logger.GLoggerfuncinit(){
log=logger.CreateLogger("./", "Myapp", logger.LDebug)
//constants from time packagelog.SetLogTimeFormat(time.RFC850) //set the time format//logout put can be in json formatlog.SetJSONLog(true)
}
//main entry pointfuncmain() {
gogo1()
gogo2()
for {
}
}

# Loggeroutput

Sample output of file : MyApp_07-09-2019.log

Screenshot 2019-07-09 at 10 08 59 AM

Json Style output

Screen Shot 2019-08-28 at 10 41 42 pm

About

logger package for Golang

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages